File: helper.rb

package info (click to toggle)
ruby-rspec 3.12.0c0e1m1s0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,752 kB
  • sloc: ruby: 69,818; sh: 1,861; makefile: 99
file content (30 lines) | stat: -rw-r--r-- 936 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$LOAD_PATH.unshift File.expand_path("../../../lib", __FILE__)
require 'rspec/core'
require 'allocation_stats'

def benchmark_allocations(burn: 1, min_allocations: 0)
  stats = AllocationStats.new(burn: burn).trace do
    yield
  end

  columns = if ENV['DETAIL']
              [:sourcefile, :sourceline, :class_plus]
            else
              [:class_plus]
            end

  results = stats.allocations(alias_paths: true).group_by(*columns).from_pwd.sort_by_size.to_text
  count_regex = /\s+(\d+)\z/

  total_objects = results.split("\n").map { |line| line[count_regex, 1] }.compact.map { |c| Integer(c) }.inject(0, :+)

  filtered = results.split("\n").select do |line|
    count = line[count_regex, 1]
    count.nil? || Integer(count) >= min_allocations
  end

  puts filtered.join("\n")
  line_length = filtered.last.length
  puts "-" * line_length
  puts "Total:#{total_objects.to_s.rjust(line_length - "Total:".length)}"
end