class Bixby::Bench

Public Class Methods

new(sample_size, memory=true) click to toggle source
# File lib/bixby/bench.rb, line 32
def initialize(sample_size, memory=true)
  @sample_size = sample_size
  @memory = memory
  @samples = []
end
run(sample_size, memory=true) { |bench| ... } click to toggle source
# File lib/bixby/bench.rb, line 12
def self.run(sample_size, memory=true)
  bench = Bench.new(sample_size, memory)
  yield(bench)

  # now that we have all samples, run the thing
  sync_stdout { bench.run_all }

  bench
end
sync_stdout() { || ... } click to toggle source
# File lib/bixby/bench.rb, line 22
def self.sync_stdout
  begin
    old_sync = STDOUT.sync
    STDOUT.sync = true
    yield
  ensure
    STDOUT.sync = old_sync unless old_sync.nil?
  end
end

Public Instance Methods

add_divider()
Alias for: divider
divider() click to toggle source
# File lib/bixby/bench.rb, line 43
def divider
  @samples << Divider.new
end
Also aliased as: add_divider
divider_width() click to toggle source
# File lib/bixby/bench.rb, line 60
def divider_width
  @divider_width ||= (label_width + (@memory ? 75 : 45))
end
label_width() click to toggle source
# File lib/bixby/bench.rb, line 48
def label_width
  if !@label_width then
    @label_width = @samples.find_all{ |s| Sample === s }.
                      max{ |a, b| a.label.length <=> b.label.length }.
                      label.length + 1

    @label_width = 40 if @label_width < 40
  end

  return @label_width
end
print_header() click to toggle source
report(label, &block)
Alias for: sample
run_all() click to toggle source
# File lib/bixby/bench.rb, line 72
def run_all
  print_header

  @samples.each do |sample|
    if Divider === sample then
      sample.print(divider_width)
      next
    end

    print sample.label.ljust(label_width)
    sample.measure.print
  end
end
sample(label, &block) click to toggle source
# File lib/bixby/bench.rb, line 38
def sample(label, &block)
  @samples << Sample.new(label, block, @sample_size, @memory)
end
Also aliased as: report