From 24f338cf06bf2df030c872c6431c41dee0907699 Mon Sep 17 00:00:00 2001 From: Matt Valentine-House Date: Thu, 4 Dec 2025 13:29:27 +0000 Subject: [PATCH 1/2] 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 --- lib/benchmark_suite.rb | 9 +++++---- test/benchmark_suite_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/benchmark_suite.rb b/lib/benchmark_suite.rb index 30c4a853..68aa48c0 100644 --- a/lib/benchmark_suite.rb +++ b/lib/benchmark_suite.rb @@ -43,15 +43,16 @@ def run(ruby:, ruby_description:) benchmark_entries = discover_benchmarks cmd_prefix = base_cmd(ruby_description) env = benchmark_env(ruby) + caller_json_path = ENV["RESULT_JSON_PATH"] benchmark_entries.each_with_index do |entry, idx| puts("Running benchmark \"#{entry.name}\" (#{idx+1}/#{benchmark_entries.length})") - result_json_path = File.join(out_path, "temp#{Process.pid}.json") + result_json_path = caller_json_path || File.join(out_path, "temp#{Process.pid}.json") result = run_single_benchmark(entry.script_path, result_json_path, ruby, cmd_prefix, env) if result[:success] - bench_data[entry.name] = process_benchmark_result(result_json_path, result[:command]) + bench_data[entry.name] = process_benchmark_result(result_json_path, result[:command], delete_file: !caller_json_path) else bench_failures[entry.name] = result[:status].exitstatus end @@ -74,10 +75,10 @@ def setup_benchmark_directories end end - def process_benchmark_result(result_json_path, command) + def process_benchmark_result(result_json_path, command, delete_file: true) JSON.parse(File.read(result_json_path)).tap do |json| json["command_line"] = command - File.unlink(result_json_path) + File.unlink(result_json_path) if delete_file end end diff --git a/test/benchmark_suite_test.rb b/test/benchmark_suite_test.rb index cd9eee3b..30daf0e8 100644 --- a/test/benchmark_suite_test.rb +++ b/test/benchmark_suite_test.rb @@ -40,6 +40,7 @@ end after do + ENV.delete('RESULT_JSON_PATH') Dir.chdir(@original_dir) FileUtils.rm_rf(@temp_dir) end @@ -424,6 +425,30 @@ assert_empty temp_files end + it 'uses RESULT_JSON_PATH env var when set and preserves the file' do + custom_json_path = File.join(@out_path, 'custom_result.json') + ENV['RESULT_JSON_PATH'] = custom_json_path + + suite = BenchmarkSuite.new( + categories: [], + name_filters: ['simple'], + out_path: @out_path, + harness: 'harness', + no_pinning: true + ) + + capture_io do + suite.run(ruby: [RbConfig.ruby], ruby_description: 'ruby 3.2.0') + end + + assert File.exist?(custom_json_path), "Expected #{custom_json_path} to exist but it was deleted" + result = JSON.parse(File.read(custom_json_path)) + assert_includes result, 'bench' + ensure + ENV.delete('RESULT_JSON_PATH') + FileUtils.rm_f(custom_json_path) + end + it 'filters benchmarks by name_filters' do # Create multiple benchmarks File.write('benchmarks/bench_a.rb', <<~RUBY) From 6b160cc2fe62464f518716c4beaec6f077f917c4 Mon Sep 17 00:00:00 2001 From: Matt Valentine-House Date: Thu, 4 Dec 2025 23:25:08 +0000 Subject: [PATCH 2/2] Make sure we clean up the result path on failure --- lib/benchmark_suite.rb | 1 + test/benchmark_suite_test.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/benchmark_suite.rb b/lib/benchmark_suite.rb index 68aa48c0..93d9bbf9 100644 --- a/lib/benchmark_suite.rb +++ b/lib/benchmark_suite.rb @@ -55,6 +55,7 @@ def run(ruby:, ruby_description:) bench_data[entry.name] = process_benchmark_result(result_json_path, result[:command], delete_file: !caller_json_path) else bench_failures[entry.name] = result[:status].exitstatus + FileUtils.rm_f(result_json_path) unless caller_json_path end end diff --git a/test/benchmark_suite_test.rb b/test/benchmark_suite_test.rb index 30daf0e8..ab9abe15 100644 --- a/test/benchmark_suite_test.rb +++ b/test/benchmark_suite_test.rb @@ -8,6 +8,7 @@ describe BenchmarkSuite do before do + ENV.delete('RESULT_JSON_PATH') @original_dir = Dir.pwd @temp_dir = Dir.mktmpdir Dir.chdir(@temp_dir)