Class: RBench::Summary

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, group, name) ⇒ Summary

Returns a new instance of Summary.



6
7
8
9
10
11
12
# File 'lib/rbench/summary.rb', line 6

def initialize(runner, group, name)
  @runner = runner
  @group  = group
  @name   = name
  @cells  = {}     # A hash with keys as columns, and values being the result
  @items  = []
end

Instance Attribute Details

#cellsObject (readonly)

Returns the value of attribute cells.



3
4
5
# File 'lib/rbench/summary.rb', line 3

def cells
  @cells
end

#itemsObject (readonly)

Returns the value of attribute items.



3
4
5
# File 'lib/rbench/summary.rb', line 3

def items
  @items
end

#linesObject

Returns the value of attribute lines.



4
5
6
# File 'lib/rbench/summary.rb', line 4

def lines
  @lines
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rbench/summary.rb', line 3

def name
  @name
end

#runnerObject (readonly)

Returns the value of attribute runner.



3
4
5
# File 'lib/rbench/summary.rb', line 3

def runner
  @runner
end

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rbench/summary.rb', line 14

def run
  # maybe add convenience-method to group to. group == runner really.
  items = (@group ? @group.items & @runner.reports : @runner.reports)
  
  rows = items.map{|item| item.cells.values_at(*@runner.columns.map{|c|c.name}) }
  rows = rows.pop.zip(*rows)

  @runner.columns.each_with_index do |c,i|
    if c.compare
      value,comparisons = 0,0
      items.each do |item|
        v1,v2 = *item.cells.values_at(*c.compare)
        if v1.kind_of?(Numeric) && v2.kind_of?(Numeric) && v1 != 0 && v2 != 0
          value += v1 / v2
          comparisons += 1
        end
      end
      @cells[c.name] = [value,comparisons] if comparisons > 0
    elsif c.name != :times
      @cells[c.name] = rows[i].compact.select{|r| r.kind_of?(Numeric)}.inject(0){|tot,v| tot += v.to_f }
    end
  end
  
  puts to_s
end

#to_sObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/rbench/summary.rb', line 40

def to_s
  out = ""
  out << @runner.separator(nil,"=") + @runner.newline unless @group
  out << "%-#{@runner.desc_width}s" % name
  @runner.columns.each do |column|
    value = @cells[column.name]
    out << column.to_s( value )
  end
  out << @runner.newline
end