/
output_to_html.rb
64 lines (59 loc) · 1.63 KB
/
output_to_html.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
require 'erb'
module OutputToHTML
TEMPLATE_HEADER = <<"EOT".freeze
<div>
All times are rounded to the nearest thousandth for display purposes. Speedups next to each time are computed
before any rounding occurs. Also, all speedup calculations are computed by comparing a given time against
the very first column (which is always the default ActiveRecord::Base.create method.
</div>
EOT
TEMPLATE = <<"EOT".freeze
<style>
td#benchmarkTitle {
border: 1px solid black;
padding: 2px;
font-size: 0.8em;
background-color: black;
color: white;
}
td#benchmarkCell {
border: 1px solid black;
padding: 2px;
font-size: 0.8em;
}
</style>
<table>
<tr>
<% columns.each do |col| %>
<td id="benchmarkTitle"><%= col %></td>
<% end %>
</tr>
<tr>
<% times.each do |time| %>
<td id="benchmarkCell"><%= time %></td>
<% end %>
</tr>
<tr><td> </td></tr>
</table>
EOT
def self.output_results( filename, results )
html = ''
results.each do |result_set|
columns = []
times = []
result_set.each do |result|
columns << result.description
if result.failed
times << "failed"
else
time = result.tms.real.round_to( 3 )
speedup = ( result_set.first.tms.real / result.tms.real ).round
times << (result == result_set.first ? time.to_s : "#{time} (#{speedup}x speedup)")
end
end
template = ERB.new( TEMPLATE, 0, "%<>")
html << template.result( binding )
end
File.open( filename, 'w' ) { |file| file.write( TEMPLATE_HEADER + html ) }
end
end