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
13 changes: 12 additions & 1 deletion lib/benchmark_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,14 +59,15 @@ 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 = +""

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 }]
Expand Down
3 changes: 2 additions & 1 deletion lib/benchmark_runner/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 }

Expand Down
28 changes: 28 additions & 0 deletions test/benchmark_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading