Class: Benchmark::IPS::Job::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/ips/job/entry.rb

Overview

Entries in Benchmark Jobs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, action) ⇒ Entry

Instantiate the Benchmark::IPS::Job::Entry.

Parameters:

  • label (#to_s)

    Label of Benchmarked code.

  • action (String, Proc)

    Code to be benchmarked.

Raises:

  • (ArgumentError)

    Raises when action is not String or not responding to call.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/benchmark/ips/job/entry.rb', line 11

def initialize(label, action)
  @label = label

  # We define #call_times on the singleton class of each Entry instance.
  # That way, there is no polymorphism for `@action.call` inside #call_times.

  if action.kind_of? String
    compile_string action
    @action = self
  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
      compile_block_with_manual_loop
    else
      compile_block
    end
  end
end

Instance Attribute Details

#actionString, Proc (readonly)

The benchmarking action.

Returns:

  • (String, Proc)

    Code to be called, could be String / Proc.


41
42
43
# File 'lib/benchmark/ips/job/entry.rb', line 41

def action
  @action
end

#label#to_s (readonly)

The label of benchmarking action.

Returns:

  • (#to_s)

    Label of action.


37
38
39
# File 'lib/benchmark/ips/job/entry.rb', line 37

def label
  @label
end

Instance Method Details

#call_times(times) ⇒ Integer

Call action by given times.

Parameters:

  • times (Integer)

    Times to call @action.

Returns:

  • (Integer)

    Number of times the @action has been called.


46
47
48
# File 'lib/benchmark/ips/job/entry.rb', line 46

def call_times(times)
  raise '#call_times should be redefined per Benchmark::IPS::Job::Entry instance'
end

#compile_blockObject


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/benchmark/ips/job/entry.rb', line 50

def compile_block
  m = (class << self; self; end)
  code = <<-CODE
    def call_times(times)
      act = @action

      i = 0
      while i < times
        act.call
        i += 1
      end
    end
  CODE
  m.class_eval code
end

#compile_block_with_manual_loopObject


66
67
68
69
70
71
72
73
74
# File 'lib/benchmark/ips/job/entry.rb', line 66

def compile_block_with_manual_loop
  m = (class << self; self; end)
  code = <<-CODE
    def call_times(times)
      @action.call(times)
    end
  CODE
  m.class_eval code
end

#compile_string(str) ⇒ Symbol

Compile code into call_times method.

Parameters:

  • str (String)

    Code to be compiled.

Returns:

  • (Symbol)

    :call_times.


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/benchmark/ips/job/entry.rb', line 79

def compile_string(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