Skip to content

Commit 6d4478b

Browse files
committed
Extract method to generate the json result
1 parent f8ae3ea commit 6d4478b

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

lib/benchmark_runner.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ def output_path(out_path_dir, out_override: nil)
1818
end
1919
end
2020

21+
# Write benchmark data to JSON file
22+
def write_json(output_path, ruby_descriptions, bench_data)
23+
out_json_path = output_path + ".json"
24+
File.open(out_json_path, "w") do |file|
25+
out_data = {
26+
metadata: ruby_descriptions,
27+
raw_data: bench_data,
28+
}
29+
json_str = JSON.generate(out_data)
30+
file.write json_str
31+
end
32+
out_json_path
33+
end
34+
2135
# Render a graph from JSON benchmark data
2236
def render_graph(json_path)
2337
png_path = json_path.sub(/\.json$/, '.png')

run_benchmarks.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,7 @@
6969
output_path = BenchmarkRunner.output_path(args.out_path, out_override: args.out_override)
7070

7171
# Save the raw data as JSON
72-
out_json_path = output_path + ".json"
73-
File.open(out_json_path, "w") do |file|
74-
out_data = {
75-
metadata: ruby_descriptions,
76-
raw_data: bench_data,
77-
}
78-
json_str = JSON.generate(out_data)
79-
file.write json_str
80-
end
72+
out_json_path = BenchmarkRunner.write_json(output_path, ruby_descriptions, bench_data)
8173

8274
# Save data as CSV so we can produce tables/graphs in a spreasheet program
8375
# NOTE: we don't do any number formatting for the output file because

test/benchmark_runner_test.rb

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

183+
describe '.write_json' do
184+
it 'writes JSON file with metadata and raw 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+
bench_data = {
192+
'ruby-base' => { 'fib' => { 'time' => 1.5 } },
193+
'ruby-yjit' => { 'fib' => { 'time' => 1.0 } }
194+
}
195+
196+
result_path = BenchmarkRunner.write_json(output_path, ruby_descriptions, bench_data)
197+
198+
expected_path = File.join(dir, 'output_001.json')
199+
assert_equal expected_path, result_path
200+
assert File.exist?(expected_path)
201+
202+
json_content = JSON.parse(File.read(expected_path))
203+
assert_equal ruby_descriptions, json_content['metadata']
204+
assert_equal bench_data, json_content['raw_data']
205+
end
206+
end
207+
208+
it 'returns the JSON file path' do
209+
Dir.mktmpdir do |dir|
210+
output_path = File.join(dir, 'output_test')
211+
result_path = BenchmarkRunner.write_json(output_path, {}, {})
212+
213+
assert_equal File.join(dir, 'output_test.json'), result_path
214+
end
215+
end
216+
217+
it 'handles empty metadata and bench data' do
218+
Dir.mktmpdir do |dir|
219+
output_path = File.join(dir, 'output_empty')
220+
221+
result_path = BenchmarkRunner.write_json(output_path, {}, {})
222+
223+
assert File.exist?(result_path)
224+
json_content = JSON.parse(File.read(result_path))
225+
assert_equal({}, json_content['metadata'])
226+
assert_equal({}, json_content['raw_data'])
227+
end
228+
end
229+
230+
it 'handles nested benchmark data structures' do
231+
Dir.mktmpdir do |dir|
232+
output_path = File.join(dir, 'output_nested')
233+
ruby_descriptions = { 'ruby' => 'ruby 3.3.0' }
234+
bench_data = {
235+
'ruby' => {
236+
'benchmark1' => {
237+
'time' => 1.5,
238+
'rss' => 12345,
239+
'iterations' => [1.4, 1.5, 1.6]
240+
}
241+
}
242+
}
243+
244+
result_path = BenchmarkRunner.write_json(output_path, ruby_descriptions, bench_data)
245+
246+
json_content = JSON.parse(File.read(result_path))
247+
assert_equal bench_data, json_content['raw_data']
248+
end
249+
end
250+
251+
it 'overwrites existing JSON file' do
252+
Dir.mktmpdir do |dir|
253+
output_path = File.join(dir, 'output_overwrite')
254+
255+
# Write first version
256+
BenchmarkRunner.write_json(output_path, { 'v' => '1' }, { 'd' => '1' })
257+
258+
# Write second version
259+
new_metadata = { 'v' => '2' }
260+
new_data = { 'd' => '2' }
261+
result_path = BenchmarkRunner.write_json(output_path, new_metadata, new_data)
262+
263+
json_content = JSON.parse(File.read(result_path))
264+
assert_equal new_metadata, json_content['metadata']
265+
assert_equal new_data, json_content['raw_data']
266+
end
267+
end
268+
end
269+
183270
describe '.render_graph' do
184271
it 'delegates to GraphRenderer and returns calculated png_path' do
185272
Dir.mktmpdir do |dir|

0 commit comments

Comments
 (0)