Class: Baseline::BenchContext
Instance Attribute Summary collapse
-
#bench_count ⇒ Object
readonly
Returns the value of attribute bench_count.
-
#repeat ⇒ Object
readonly
Returns the value of attribute repeat.
-
#subcontext_count ⇒ Object
readonly
Returns the value of attribute subcontext_count.
-
#total_time ⇒ Object
readonly
Returns the value of attribute total_time.
Instance Method Summary collapse
- #after(&block) ⇒ Object
- #before(&block) ⇒ Object
- #bench(name, options = {}, &block) ⇒ Object
- #compare(bench1, bench2) ⇒ Object
- #context(name, options = {}, &block) ⇒ Object
- #exec_bench(name, new_repeat, &block) ⇒ Object
- #exec_hooks(hooks) ⇒ Object
-
#initialize(repeat, nest_level, before, after) ⇒ BenchContext
constructor
A new instance of BenchContext.
- #output_format ⇒ Object
- #rank(*names) ⇒ Object
- #show(bench_data, &block) ⇒ Object
- #time_mode ⇒ Object
- #wrap_with_hooks(options = {}, &block) ⇒ Object
Constructor Details
#initialize(repeat, nest_level, before, after) ⇒ BenchContext
Returns a new instance of BenchContext.
25 26 27 28 29 30 31 32 33 |
# File 'lib/baseline.rb', line 25 def initialize(repeat, nest_level, before, after) @repeat = repeat @total_time = 0 @nest_level = nest_level @before = Array(before).dup @after = Array(after).dup @results = {} @bench_count = @subcontext_count = 0 end |
Instance Attribute Details
#bench_count ⇒ Object (readonly)
Returns the value of attribute bench_count.
22 23 24 |
# File 'lib/baseline.rb', line 22 def bench_count @bench_count end |
#repeat ⇒ Object (readonly)
Returns the value of attribute repeat.
22 23 24 |
# File 'lib/baseline.rb', line 22 def repeat @repeat end |
#subcontext_count ⇒ Object (readonly)
Returns the value of attribute subcontext_count.
22 23 24 |
# File 'lib/baseline.rb', line 22 def subcontext_count @subcontext_count end |
#total_time ⇒ Object (readonly)
Returns the value of attribute total_time.
22 23 24 |
# File 'lib/baseline.rb', line 22 def total_time @total_time end |
Instance Method Details
#after(&block) ⇒ Object
39 40 41 |
# File 'lib/baseline.rb', line 39 def after(&block) @after.unshift(block) end |
#before(&block) ⇒ Object
35 36 37 |
# File 'lib/baseline.rb', line 35 def before(&block) @before.push(block) end |
#bench(name, options = {}, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/baseline.rb', line 90 def bench(name, ={}, &block) # if no block then assume block is provided by show method and # pass along requisite data so show can do its thing if !block_given? [name, [:repeat], :without_own_block] else exec_bench(name, [:repeat], &block) end end |
#compare(bench1, bench2) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/baseline.rb', line 123 def compare(bench1, bench2) benches = [bench1, bench2] winner, loser = benches.sort_by! { |v| @results[v] } time_diff = @results[loser] - @results[winner] time_ratio = @results[loser] / @results[winner].to_f output_format.compare_output(winner, loser, time_diff, time_ratio, @nest_level) end |
#context(name, options = {}, &block) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/baseline.rb', line 101 def context(name, ={}, &block) return output_format.context_skip(name) if [:skip] repeat = [:repeat] || @repeat bc = BenchContext.new(repeat, @nest_level + 1, @before, @after) bench_count = subcontext_count = 0 output_format.context_output_header(name, [:repeat], @repeat, @nest_level) @total_time += time = bc.tap do |v| v.instance_eval(&block) bench_count = v.bench_count subcontext_count = v.subcontext_count end. total_time output_format.(name, time, bench_count, subcontext_count, @nest_level) @results[name] = time @subcontext_count += 1 @total_time end |
#exec_bench(name, new_repeat, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/baseline.rb', line 63 def exec_bench(name, new_repeat, &block) repeat = new_repeat || @repeat bm_block = proc { repeat.times { yield } } time = 0 wrap_with_hooks(:before => @before, :after => @after) do time = Benchmark.measure(&bm_block).send(time_mode) @total_time += time end @results[name] = time @bench_count += 1 [name, time, new_repeat, :with_own_block] end |
#exec_hooks(hooks) ⇒ Object
43 44 45 46 47 |
# File 'lib/baseline.rb', line 43 def exec_hooks(hooks) hooks.each do |b| instance_eval(&b) end end |
#output_format ⇒ Object
59 60 61 |
# File 'lib/baseline.rb', line 59 def output_format Baseline.output_format end |
#rank(*names) ⇒ Object
132 133 134 135 |
# File 'lib/baseline.rb', line 132 def rank(*names) ranking = names.sort_by! { |v| @results[v] } output_format.rank_output(ranking, @nest_level) end |
#show(bench_data, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/baseline.rb', line 79 def show(bench_data, &block) case bench_data.last when :without_own_block name, new_repeat = bench_data show exec_bench(name, new_repeat, &block) when :with_own_block name, time, new_repeat = bench_data output_format.bench_output(name, time, new_repeat, @repeat, @nest_level) end end |
#wrap_with_hooks(options = {}, &block) ⇒ Object
49 50 51 52 53 |
# File 'lib/baseline.rb', line 49 def wrap_with_hooks(={}, &block) exec_hooks([:before]) yield exec_hooks([:after]) end |