summaryrefslogtreecommitdiff
path: root/support/benchmarks/load_allocations.rb
blob: b0f9768d5ab7f1f5972b39e67920cf02b38b06df (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- ruby encoding: utf-8 -*-

if RUBY_VERSION < '2.1'
  $stderr.puts "Cannot count allocations on #{RUBY_VERSION}."
  exit 1
end

begin
  require 'allocation_tracer'
rescue LoadError
  $stderr.puts "Allocation tracking requires the gem 'allocation_tracer'."
  exit 1
end

module Benchmarks
  class LoadAllocations
    def self.report(columnar: false, top_x: nil, mime_types_only: false)
      new(columnar: columnar, top_x: top_x, mime_types_only: mime_types_only).
        report
    end

    def initialize(columnar: false, top_x: nil, mime_types_only: false)
      @columnar = columnar
      @mime_types_only = !!mime_types_only

      @top_x = top_x
      if @top_x
        @top_x = top_x.to_i
        @top_x = 10 if @top_x <= 0
      end

    end

    def report
      collect
      report_top_x if @top_x
      puts "TOTAL Allocations: #{@count}"
    end

    private
    def report_top_x
      table = @allocations.sort_by { |_, v| v.first }.reverse.first(@top_x)
      table.map! { |(location, allocs)|
        next if @mime_types_only and location.first !~ %r{mime-types/lib}
        [ location.join(':').gsub(%r{^#{Dir.pwd}/}, ''), *allocs ]
      }.compact!

      head = (ObjectSpace::AllocationTracer.header - [ :line ]).map {|h|
        h.to_s.split(/_/).map(&:capitalize).join(' ')
      }
      table.unshift head

      max_widths = [].tap do |mw|
        table.map { |row| row.lazy.map(&:to_s).map(&:length).to_a }.tap do |w|
          w.first.each_index do |i|
            mw << w.lazy.map { |r| r[i] }.max
          end
        end
      end

      pattern = [ "%%-%ds" ]
      pattern << ([ "%% %ds" ] * (max_widths.length - 1))
      pattern = pattern.join("\t") % max_widths
      table.each { |row| puts pattern % row }
      puts
    end

    def collect
      if @columnar
        @allocations = ObjectSpace::AllocationTracer.trace do
          require 'mime/types/columnar'
        end
      else
        @allocations = ObjectSpace::AllocationTracer.trace do
          require 'mime/types'
        end
      end

      @count = ObjectSpace::AllocationTracer.allocated_count_table.values.
        inject(:+)
    end
  end
end