Class: Bixby::Bench

Inherits:
Object
  • Object
show all
Defined in:
lib/bixby/bench.rb,
lib/bixby/bench/report.rb,
lib/bixby/bench/sample.rb,
lib/bixby/bench/divider.rb

Defined Under Namespace

Classes: Divider, Report, Sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sample_size, memory = true) ⇒ Bench

Returns a new instance of Bench.



32
33
34
35
36
# File 'lib/bixby/bench.rb', line 32

def initialize(sample_size, memory=true)
  @sample_size = sample_size
  @memory = memory
  @samples = []
end

Class Method Details

.run(sample_size, memory = true) {|bench| ... } ⇒ Object

Yields:

  • (bench)


12
13
14
15
16
17
18
19
20
# 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_stdoutObject



22
23
24
25
26
27
28
29
30
# 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

Instance Method Details

#dividerObject Also known as: add_divider



43
44
45
# File 'lib/bixby/bench.rb', line 43

def divider
  @samples << Divider.new
end

#divider_widthObject



60
61
62
# File 'lib/bixby/bench.rb', line 60

def divider_width
  @divider_width ||= (label_width + (@memory ? 75 : 45))
end

#label_widthObject



48
49
50
51
52
53
54
55
56
57
58
# 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


64
65
66
67
68
69
70
# File 'lib/bixby/bench.rb', line 64

def print_header
  caption = Benchmark::CAPTION
  if @memory then
    caption = caption.gsub(/\n$/, '') + "       allocations      memsize\n"
  end
  print ' '*label_width + caption
end

#run_allObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object Also known as: report



38
39
40
# File 'lib/bixby/bench.rb', line 38

def sample(label, &block)
  @samples << Sample.new(label, block, @sample_size, @memory)
end