summaryrefslogtreecommitdiff
path: root/support/benchmarks/object_counts.rb
blob: d548bd14f4406860bf69c2f32725a4ff4f135c1d (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
# frozen_string_literal: true

module Benchmarks
  # Benchmark object counts
  class ObjectCounts
    def self.report(columnar: false, full: false)
      new(columnar: columnar, full: full).report
    end

    def initialize(columnar: false, full: false)
      @columnar = columnar
      @full = full
    end

    def report
      collect
      @before.keys.grep(/T_/).map { |key|
        [key, @after[key] - @before[key]]
      }.sort_by { |_, delta| -delta }.each { |key, delta|
        puts '%10s +%6d' % [key, delta]
      }
    end

    private

    def collect
      @before = count_objects

      if @columnar
        require 'mime/types'
        MIME::Types.first.to_h if @full
      else
        require 'mime/types/full'
      end

      @after = count_objects
    end

    def count_objects
      GC.start
      ObjectSpace.count_objects
    end
  end
end