|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require 'json' |
| 5 | +require 'fileutils' |
| 6 | +require 'digest' |
| 7 | +require 'benchmark' |
| 8 | +require 'optparse' |
| 9 | + |
| 10 | +PROGRAMS_DIR = File.expand_path('programs', __dir__) |
| 11 | +FIXTURES_DIR = File.expand_path('fixtures', __dir__) |
| 12 | +TMP_DIR = File.expand_path('tmp', __dir__) |
| 13 | +WRITE_REPORT_DEFAULT = 'console' |
| 14 | + |
| 15 | +options = { write_report: WRITE_REPORT_DEFAULT } |
| 16 | +OptionParser.new do |opts| |
| 17 | + opts.banner = 'Usage: ruby run_benchmarks.rb BENCHMARK_DIR [options]' |
| 18 | + opts.on('--write-report=DEST', 'console or path to .json/.svg report') do |dest| |
| 19 | + options[:write_report] = dest |
| 20 | + end |
| 21 | +end.parse! |
| 22 | + |
| 23 | +benchmark_dir = ARGV.shift || abort('Usage: ruby run_benchmarks.rb BENCHMARK_DIR [options]') |
| 24 | +unless Dir.exist?(benchmark_dir) |
| 25 | + abort("Benchmark directory not found: #{benchmark_dir}") |
| 26 | +end |
| 27 | + |
| 28 | +# Collect benchmark names (file basenames without extension) |
| 29 | +benchmarks = Dir.glob(File.join(benchmark_dir, '*.rb')).map { |f| File.basename(f, '.rb') } |
| 30 | +if benchmarks.empty? |
| 31 | + abort("No benchmark files (*.rb) found in directory: #{benchmark_dir}") |
| 32 | +end |
| 33 | + |
| 34 | +# Compare two files for identical content |
| 35 | +def files_identical?(a, b) |
| 36 | + cmp_result = system('cmp', '-s', a, b) |
| 37 | + return $?.success? if !cmp_result.nil? |
| 38 | + File.binread(a) == File.binread(b) |
| 39 | +end |
| 40 | + |
| 41 | +# Run a single benchmark by name |
| 42 | +def run_benchmark(name, benchmark_dir) |
| 43 | + program = File.expand_path(File.join(benchmark_dir, "#{name}.rb")) |
| 44 | + fixture = File.join(FIXTURES_DIR, "#{name}_trace.json") |
| 45 | + output_dir = File.join(TMP_DIR, name) |
| 46 | + |
| 47 | + FileUtils.mkdir_p(output_dir) |
| 48 | + raise 'Reference trace unavailable' unless File.exist?(fixture) |
| 49 | + |
| 50 | + elapsed = Benchmark.realtime do |
| 51 | + system('ruby', File.expand_path('../../gems/pure-ruby-tracer/lib/trace.rb', __dir__), |
| 52 | + '--out-dir', output_dir, |
| 53 | + program) |
| 54 | + raise 'Trace failed' unless $?.success? |
| 55 | + end |
| 56 | + runtime_ms = (elapsed * 1000).round |
| 57 | + output_trace = File.join(output_dir, 'trace.json') |
| 58 | + success = files_identical?(fixture, output_trace) |
| 59 | + size_bytes = File.size(output_trace) |
| 60 | + |
| 61 | + { name: name, runtime_ms: runtime_ms, trace_size: size_bytes, success: success } |
| 62 | +end |
| 63 | + |
| 64 | +# Execute all benchmarks |
| 65 | +results = benchmarks.map { |b| run_benchmark(b, benchmark_dir) } |
| 66 | + |
| 67 | +# Reporting |
| 68 | +if options[:write_report] == 'console' |
| 69 | + # Determine column widths |
| 70 | + name_w = [ 'Benchmark'.length, *results.map { |r| r[:name].length } ].max |
| 71 | + rt_w = [ 'Runtime'.length, *results.map { |r| r[:runtime_ms].to_s.length } ].max |
| 72 | + ts_w = [ 'Trace Size'.length, *results.map { |r| r[:trace_size].to_s.length } ].max |
| 73 | + |
| 74 | + # Header |
| 75 | + printf "%-#{name_w}s %#{rt_w}s %#{ts_w}s %s\n", 'Benchmark', 'Runtime', 'Trace Size', 'Status' |
| 76 | + puts '-' * (name_w + rt_w + ts_w + 10) |
| 77 | + |
| 78 | + # Rows |
| 79 | + results.each do |r| |
| 80 | + status = r[:success] ? 'OK' : 'FAIL' |
| 81 | + printf "%-#{name_w}s %#{rt_w}d ms %#{ts_w}d %s\n", r[:name], r[:runtime_ms], r[:trace_size], status |
| 82 | + end |
| 83 | + |
| 84 | + # Exit with non-zero if any failed |
| 85 | + exit 1 unless results.all? { |r| r[:success] } |
| 86 | +else |
| 87 | + dest = options[:write_report] |
| 88 | + FileUtils.mkdir_p(File.dirname(dest)) |
| 89 | + |
| 90 | + case File.extname(dest) |
| 91 | + when '.json' |
| 92 | + data = results.map { |r| { benchmark: r[:name], runtime_ms: r[:runtime_ms], trace_bytes: r[:trace_size] } } |
| 93 | + File.write(dest, JSON.pretty_generate(data)) |
| 94 | + when '.svg' |
| 95 | + row_height = 25 |
| 96 | + height = 40 + row_height * results.size |
| 97 | + svg = +"<svg xmlns='http://www.w3.org/2000/svg' width='500' height='\#{height}'>\n" |
| 98 | + svg << " <foreignObject width='100%' height='100%'>\n" |
| 99 | + svg << " <style>table{border-collapse:collapse;font-family:sans-serif;}td,th{border:1px solid #999;padding:4px;}</style>\n" |
| 100 | + svg << " <table>\n" |
| 101 | + svg << " <thead><tr><th>Benchmark</th><th>Runtime (ms)</th><th>Trace size (bytes)</th><th>Status</th></tr></thead>\n" |
| 102 | + svg << " <tbody>\n" |
| 103 | + results.each do |r| |
| 104 | + status = r[:success] ? 'OK' : 'FAIL' |
| 105 | + svg << " <tr><td>#{r[:name]}</td><td>#{r[:runtime_ms]}</td><td>#{r[:trace_size]}</td><td>#{status}</td></tr>\n" |
| 106 | + end |
| 107 | + svg << " </tbody>\n" |
| 108 | + svg << " </table>\n" |
| 109 | + svg << " </foreignObject>\n" |
| 110 | + svg << "</svg>\n" |
| 111 | + File.write(dest, svg) |
| 112 | + else |
| 113 | + abort "Unknown report format '\#{dest}'" |
| 114 | + end |
| 115 | + |
| 116 | + # Warn and exit if any failures |
| 117 | + unless results.all? { |r| r[:success] } |
| 118 | + warn 'One or more traces differ from reference!' |
| 119 | + exit 1 |
| 120 | + end |
| 121 | +end |
0 commit comments