Skip to content

Commit 24f338c

Browse files
Allow optional deletion of the raw json results.
This allows downstream users of ruby-bench to configure where the results.json files are to be stored, so they can use them directly, rather than having them deleted by ruby-bench after processing
1 parent e43471e commit 24f338c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/benchmark_suite.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ def run(ruby:, ruby_description:)
4343
benchmark_entries = discover_benchmarks
4444
cmd_prefix = base_cmd(ruby_description)
4545
env = benchmark_env(ruby)
46+
caller_json_path = ENV["RESULT_JSON_PATH"]
4647

4748
benchmark_entries.each_with_index do |entry, idx|
4849
puts("Running benchmark \"#{entry.name}\" (#{idx+1}/#{benchmark_entries.length})")
4950

50-
result_json_path = File.join(out_path, "temp#{Process.pid}.json")
51+
result_json_path = caller_json_path || File.join(out_path, "temp#{Process.pid}.json")
5152
result = run_single_benchmark(entry.script_path, result_json_path, ruby, cmd_prefix, env)
5253

5354
if result[:success]
54-
bench_data[entry.name] = process_benchmark_result(result_json_path, result[:command])
55+
bench_data[entry.name] = process_benchmark_result(result_json_path, result[:command], delete_file: !caller_json_path)
5556
else
5657
bench_failures[entry.name] = result[:status].exitstatus
5758
end
@@ -74,10 +75,10 @@ def setup_benchmark_directories
7475
end
7576
end
7677

77-
def process_benchmark_result(result_json_path, command)
78+
def process_benchmark_result(result_json_path, command, delete_file: true)
7879
JSON.parse(File.read(result_json_path)).tap do |json|
7980
json["command_line"] = command
80-
File.unlink(result_json_path)
81+
File.unlink(result_json_path) if delete_file
8182
end
8283
end
8384

test/benchmark_suite_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
end
4141

4242
after do
43+
ENV.delete('RESULT_JSON_PATH')
4344
Dir.chdir(@original_dir)
4445
FileUtils.rm_rf(@temp_dir)
4546
end
@@ -424,6 +425,30 @@
424425
assert_empty temp_files
425426
end
426427

428+
it 'uses RESULT_JSON_PATH env var when set and preserves the file' do
429+
custom_json_path = File.join(@out_path, 'custom_result.json')
430+
ENV['RESULT_JSON_PATH'] = custom_json_path
431+
432+
suite = BenchmarkSuite.new(
433+
categories: [],
434+
name_filters: ['simple'],
435+
out_path: @out_path,
436+
harness: 'harness',
437+
no_pinning: true
438+
)
439+
440+
capture_io do
441+
suite.run(ruby: [RbConfig.ruby], ruby_description: 'ruby 3.2.0')
442+
end
443+
444+
assert File.exist?(custom_json_path), "Expected #{custom_json_path} to exist but it was deleted"
445+
result = JSON.parse(File.read(custom_json_path))
446+
assert_includes result, 'bench'
447+
ensure
448+
ENV.delete('RESULT_JSON_PATH')
449+
FileUtils.rm_f(custom_json_path)
450+
end
451+
427452
it 'filters benchmarks by name_filters' do
428453
# Create multiple benchmarks
429454
File.write('benchmarks/bench_a.rb', <<~RUBY)

0 commit comments

Comments
 (0)