Class: RBench::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(times) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
9
10
# File 'lib/rbench/runner.rb', line 5

def initialize(times)
  @width = 0
  @times = times
  @columns = []
  @items = []
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



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

def columns
  @columns
end

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#timesObject

Returns the value of attribute times.



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

def times
  @times
end

#width(value = nil) ⇒ Object

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#column(name, options = {}) ⇒ Object



42
43
44
# File 'lib/rbench/runner.rb', line 42

def column(name,options={})
  @columns << Column.new(self,name,options)
end

#columns_widthObject



91
92
93
# File 'lib/rbench/runner.rb', line 91

def columns_width
  @columns.inject(0){ |tot,c| tot += (c.to_s.length) }
end

#desc_widthObject



87
88
89
# File 'lib/rbench/runner.rb', line 87

def desc_width
  @desc_width ||= [items.map{|i| (i.items.map{|r| r.name} << i.name) }.flatten.map{|i| i.to_s.length}.max+8,@width-columns_width].max
end

#format(options = {}) ⇒ Object



38
39
40
# File 'lib/rbench/runner.rb', line 38

def format(options={})
  @width = options.delete(:width) || @width
end

#group(name, times = nil, &block) ⇒ Object



46
47
48
# File 'lib/rbench/runner.rb', line 46

def group(name,times=nil,&block)
  @items << Group.new(self,name,times,&block)
end

#groupsObject

convenience-methods



66
67
68
# File 'lib/rbench/runner.rb', line 66

def groups
  @items.select{|item| item.is_a?(Group) }
end

#headerObject



83
84
85
# File 'lib/rbench/runner.rb', line 83

def header
  " " * desc_width + @columns.map {|c| c.to_s }.join + newline
end

#newlineObject

for rendering text. pull out in separate module when to_xml and to_html is in place



79
80
81
# File 'lib/rbench/runner.rb', line 79

def newline
  "\n"
end

#report(name, times = nil, &block) ⇒ Object



50
51
52
53
54
55
# File 'lib/rbench/runner.rb', line 50

def report(name,times=nil,&block)
  # create an anonymous group, or add it to the last open group.
  group(nil) unless @items.last.is_a?(Group) && !@items.last.block
  # now create the report on the last group
  @items.last.report(name,times,&block)
end

#reportsObject



70
71
72
73
# File 'lib/rbench/runner.rb', line 70

def reports
  # we now want _all_ reports, also those that are part of subgroups
  groups.map{|g| g.items.select{|item| item.is_a?(Report) } }.flatten
end

#run(&block) ⇒ Object



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

def run(&block)
  
  # initiate all the columns, groups, reports, and summaries on top level.
  self.instance_eval(&block)
  
  # the groups has not run just yet, but when they do, we really only want
  # to make them initiate their reports, not run them (just yet)
  # when we only have two levels, _every_ report should now be initialized.
  @items.each{|item| item.prepare if item.is_a?(Group)}
  
  # We are putting the summary to the back if its there.
  @items << @items.shift if @items.first.is_a?(Summary)
  
  # if on columns were set, create a default column
  column(:results, :title => "Results") if @columns.empty?
  
  # since we are about to start rendering, we put out the column-header
  puts header
  
  # now we are ready to loop through our items and run!
  items.each { |item| item.run if item.respond_to?(:run) }
  
  # returning self so people can output it in different formats.
  self
end

#separator(title = nil, chr = "-", length = width) ⇒ Object



99
100
101
# File 'lib/rbench/runner.rb', line 99

def separator(title=nil,chr="-",length=width)
  title ? chr*2 + title + chr * (width - title.length - 2) : chr * length
end

#summary(name) ⇒ Object



57
58
59
60
# File 'lib/rbench/runner.rb', line 57

def summary(name)
  # adding the summary to the front, so it is easier to put it last later.
  @items.unshift(Summary.new(self,nil,name)) unless @items.detect{|i| i.is_a?(Summary)}
end

#to_sObject



103
104
105
106
# File 'lib/rbench/runner.rb', line 103

def to_s
  out = " " * desc_width + @columns.map {|c| c.to_s }.join + newline
  out << @items.map {|item| item.to_s}.join
end