diff --git a/lib/benchmark_runner.rb b/lib/benchmark_runner.rb index 43c427ff..4894d55f 100644 --- a/lib/benchmark_runner.rb +++ b/lib/benchmark_runner.rb @@ -10,10 +10,13 @@ # Extracted helper methods from run_benchmarks.rb for testing module BenchmarkRunner class << self + # Return the ruby-bench commit like `ruby -v` shows its revision: "2026-11-20 c54da6e016", + # using the committer date in UTC and a 10-character sha. 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? + timestamp, sha = IO.popen(['git', '-C', dir, 'log', '-1', '--format=%ct %H'], err: File::NULL, &:read).split + return 'unknown' unless $?.success? && sha + revision = "#{Time.at(Integer(timestamp)).utc.strftime('%Y-%m-%d')} #{sha[0, 10]}" clean = system('git', '-C', dir, 'diff', '--quiet', 'HEAD', out: File::NULL, err: File::NULL) clean ? revision : "#{revision}-dirty" rescue SystemCallError @@ -65,10 +68,10 @@ def build_output_text(ruby_descriptions, table, format, bench_failures, include_ output_str = +"" + output_str << "ruby-bench: #{ruby_bench_revision}\n" if ruby_bench_revision 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_tables: gc_tables, memory_tables: memory_tables }] diff --git a/test/benchmark_runner_test.rb b/test/benchmark_runner_test.rb index 580a8b92..07260d8d 100644 --- a/test/benchmark_runner_test.rb +++ b/test/benchmark_runner_test.rb @@ -10,23 +10,23 @@ 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" + def git(dir, *args, env: {}) + system(env, '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 + it 'returns the HEAD commit date and short sha, 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') + git(dir, 'commit', '-q', '-m', 'init', env: { 'GIT_COMMITTER_DATE' => '2026-11-20T03:00:00Z' }) 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) + assert_equal "2026-11-20 #{head[0, 10]}", BenchmarkRunner.ruby_bench_revision(dir) File.write(File.join(dir, 'a.rb'), "2\n") - assert_equal "#{head}-dirty", BenchmarkRunner.ruby_bench_revision(dir) + assert_equal "2026-11-20 #{head[0, 10]}-dirty", BenchmarkRunner.ruby_bench_revision(dir) end end @@ -417,6 +417,24 @@ def git(dir, *args) refute_includes result, "p < 0.001" end + it 'prints the ruby-bench revision before the Ruby descriptions' do + ruby_descriptions = { + 'before' => 'ruby 3.3.0', + 'after' => 'ruby 3.4.0' + } + table = [ + ['bench', 'before (ms)', 'stddev (%)', 'after (ms)', 'stddev (%)'], + ['fib', '100.0', '5.0', '50.0', '3.0'] + ] + format = ['%s', '%.1f', '%.1f', '%.1f', '%.1f'] + + result = BenchmarkRunner.build_output_text( + ruby_descriptions, table, format, {}, ruby_bench_revision: '2026-11-20 c54da6e016' + ) + + assert result.start_with?("ruby-bench: 2026-11-20 c54da6e016\nbefore: ruby 3.3.0\nafter: ruby 3.4.0\n\n") + end + it 'includes p-value legend when include_pvalue is true' do ruby_descriptions = { 'ruby-base' => 'ruby 3.3.0',