Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/benchmark_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
26 changes: 26 additions & 0 deletions test/benchmark_suite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -40,6 +41,7 @@
end

after do
ENV.delete('RESULT_JSON_PATH')
Dir.chdir(@original_dir)
FileUtils.rm_rf(@temp_dir)
end
Expand Down Expand Up @@ -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)
Expand Down
Loading