diff --git a/lib/benchmark_suite.rb b/lib/benchmark_suite.rb index 30c4a853..93d9bbf9 100644 --- a/lib/benchmark_suite.rb +++ b/lib/benchmark_suite.rb @@ -43,17 +43,19 @@ 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 + FileUtils.rm_f(result_json_path) unless caller_json_path end end @@ -74,10 +76,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..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) @@ -40,6 +41,7 @@ end after do + ENV.delete('RESULT_JSON_PATH') Dir.chdir(@original_dir) FileUtils.rm_rf(@temp_dir) end @@ -424,6 +426,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)