|
| 1 | +require 'json' |
| 2 | +require 'fileutils' |
| 3 | +require 'digest' |
| 4 | +require 'benchmark' |
| 5 | + |
| 6 | +USAGE = "Usage: ruby run_benchmark.rb BENCHMARK_NAME" |
| 7 | + |
| 8 | +BENCHMARK = ARGV.shift || abort(USAGE) |
| 9 | +HASHES = { |
| 10 | + 'heavy_work' => '912fc0347cb8a57abd94a7defd76b147f3a79e556745e45207b89529f8a59d8b' |
| 11 | +} |
| 12 | + |
| 13 | +unless HASHES.key?(BENCHMARK) |
| 14 | + abort("Unknown benchmark '#{BENCHMARK}'") |
| 15 | +end |
| 16 | + |
| 17 | +PROGRAM = File.expand_path("programs/#{BENCHMARK}.rb", __dir__) |
| 18 | +FIXTURE = File.expand_path("fixtures/#{BENCHMARK}_trace.json", __dir__) |
| 19 | +TMP_DIR = File.expand_path('tmp', __dir__) |
| 20 | +OUTPUT = File.join(TMP_DIR, "#{BENCHMARK}_trace.json") |
| 21 | +EXPECTED_HASH = HASHES[BENCHMARK] |
| 22 | + |
| 23 | +FileUtils.mkdir_p(TMP_DIR) |
| 24 | + |
| 25 | +unless File.exist?(FIXTURE) && Digest::SHA256.file(FIXTURE).hexdigest == EXPECTED_HASH |
| 26 | + warn "Reference trace missing or corrupt. Attempting to fetch via git lfs..." |
| 27 | + system('git', 'lfs', 'pull', '--include', FIXTURE) |
| 28 | +end |
| 29 | + |
| 30 | +raise 'reference trace unavailable' unless File.exist?(FIXTURE) |
| 31 | +raise 'reference trace hash mismatch' unless Digest::SHA256.file(FIXTURE).hexdigest == EXPECTED_HASH |
| 32 | + |
| 33 | +elapsed = Benchmark.realtime do |
| 34 | + env = { 'CODETRACER_DB_TRACE_PATH' => OUTPUT } |
| 35 | + system(env, 'ruby', File.expand_path('../../src/trace.rb', __dir__), PROGRAM) |
| 36 | + raise 'trace failed' unless $?.success? |
| 37 | +end |
| 38 | +puts "Benchmark runtime: #{(elapsed * 1000).round} ms" |
| 39 | + |
| 40 | +def files_identical?(a, b) |
| 41 | + cmp_result = system('cmp', '-s', a, b) |
| 42 | + return $?.success? if !cmp_result.nil? |
| 43 | + File.binread(a) == File.binread(b) |
| 44 | +end |
| 45 | + |
| 46 | +if files_identical?(FIXTURE, OUTPUT) |
| 47 | + puts 'Trace matches reference.' |
| 48 | +else |
| 49 | + warn 'Trace differs from reference!' |
| 50 | + exit 1 |
| 51 | +end |
0 commit comments