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

  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.

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

Parameters:

  • times (Integer)

    Times to call @action.

Returns:

  • (Integer)

    Number of times the @action has been called.


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.

Parameters:

  • str (String)

    Code to be compiled.

Returns:

  • (Symbol)

    :call_times.


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 = <<-CODE
    def call_times(__total);
      __i = 0
      while __i < __total
        #{str};
        __i += 1
      end
    end
  CODE
  m.class_eval code
end