Class: RBench::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/rbench/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, group, name, times = nil, &block) ⇒ Report

Returns a new instance of Report.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbench/report.rb', line 9

def initialize(runner, group, name, times=nil,&block)
  @runner = runner
  @group  = group
  @name   = name
  @times  = (times || runner.times).ceil
  @cells  = {}
  @block  = block 
  
  # Setting the default for all cells
  runner.columns.each {|c| @cells[c.name] = c.name == :times ? "x#{@times}" : c.default }
  
  new_self = (class << self; self end)
  @runner.columns.each do |column|
    new_self.class_eval <<-CLASS
      def #{column.name}(val=nil,&block)
        @cells[#{column.name.inspect}] = block ? Benchmark.measure { @times.times(&block) }.real : val
      end
    CLASS
  end
end

Instance Attribute Details

#cellsObject (readonly)

Returns the value of attribute cells.



7
8
9
# File 'lib/rbench/report.rb', line 7

def cells
  @cells
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/rbench/report.rb', line 7

def name
  @name
end

Instance Method Details

#runObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/rbench/report.rb', line 30

def run
  # runs the actual benchmarks. If there is only one column, just evaluate the block itself.
  if @runner.columns.length == 1
    @cells[@runner.columns.first.name] = Benchmark.measure { @times.times(&@block) }.real
  else
    self.instance_eval(&@block)
  end
  # puts its row now that it is complete
  puts to_s
end

#to_sObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rbench/report.rb', line 41

def to_s
  out = "%-#{@runner.desc_width}s" % name
  @runner.columns.each do |column|
    value = @cells[column.name]
    value = @cells.values_at(*value) if value.is_a?(Array)
    value = nil if value.is_a?(Array) && value.nitems != 2
    
    out << column.to_s(value)
  end
  out << @runner.newline
end