From af85c7e8d469b3712592a1eb029a87c71b058d14 Mon Sep 17 00:00:00 2001 From: Matt Valentine-House Date: Thu, 8 Oct 2026 14:55:40 +0100 Subject: [PATCH] Record the Ruby bench version in the output artifacts --- lib/benchmark_runner.rb | 13 ++++++++++++- lib/benchmark_runner/cli.rb | 3 ++- test/benchmark_runner_test.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/benchmark_runner.rb b/lib/benchmark_runner.rb index 6d050092..b02d807c 100644 --- a/lib/benchmark_runner.rb +++ b/lib/benchmark_runner.rb @@ -9,6 +9,16 @@ # Extracted helper methods from run_benchmarks.rb for testing module BenchmarkRunner class << self + def ruby_bench_revision(dir = File.expand_path('..', __dir__)) + revision = IO.popen(['git', '-C', dir, 'rev-parse', 'HEAD'], err: File::NULL, &:read).strip + return 'unknown' unless $?.success? && !revision.empty? + + clean = system('git', '-C', dir, 'diff', '--quiet', 'HEAD', out: File::NULL, err: File::NULL) + clean ? revision : "#{revision}-dirty" + rescue SystemCallError + 'unknown' + end + # Determine output path - either use the override or find a free file number def output_path(out_path_dir, out_override: nil) if out_override @@ -49,7 +59,7 @@ def write_csv(output_path, ruby_descriptions, table) end # Build output text string with metadata, table, and legend - def build_output_text(ruby_descriptions, table, format, bench_failures, include_rss: false, include_gc: false, include_pvalue: false, gc_table: nil, gc_format: nil, sections: nil) + def build_output_text(ruby_descriptions, table, format, bench_failures, include_rss: false, include_gc: false, include_pvalue: false, gc_table: nil, gc_format: nil, sections: nil, ruby_bench_revision: nil) base_name, *other_names = ruby_descriptions.keys output_str = +"" @@ -57,6 +67,7 @@ def build_output_text(ruby_descriptions, table, format, bench_failures, include_ ruby_descriptions.each do |key, value| output_str << "#{key}: #{value}\n" end + output_str << "ruby-bench: #{ruby_bench_revision}\n" if ruby_bench_revision output_str << "\n" sections ||= [{ table: table, format: format, failures: bench_failures, include_gc: include_gc, gc_table: gc_table, gc_format: gc_format }] diff --git a/lib/benchmark_runner/cli.rb b/lib/benchmark_runner/cli.rb index ce0072fb..9fab852d 100644 --- a/lib/benchmark_runner/cli.rb +++ b/lib/benchmark_runner/cli.rb @@ -31,6 +31,7 @@ def run # Create the output directory FileUtils.mkdir_p(args.out_path) + ruby_bench_revision = BenchmarkRunner.ruby_bench_revision ruby_descriptions = {} suite = BenchmarkSuite.new( @@ -136,7 +137,7 @@ def run # Save the output in a text file that we can easily refer to output_sections = build_output_sections(ruby_descriptions.keys, bench_data, bench_harnesses, bench_failures) - output_str = BenchmarkRunner.build_output_text(ruby_descriptions, table, format, bench_failures, include_rss: args.rss, include_gc: builder.include_gc?, include_pvalue: args.pvalue, gc_table: gc_table, gc_format: gc_format, sections: output_sections) + output_str = BenchmarkRunner.build_output_text(ruby_descriptions, table, format, bench_failures, include_rss: args.rss, include_gc: builder.include_gc?, include_pvalue: args.pvalue, gc_table: gc_table, gc_format: gc_format, sections: output_sections, ruby_bench_revision: ruby_bench_revision) out_txt_path = output_path + ".txt" File.open(out_txt_path, "w") { |f| f.write output_str } diff --git a/test/benchmark_runner_test.rb b/test/benchmark_runner_test.rb index 7792d8b5..89fd6610 100644 --- a/test/benchmark_runner_test.rb +++ b/test/benchmark_runner_test.rb @@ -9,6 +9,34 @@ require 'yaml' describe BenchmarkRunner do + describe '.ruby_bench_revision' do + def git(dir, *args) + system('git', '-C', dir, '-c', 'user.name=t', '-c', 'user.email=t@t', *args, out: File::NULL, err: File::NULL) or raise "git #{args.join(' ')} failed" + end + + it 'returns the HEAD commit, with -dirty only when tracked files change' do + Dir.mktmpdir do |dir| + git(dir, 'init', '-q') + File.write(File.join(dir, 'a.rb'), "1\n") + git(dir, 'add', 'a.rb') + git(dir, 'commit', '-q', '-m', 'init') + head = IO.popen(['git', '-C', dir, 'rev-parse', 'HEAD'], &:read).strip + + File.write(File.join(dir, 'untracked.txt'), "x\n") + assert_equal head, BenchmarkRunner.ruby_bench_revision(dir) + + File.write(File.join(dir, 'a.rb'), "2\n") + assert_equal "#{head}-dirty", BenchmarkRunner.ruby_bench_revision(dir) + end + end + + it 'returns unknown outside a git checkout' do + Dir.mktmpdir do |dir| + assert_equal 'unknown', BenchmarkRunner.ruby_bench_revision(dir) + end + end + end + describe '.check_call' do it 'runs a successful command and returns success status' do result = nil