Class: Benchmarker::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmarker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title: nil, width: 30, loop: 1, iter: 1, extra: 0, inverse: false, outfile: nil, quiet: false, colorize: nil, sleep: nil, filter: nil) ⇒ Benchmark

Returns a new instance of Benchmark.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/benchmarker.rb', line 39

def initialize(title: nil, width: 30, loop: 1, iter: 1, extra: 0, inverse: false, outfile: nil, quiet: false, colorize: nil, sleep: nil, filter: nil)
  @title    = title
  @width    = width   || 30
  @loop     = loop    || 1
  @iter     = iter    || 1
  @extra    = extra   || 0
  @inverse  = inverse || false
  @outfile  = outfile
  @quiet    = quiet   || false
  @colorize = colorize
  @sleep    = sleep
  @filter   = filter
  if filter
    #; [!0mz0f] error when filter string is invalid format.
    filter =~ /^(task|tag)(!?=+)(.*)/  or
      raise ArgumentError.new("#{filter}: invalid filter.")
    #; [!xo7bq] error when filter operator is invalid.
    $2 == '=' || $2 == '!='  or
      raise ArgumentError.new("#{filter}: expected operator is '=' or '!='.")
  end
  @entries  = []    # [[Task, Resutl]]
  @jdata    = {}
  @hooks    = {}    # {before: Proc, after: Proc, ...}
  @empty_task = nil
end

Instance Attribute Details

#colorizeObject (readonly)

Returns the value of attribute colorize.



65
66
67
# File 'lib/benchmarker.rb', line 65

def colorize
  @colorize
end

#extraObject (readonly)

Returns the value of attribute extra.



65
66
67
# File 'lib/benchmarker.rb', line 65

def extra
  @extra
end

#filterObject (readonly)

Returns the value of attribute filter.



65
66
67
# File 'lib/benchmarker.rb', line 65

def filter
  @filter
end

#inverseObject (readonly)

Returns the value of attribute inverse.



65
66
67
# File 'lib/benchmarker.rb', line 65

def inverse
  @inverse
end

#iterObject (readonly)

Returns the value of attribute iter.



65
66
67
# File 'lib/benchmarker.rb', line 65

def iter
  @iter
end

#loopObject (readonly)

Returns the value of attribute loop.



65
66
67
# File 'lib/benchmarker.rb', line 65

def loop
  @loop
end

#outfileObject (readonly)

Returns the value of attribute outfile.



65
66
67
# File 'lib/benchmarker.rb', line 65

def outfile
  @outfile
end

#quietObject (readonly)

Returns the value of attribute quiet.



65
66
67
# File 'lib/benchmarker.rb', line 65

def quiet
  @quiet
end

#sleepObject (readonly)

Returns the value of attribute sleep.



65
66
67
# File 'lib/benchmarker.rb', line 65

def sleep
  @sleep
end

#titleObject (readonly)

Returns the value of attribute title.



65
66
67
# File 'lib/benchmarker.rb', line 65

def title
  @title
end

#widthObject (readonly)

Returns the value of attribute width.



65
66
67
# File 'lib/benchmarker.rb', line 65

def width
  @width
end

Instance Method Details

#clearObject



67
68
69
70
71
72
# File 'lib/benchmarker.rb', line 67

def clear()
  #; [!phqdn] clears benchmark result and JSON data.
  @entries.each {|_, result| result.clear() }
  @jdata = {}
  self
end

#define_empty_task(code = nil, tag: nil, skip: nil, &block) ⇒ Object

:nodoc:



83
84
85
86
87
88
89
90
# File 'lib/benchmarker.rb', line 83

def define_empty_task(code=nil, tag: nil, skip: nil, &block)   # :nodoc:
  #; [!qzr1s] error when called more than once.
  @empty_task.nil?  or
    raise "cannot define empty task more than once."
  #; [!w66xp] creates empty task.
  @empty_task = TASK.new(nil, code, tag: tag, skip: skip, &block)
  return @empty_task
end

#define_hook(key, &block) ⇒ Object



100
101
102
103
104
# File 'lib/benchmarker.rb', line 100

def define_hook(key, &block)
  #; [!2u53t] register proc object with symbol key.
  @hooks[key] = block
  self
end

#define_task(name, code = nil, tag: nil, skip: nil, &block) ⇒ Object

:nodoc:



92
93
94
95
96
97
98
# File 'lib/benchmarker.rb', line 92

def define_task(name, code=nil, tag: nil, skip: nil, &block)   # :nodoc:
  #; [!re6b8] creates new task.
  #; [!r8o0p] can take a tag.
  task = TASK.new(name, code, tag: tag, skip: skip, &block)
  @entries << [task, Result.new]
  return task
end

#run(warmup: false) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/benchmarker.rb', line 113

def run(warmup: false)
  #; [!0fo0l] runs benchmark tasks and reports result.
  report_environment()
  filter_tasks()
  #; [!2j4ks] calls 'before_all' hook.
  call_hook(:before_all)
  begin
    if warmup
      #; [!6h26u] runs preriminary round when `warmup: true` provided.
      _ignore_output { invoke_tasks() }
      clear()
    end
    invoke_tasks()
  #; [!w1rq7] calls 'after_all' hook even if error raised.
  ensure
    call_hook(:after_all)
  end
  ignore_skipped_tasks()
  report_minmax()
  report_average()
  report_stats()
  write_outfile()
  nil
end

#scope(&block) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/benchmarker.rb', line 74

def scope(&block)
  #; [!wrjy0] creates wrapper object and yields block with it as self.
  #; [!6h24d] passes benchmark object as argument of block.
  scope = Scope.new(self)
  scope.instance_exec(self, &block)
  #; [!y0uwr] returns self.
  self
end