Class: Benchmark::IPS::Job::Entry
- Inherits:
-
Object
- Object
- Benchmark::IPS::Job::Entry
- Defined in:
- lib/benchmark/ips/job.rb
Overview
Entries in Benchmark Jobs.
Instance Attribute Summary collapse
-
#action ⇒ String, Proc
readonly
The benchmarking action.
-
#label ⇒ #to_s
readonly
The label of benchmarking action.
Instance Method Summary collapse
-
#call_times(times) ⇒ Integer
Call action by given times, return if @call_loop is present.
-
#compile(str) ⇒ Symbol
Compile code into
call_times
method. -
#initialize(label, action) ⇒ Entry
constructor
Instantiate the Benchmark::IPS::Job::Entry.
-
#label_rjust ⇒ String
Add padding to label’s right if label’s length < 20, Otherwise add a new line and 20 whitespaces.
Constructor Details
#initialize(label, action) ⇒ Entry
Instantiate the Benchmark::IPS::Job::Entry.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/benchmark/ips/job.rb', line 19 def initialize(label, action) @label = label if action.kind_of? String compile action @action = self @as_action = true else unless action.respond_to? :call raise ArgumentError, "invalid action, must respond to #call" end @action = action if action.respond_to? :arity and action.arity > 0 @call_loop = true else @call_loop = false end @as_action = false end end |
Instance Attribute Details
#action ⇒ String, Proc (readonly)
The benchmarking action.
49 50 51 |
# File 'lib/benchmark/ips/job.rb', line 49 def action @action end |
#label ⇒ #to_s (readonly)
The label of benchmarking action.
45 46 47 |
# File 'lib/benchmark/ips/job.rb', line 45 def label @label end |
Instance Method Details
#call_times(times) ⇒ Integer
Call action by given times, return if @call_loop is present.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/benchmark/ips/job.rb', line 66 def call_times(times) return @action.call(times) if @call_loop act = @action i = 0 while i < times act.call i += 1 end end |
#compile(str) ⇒ Symbol
Compile code into call_times
method.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/benchmark/ips/job.rb', line 81 def compile(str) m = (class << self; self; end) code = <<-CODE def call_times(__total); __i = 0 while __i < __total #{str}; __i += 1 end end CODE m.class_eval code end |
#label_rjust ⇒ String
Add padding to label’s right if label’s length < 20, Otherwise add a new line and 20 whitespaces.
54 55 56 57 58 59 60 61 |
# File 'lib/benchmark/ips/job.rb', line 54 def label_rjust label = @label.to_s if label.size > 20 "#{label}\n#{' ' * 20}" else label.rjust(20) end end |