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.

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

  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

#actionString, Proc (readonly)

The benchmarking action.


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.


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, return if @call_loop is present.


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/benchmark/ips/job/entry.rb', line 46

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.


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/benchmark/ips/job/entry.rb', line 61

def compile(str)
  m = (class << self; self; end)
  code = "def call_times(__total);\n__i = 0\nwhile __i < __total\n\#{str};\n__i += 1\nend\nend\n"
  m.class_eval code
end