Skip to content

Commit e1e2ae3

Browse files
Merge pull request #452 from ruby/mvh-persist-yjit-results
Allow optional deletion of the raw json results.
2 parents e43471e + 6b160cc commit e1e2ae3

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/benchmark_suite.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,19 @@ 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
58+
FileUtils.rm_f(result_json_path) unless caller_json_path
5759
end
5860
end
5961

@@ -74,10 +76,10 @@ def setup_benchmark_directories
7476
end
7577
end
7678

77-
def process_benchmark_result(result_json_path, command)
79+
def process_benchmark_result(result_json_path, command, delete_file: true)
7880
JSON.parse(File.read(result_json_path)).tap do |json|
7981
json["command_line"] = command
80-
File.unlink(result_json_path)
82+
File.unlink(result_json_path) if delete_file
8183
end
8284
end
8385

test/benchmark_suite_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
describe BenchmarkSuite do
1010
before do
11+
ENV.delete('RESULT_JSON_PATH')
1112
@original_dir = Dir.pwd
1213
@temp_dir = Dir.mktmpdir
1314
Dir.chdir(@temp_dir)
@@ -40,6 +41,7 @@
4041
end
4142

4243
after do
44+
ENV.delete('RESULT_JSON_PATH')
4345
Dir.chdir(@original_dir)
4446
FileUtils.rm_rf(@temp_dir)
4547
end
@@ -424,6 +426,30 @@
424426
assert_empty temp_files
425427
end
426428

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

0 commit comments

Comments
 (0)