-
Notifications
You must be signed in to change notification settings - Fork 0
/
bm_report.rb
80 lines (59 loc) · 1.37 KB
/
bm_report.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# ~/lib/ruby/bm_report.rb
require 'tty-table'
# Print a pretty benchmark report
#
# results is an Array of test_case benchmarks
#
# When there 2 test_cases, calculates the
# % change in performance of the first test_case
# over the 2nd (last) test_case.
#
def bm_report(results)
test_cases = results.size
# labels is the first column in the
# report table. it is the names of the
# benchmark attributes.
#
labels = %w[
label
cstime
cutime
stime
utime
real
total
]
headers = ['Label']
data = []
labels.each do |label|
if "label" == label
results.each do |r|
headers << r.send(label)
end
headers << "Calc %" if 2 == test_cases
next
end
row = [label]
results.each do |r|
row << r.send(label).round(5)
end
if 2 == test_cases
row << (results.first.send(label) / results.last.send(label) * 100.0).round(5)
end
data << row
end
table = TTY::Table.new(headers, data)
puts table.render(:unicode, padding: [0, 1, 0, 1]) # Top, Right, Bottom, Left
nil
end
__END__
See Also: ~/lib/ruby/quick.rb
Example Usage:
# benchmarking 3 test cases ...
def bm(how_many=1000)
one = quick(how_many, 'default') { rand }
two = quick(how_many, '10') { rand(10)}
three = quick(how_many, '100') { rand(100) }
[one, two, three]
end
bm_report bm