-
Notifications
You must be signed in to change notification settings - Fork 17
/
benchmark.rb
42 lines (33 loc) · 882 Bytes
/
benchmark.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true
require 'benchmark'
require_relative 'reporter'
module Performance
module Reporters
class Benchmark < Performance::Reporters::Reporter
def initialize _ = nil, output = $stdout.dup
super()
@output = output
end
def do_report iterations, params
params.each do |param|
output.print "#{iterations} iterations - #{param}".ljust 40
measure iterations, param do |_|
yield param
end
end
end
private
attr_reader :output
def measure iterations, param
result = nil
require 'benchmark'
measure = ::Benchmark.measure do
iterations.times { result = yield param }
end
output.puts result.to_s.ljust 10
output.print ' ' * 30
output.puts measure
end
end
end
end