forked from ruby/ruby-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresults_table_builder.rb
More file actions
361 lines (300 loc) · 8.76 KB
/
results_table_builder.rb
File metadata and controls
361 lines (300 loc) · 8.76 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
require_relative '../misc/stats'
require 'yaml'
class ResultsTableBuilder
SECONDS_TO_MS = 1000.0
BYTES_TO_MIB = 1024.0 * 1024.0
def initialize(executable_names:, bench_data:, include_rss: false, include_pvalue: false, include_zjit_mem: false)
@executable_names = executable_names
@bench_data = bench_data
@include_rss = include_rss
@include_pvalue = include_pvalue
@include_zjit_mem = include_zjit_mem
@include_gc = detect_gc_data(bench_data)
@base_name = executable_names.first
@other_names = executable_names[1..]
@bench_names = compute_bench_names
end
def include_gc?
@include_gc
end
def build
table = [build_header]
format = build_format
@bench_names.each do |bench_name|
next unless has_complete_data?(bench_name)
row = build_row(bench_name)
table << row
end
[table, format]
end
private
def has_complete_data?(bench_name)
@bench_data.all? { |(_k, v)| v[bench_name] }
end
def build_header
header = ["bench"]
@executable_names.each do |name|
header << "#{name} (ms)"
header << "RSS (MiB)" if @include_rss
if @include_zjit_mem
header << "code (B)"
header << "alloc (B)"
end
if @include_gc
header << "#{name} mark (ms)"
header << "#{name} sweep (ms)"
end
end
@other_names.each do |name|
header << "#{name} 1st itr"
end
@other_names.each do |name|
header << "#{@base_name}/#{name}"
if @include_pvalue
header << "p-value" << "sig"
end
end
if @include_rss
@other_names.each do |name|
header << "RSS #{@base_name}/#{name}"
end
end
if @include_gc
@other_names.each do |name|
header << "mark #{@base_name}/#{name}"
header << "sweep #{@base_name}/#{name}"
end
end
header
end
def build_format
format = ["%s"]
@executable_names.each do |_name|
format << "%s"
format << "%.1f" if @include_rss
if @include_zjit_mem
format << "%s"
format << "%s"
end
if @include_gc
format << "%s"
format << "%s"
end
end
@other_names.each do |_name|
format << "%.3f"
end
@other_names.each do |_name|
format << "%s"
if @include_pvalue
format << "%s" << "%s"
end
end
if @include_rss
@other_names.each do |_name|
format << "%.3f"
end
end
if @include_gc
@other_names.each do |_name|
format << "%s"
format << "%s"
end
end
format
end
def build_row(bench_name)
t0s = extract_first_iteration_times(bench_name)
times_no_warmup = extract_benchmark_times(bench_name)
rsss = extract_rss_values(bench_name)
base_t0, *other_t0s = t0s
base_t, *other_ts = times_no_warmup
base_rss, *other_rsss = rsss
if @include_zjit_mem
code_regions = extract_zjit_stat(bench_name, 'code_region_bytes')
zjit_allocs = extract_zjit_stat(bench_name, 'zjit_alloc_bytes')
base_code, *other_codes = code_regions
base_alloc, *other_allocs = zjit_allocs
end
if @include_gc
marking_times = extract_gc_times(bench_name, 'gc_marking_time_bench')
sweeping_times = extract_gc_times(bench_name, 'gc_sweeping_time_bench')
base_mark, *other_marks = marking_times
base_sweep, *other_sweeps = sweeping_times
end
row = [bench_name]
build_base_columns(row, base_t, base_rss, base_code, base_alloc, base_mark, base_sweep)
build_comparison_columns(row, other_ts, other_rsss, other_codes, other_allocs, other_marks, other_sweeps)
build_ratio_columns(row, base_t0, other_t0s, base_t, other_ts)
build_rss_ratio_columns(row, base_rss, other_rsss)
build_gc_ratio_columns(row, base_mark, other_marks, base_sweep, other_sweeps)
row
end
def build_base_columns(row, base_t, base_rss, base_code, base_alloc, base_mark, base_sweep)
row << format_time_with_stddev(base_t)
row << base_rss if @include_rss
if @include_zjit_mem
row << format_bytes(base_code)
row << format_bytes(base_alloc)
end
if @include_gc
row << format_time_with_stddev(base_mark)
row << format_time_with_stddev(base_sweep)
end
end
def build_comparison_columns(row, other_ts, other_rsss, other_codes, other_allocs, other_marks, other_sweeps)
other_ts.each_with_index do |other_t, i|
row << format_time_with_stddev(other_t)
row << other_rsss[i] if @include_rss
if @include_zjit_mem
row << format_bytes(other_codes[i])
row << format_bytes(other_allocs[i])
end
if @include_gc
row << format_time_with_stddev(other_marks[i])
row << format_time_with_stddev(other_sweeps[i])
end
end
end
def format_bytes(value)
return "N/A" if value.nil?
value.to_i.to_s.gsub(/(\d)(?=(\d{3})+(?!\d))/, '\1,')
end
def format_time_with_stddev(values)
return "N/A" if values.nil? || values.empty?
"%.1f ± %.1f%%" % [mean(values), stddev_percent(values)]
end
def build_ratio_columns(row, base_t0, other_t0s, base_t, other_ts)
ratio_1sts = other_t0s.map { |other_t0| base_t0 / other_t0 }
row.concat(ratio_1sts)
other_ts.each do |other_t|
pval = @include_pvalue ? Stats.welch_p_value(base_t, other_t) : nil
row << format_ratio(mean(base_t) / mean(other_t), pval)
if @include_pvalue
row << format_p_value(pval)
row << significance_level(pval)
end
end
end
def build_rss_ratio_columns(row, base_rss, other_rsss)
return unless @include_rss
other_rsss.each do |other_rss|
row << base_rss / other_rss
end
end
def build_gc_ratio_columns(row, base_mark, other_marks, base_sweep, other_sweeps)
return unless @include_gc
(other_marks || []).each do |other_mark|
row << gc_ratio(base_mark, other_mark)
end
(other_sweeps || []).each do |other_sweep|
row << gc_ratio(base_sweep, other_sweep)
end
end
def gc_ratio(base, other)
if base.nil? || base.empty? || other.nil? || other.empty? ||
mean(other) == 0.0
return "N/A"
end
pval = @include_pvalue ? Stats.welch_p_value(base, other) : nil
format_ratio(mean(base) / mean(other), pval)
end
def format_ratio(ratio, pval)
sym = significance_symbol(pval)
formatted = "%.3f" % ratio
suffix = sym.empty? ? "" : " (#{sym})"
(formatted + suffix).ljust(formatted.length + 6)
end
def format_p_value(pval)
return "N/A" if pval.nil?
if pval >= 0.001
"%.3f" % pval
else
"%.1e" % pval
end
end
def significance_symbol(pval)
return "" if pval.nil?
if pval < 0.001
"***"
elsif pval < 0.01
"**"
elsif pval < 0.05
"*"
else
""
end
end
def significance_level(pval)
return "" if pval.nil?
if pval < 0.001
"p < 0.001"
elsif pval < 0.01
"p < 0.01"
elsif pval < 0.05
"p < 0.05"
else
""
end
end
def extract_first_iteration_times(bench_name)
@executable_names.map do |name|
data = bench_data_for(name, bench_name)
(data['warmup'][0] || data['bench'][0]) * SECONDS_TO_MS
end
end
def extract_benchmark_times(bench_name)
@executable_names.map do |name|
bench_data_for(name, bench_name)['bench'].map { |v| v * SECONDS_TO_MS }
end
end
def extract_rss_values(bench_name)
@executable_names.map do |name|
bench_data_for(name, bench_name)['rss'] / BYTES_TO_MIB
end
end
def extract_zjit_stat(bench_name, key)
@executable_names.map do |name|
bench_data_for(name, bench_name).dig('zjit_stats', key)
end
end
def extract_gc_times(bench_name, key)
@executable_names.map do |name|
bench_data_for(name, bench_name)[key] || []
end
end
def detect_gc_data(bench_data)
bench_data.values.any? { |benchmarks| benchmarks.values.any? { |d| d.is_a?(Hash) && d.key?('gc_marking_time_bench') } }
end
def bench_data_for(name, bench_name)
@bench_data[name][bench_name]
end
def mean(values)
Stats.new(values).mean
end
def stddev(values)
Stats.new(values).stddev
end
def stddev_percent(values)
100 * stddev(values) / mean(values)
end
def compute_bench_names
benchmarks_metadata = YAML.load_file('benchmarks.yml')
sort_benchmarks(all_benchmark_names, benchmarks_metadata)
end
def all_benchmark_names
@bench_data.values.flat_map(&:keys).uniq
end
# Sort benchmarks with headlines first, then others, then micro
def sort_benchmarks(bench_names, metadata)
bench_names.sort_by { |name| [category_priority(name, metadata), name] }
end
def category_priority(bench_name, metadata)
category = metadata.dig(bench_name, 'category') || 'other'
case category
when 'headline' then 0
when 'micro' then 2
else 1
end
end
end