Skip to content

Commit b5db6f0

Browse files
committed
Extract method to generate the csv fike
1 parent 2f2d40a commit b5db6f0

3 files changed

Lines changed: 117 additions & 12 deletions

File tree

lib/benchmark_runner.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ def write_json(output_path, ruby_descriptions, bench_data)
2929
out_json_path
3030
end
3131

32+
# Write benchmark results to CSV file
33+
def write_csv(output_path, ruby_descriptions, table)
34+
out_csv_path = "#{output_path}.csv"
35+
output_rows = []
36+
ruby_descriptions.each do |key, value|
37+
output_rows.append([key, value])
38+
end
39+
output_rows.append([])
40+
output_rows.concat(table)
41+
42+
CSV.open(out_csv_path, "wb") do |csv|
43+
output_rows.each do |row|
44+
csv << row
45+
end
46+
end
47+
out_csv_path
48+
end
49+
3250
# Render a graph from JSON benchmark data
3351
def render_graph(json_path)
3452
png_path = json_path.sub(/\.json$/, '.png')

run_benchmarks.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,7 @@
7474
# Save data as CSV so we can produce tables/graphs in a spreasheet program
7575
# NOTE: we don't do any number formatting for the output file because
7676
# we don't want to lose any precision
77-
output_rows = []
78-
ruby_descriptions.each do |key, value|
79-
output_rows.append([key, value])
80-
end
81-
output_rows.append([])
82-
output_rows.concat(table)
83-
out_tbl_path = output_path + ".csv"
84-
CSV.open(out_tbl_path, "wb") do |csv|
85-
output_rows.each do |row|
86-
csv << row
87-
end
88-
end
77+
BenchmarkRunner.write_csv(output_path, ruby_descriptions, table)
8978

9079
# Save the output in a text file that we can easily refer to
9180
output_str = ""

test/benchmark_runner_test.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,104 @@
180180
end
181181
end
182182

183+
describe '.write_csv' do
184+
it 'writes CSV file with metadata and table data' do
185+
Dir.mktmpdir do |dir|
186+
output_path = File.join(dir, 'output_001')
187+
ruby_descriptions = {
188+
'ruby-base' => 'ruby 3.3.0',
189+
'ruby-yjit' => 'ruby 3.3.0 +YJIT'
190+
}
191+
table = [
192+
['Benchmark', 'ruby-base', 'ruby-yjit'],
193+
['fib', '1.5', '1.0'],
194+
['matmul', '2.0', '1.8']
195+
]
196+
197+
result_path = BenchmarkRunner.write_csv(output_path, ruby_descriptions, table)
198+
199+
expected_path = File.join(dir, 'output_001.csv')
200+
assert_equal expected_path, result_path
201+
assert File.exist?(expected_path)
202+
203+
csv_rows = CSV.read(expected_path)
204+
assert_equal 'ruby-base', csv_rows[0][0]
205+
assert_equal 'ruby 3.3.0', csv_rows[0][1]
206+
assert_equal 'ruby-yjit', csv_rows[1][0]
207+
assert_equal 'ruby 3.3.0 +YJIT', csv_rows[1][1]
208+
assert_equal [], csv_rows[2]
209+
assert_equal table, csv_rows[3..5]
210+
end
211+
end
212+
213+
it 'returns the CSV file path' do
214+
Dir.mktmpdir do |dir|
215+
output_path = File.join(dir, 'output_test')
216+
result_path = BenchmarkRunner.write_csv(output_path, {}, [])
217+
218+
assert_equal File.join(dir, 'output_test.csv'), result_path
219+
end
220+
end
221+
222+
it 'handles empty metadata and table' do
223+
Dir.mktmpdir do |dir|
224+
output_path = File.join(dir, 'output_empty')
225+
226+
result_path = BenchmarkRunner.write_csv(output_path, {}, [])
227+
228+
assert File.exist?(result_path)
229+
csv_rows = CSV.read(result_path)
230+
assert_equal [[]], csv_rows
231+
end
232+
end
233+
234+
it 'handles single metadata entry' do
235+
Dir.mktmpdir do |dir|
236+
output_path = File.join(dir, 'output_single')
237+
ruby_descriptions = { 'ruby' => 'ruby 3.3.0' }
238+
table = [['Benchmark', 'Time'], ['test', '1.0']]
239+
240+
result_path = BenchmarkRunner.write_csv(output_path, ruby_descriptions, table)
241+
242+
csv_rows = CSV.read(result_path)
243+
assert_equal 'ruby', csv_rows[0][0]
244+
assert_equal 'ruby 3.3.0', csv_rows[0][1]
245+
assert_equal [], csv_rows[1]
246+
assert_equal table, csv_rows[2..3]
247+
end
248+
end
249+
250+
it 'preserves precision in numeric strings' do
251+
Dir.mktmpdir do |dir|
252+
output_path = File.join(dir, 'output_precision')
253+
table = [['Benchmark', 'Time'], ['test', '1.234567890123']]
254+
255+
result_path = BenchmarkRunner.write_csv(output_path, {}, table)
256+
257+
csv_rows = CSV.read(result_path)
258+
assert_equal '1.234567890123', csv_rows[2][1]
259+
end
260+
end
261+
262+
it 'overwrites existing CSV file' do
263+
Dir.mktmpdir do |dir|
264+
output_path = File.join(dir, 'output_overwrite')
265+
266+
# Write first version
267+
BenchmarkRunner.write_csv(output_path, { 'v1' => 'first' }, [['old']])
268+
269+
# Write second version
270+
new_descriptions = { 'v2' => 'second' }
271+
new_table = [['new']]
272+
result_path = BenchmarkRunner.write_csv(output_path, new_descriptions, new_table)
273+
274+
csv_rows = CSV.read(result_path)
275+
assert_equal 'v2', csv_rows[0][0]
276+
assert_equal 'second', csv_rows[0][1]
277+
end
278+
end
279+
end
280+
183281
describe '.write_json' do
184282
it 'writes JSON file with metadata and raw data' do
185283
Dir.mktmpdir do |dir|

0 commit comments

Comments
 (0)