Class: Benchmark

Inherits:
Object
  • Object
show all
Includes:
Abacus, Report
Defined in:
lib/benchmark_color.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Report

arrow, format, output, seperator, small_arrow

Methods included from Abacus

block_time, measure

Constructor Details

#initialize(args = {}) ⇒ Benchmark

Returns a new instance of Benchmark.



71
72
73
74
75
76
# File 'lib/benchmark_color.rb', line 71

def initialize args={}
  @winner = args.fetch(:winner, :green)
  @loser = args.fetch(:loser, :red)
  @option = args.fetch(:option, :light_blue)
  @block_hash ||= {}
end

Instance Attribute Details

#block_hashObject (readonly)

Returns the value of attribute block_hash.



65
66
67
# File 'lib/benchmark_color.rb', line 65

def block_hash
  @block_hash
end

#loserObject

Returns the value of attribute loser.



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

def loser
  @loser
end

#optionObject

Returns the value of attribute option.



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

def option
  @option
end

#winnerObject

Returns the value of attribute winner.



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

def winner
  @winner
end

Instance Method Details

#compareObject



89
90
91
# File 'lib/benchmark_color.rb', line 89

def compare
  Report.output @block_hash, winner, loser, option
end

#instructionsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/benchmark_color.rb', line 93

def instructions
  %{You must supply a block of code like this:

   b = Benchmark.new
   b.measure("some-label:") { n.times do; x = 1 + 3; end}
   b.compare #to get your results

   Or this:

   n = 1000
   b = Benchmark.new
   b.measure("some-label:") do
     n.times do 
       x = 1 + 3
     end
   end
   b.compare #to get your results}
end

#measure(label = "") ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/benchmark_color.rb', line 78

def measure label = ""
  if block_given?
    block = Abacus.block_time {yield}
    @block_hash[label] = block
    true
  else
    puts instructions
    false
  end
end