Class: BenchmarkAllocation

Inherits:
Object show all
Defined in:
lib/utilrb/objectstats.rb

Overview

BenchmarkAllocation is a Benchmark-like interface to benchmark object allocation.

Formatting

BenchmarkAllocation formats its output in two ways (see examples below)

  • first, each part of a class path is displayed in its own line, to reduce the output width

  • then, output is formatted so that it does not exceed BenchmarkAllocation::SCREEN_WIDTH width

Examples

For instance,

require 'utilrb/objectstats'

module Namespace

class MyClass end

end

BenchmarkAllocation.bm do |x|
    x.report("array") { Array.new }
    x.report("hash") { Hash.new }
    x.report("myclass") { MyClass.new }
end

will produce the output

         Array  Hash  Namespace::
                          MyClass
  array      1     -            -
   hash      -     1            -
myclass      -     -            1

Like Benchmark, a rehearsal benchmark method, BenchmarkAllocation.bmbm is provided:

require 'utilrb/objectstats'
require 'delegate'

module Namespace
    class MyClass
    end
end

delegate_klass = nil
BenchmarkAllocation.bmbm do |x|
    x.report("array") { Array.new }
    x.report("hash") { Hash.new }
    x.report("myclass") { Namespace::MyClass.new }
    x.report("delegate") do
	delegate_klass ||= Class.new(DelegateClass(Namespace::MyClass)) do
	    def self.name; "Delegate(MyClass)" end
	end
	delegate_klass.new(Namespace::MyClass.new)
    end
end

produces

Rehearsal --------------------------------------------------------------------------------

          Array  Class  Delegate(MyClass)  Hash  Namespace::  String
                                                     MyClass
   array      1      -                  -     -            -       -
    hash      -      -                  -     1            -       -
 myclass      -      -                  -     -            1       -
delegate      5      2                  1     2            1     247
------------------------------------------------------------------------------------------

          Array  Delegate(MyClass)  Hash  Namespace::
                                              MyClass
   array      1                  -     -            -
    hash      -                  -     1            -
 myclass      -                  -     -            1
delegate      -                  1     -            1

Constant Summary collapse

SCREEN_WIDTH =
90
MARGIN =
2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bm(label_width = nil) {|gather = new| ... } ⇒ Object

Yields:

  • (gather = new)


155
156
157
158
# File 'lib/utilrb/objectstats.rb', line 155

def self.bm(label_width = nil)
	yield(gather = new)
	gather.format
end

.bmbm(label_width = nil) {|gather = new| ... } ⇒ Object

Yields:

  • (gather = new)


159
160
161
162
163
164
165
166
167
168
169
# File 'lib/utilrb/objectstats.rb', line 159

def self.bmbm(label_width = nil)
	yield(gather = new)

	title = "Rehearsal"
	puts title + " " + "-" * (SCREEN_WIDTH - title.length - 1)
	gather.format
	puts "-" * SCREEN_WIDTH

	yield(gather = new)
	gather.format
end

Instance Method Details

#format(screen_width = SCREEN_WIDTH, margin = MARGIN) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/utilrb/objectstats.rb', line 171

def format(screen_width = SCREEN_WIDTH, margin = MARGIN)
	data = profiles.map do |label, line_data|
 line_data['label'] = label
 line_data
	end
	ColumnFormatter.from_hashes(data, screen_width)
end

#report(label) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/utilrb/objectstats.rb', line 180

def report(label)
	result = ObjectStats.profile do
 yield
	end
	result.inject({}) do |result, (klass, count)|
 klass = klass.to_s
 klass = "unknown" if !klass || klass.empty?
 result[klass] = count
 result
	end
	profiles << [label, result]
end