class Benchpress::Entity

Attributes

data_points[RW]
method[R]
name[R]

Public Class Methods

new(steps, cycles, opts = {}) click to toggle source

Pass in the name of the entity as a symbol, and its method as a lambda

Ex:

Benchpress::Entity.new(string: -> { 'string' })

Warning: Direct instantiation has been deprecated, read the

# File lib/benchpress/entity.rb, line 12
def initialize(steps, cycles, opts = {})
  raise 'Invalid formatting! Use "name: -> { method }"' unless opts.keys.length == 1
  @cycles        = cycles
  @steps         = steps
  @name, @method = *opts.flatten
end

Public Instance Methods

data() click to toggle source
# File lib/benchpress/entity.rb, line 19
def data
  [@name.to_s, flatten_data(cycle_data)]
end

Private Instance Methods

cycle_data() click to toggle source
# File lib/benchpress/entity.rb, line 25
def cycle_data
  @cycles.times.reduce([]) { |table, _| table << measure_realtime }
end
flatten_data(table) click to toggle source
# File lib/benchpress/entity.rb, line 33
def flatten_data(table)
  if table.length == 1
    table.flatten
  else
    table.transpose.reduce([]) { |ary, data| ary << (data.reduce(:+) / data.length) }
  end
end
measure_realtime() click to toggle source
# File lib/benchpress/entity.rb, line 29
def measure_realtime
  @steps.reduce([]) { |data, n| data << Benchmark.measure { n.times { @method.call } }.real }
end