This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrender.rb
224 lines (188 loc) · 4.51 KB
/
render.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
require 'json'
require 'active_support'
lines = Dir['./data/**/*.json'].flat_map do |filename|
IO.readlines(filename)
end
lines.map! do |json|
next if json == "\n"
JSON.parse(json)
end
lines.compact!
benchmarks = lines.group_by do |data|
data['label']
end
DATABASE_COLOURS = {
"Mysql2" => '#e48e00',
"SQLite" => '#d0d0d0',
"PostgreSQL" => '#336791',
}
module CommonHTML
def style
<<-CSS
body {
max-width: 960px;
margin: auto;
}
a {
text-decoration: none;
}
CSS
end
end
class BenchmarkResultHTML < Struct.new(:measurements)
include CommonHTML
def chart_data
{
labels: values_for(:solidus_version),
datasets: values_for('database').map do |database|
{
label: database,
backgroundColor: DATABASE_COLOURS[database],
data: values_for(:solidus_version).map do |version|
mean = get_attr(:mean, solidus_version: version, database: database)
mean && mean * 1000.0
end,
fullData: values_for(:solidus_version).map do |version|
get_measurement(solidus_version: version, database: database)
end
}
end
}
end
def label
measurements.first['label']
end
def values_for(key)
measurements.map{|x| x[key.to_s] }.sort.uniq
end
def get_measurements(attributes)
measurements.select do |measurement|
attributes.all? do |key, value|
measurement[key.to_s] == value
end
end
end
def get_measurement(attributes)
measurements = get_measurements(attributes)
if measurements.length > 1
measurements.each do |m|
p m
end
raise "multiple measurements for #{attributes}: #{measurements.inspect}"
end
measurements[0]
end
def get_attr(attr, attributes)
measurement = get_measurement(attributes)
measurement && measurement[attr.to_s]
end
def description
measurements[0]['description']
end
def notes
measurements[0]['notes']
end
def description_html
"<p>#{description}</p>"
end
def notes_html
return if !notes || notes.empty?
"<h3>Notes</h3><ul>" + notes.map do |note|
"<li>#{note}</li>"
end.join + "</ul>"
end
def html
<<-HTML
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.min.js"></script>
<title>#{label} - Solidus Benchmark</title>
<style>#{style}</style>
</head>
<body>
<a href="/">« all benchmarks</a>
<h1>#{label}</h1>
#{description_html}
<canvas id="chart" width="400" height="200"></canvas>
#{notes_html}
<script>
var ctx = document.getElementById("chart");
var data = #{JSON.dump(chart_data)};
var options = {
scales: {
yAxes: [{
display: true,
ticks: {
beginAtZero: true
},
scaleLabel: {
display: true,
labelString: "milliseconds"
}
}]
},
tooltips: {
callbacks: {
title: function (tooltipItem, data) {
var measurement = data.datasets[tooltipItem[0].datasetIndex].fullData[tooltipItem[0].index];
return ["Solidus " + measurement.solidus_version, measurement.database];
},
label: function (tooltipItem, data) {
var measurement = data.datasets[tooltipItem.datasetIndex].fullData[tooltipItem.index];
return [
"mean: " + (measurement.mean*1000).toFixed(2) + "ms",
"stddev: " + (measurement.stddev*1000).toFixed(2) + "ms",
"",
"iterations: " + measurement.iterations + " in " + (measurement.iterations * measurement.mean).toFixed(2) + "s",
"ruby version: " + measurement.ruby_version,
"rails version: " + measurement.rails_version,
]
}
}
}
};
var myBarChart = new Chart(ctx, {
type: 'bar',
data: data,
options: options
});
</script>
</body>
</html>
HTML
end
end
class BenchmarkIndexHTML < Struct.new(:benchmarks)
include CommonHTML
def listing_html
benchmarks.map do |name, measurements|
%{<li><a href="./#{name}.html">#{name}</a></li>}
end
end
def html
<<-HTML
<html>
<head>
<title>Solidus Benchmark</title>
<style>#{style}</style>
</head>
<body>
<h1>Solidus Benchmarks</h1>
<ul>
#{listing_html.join}
</ul>
</body>
</html>
HTML
end
end
require 'fileutils'
FileUtils.mkdir_p('output')
benchmark = benchmarks.each do |name, measurements|
result = BenchmarkResultHTML.new(measurements)
filename = "output/#{name}.html"
FileUtils.mkdir_p(File.dirname(filename))
File.write(filename, result.html)
end
index = BenchmarkIndexHTML.new(benchmarks)
File.write("output/index.html", index.html)