|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'fileutils' |
| 4 | +require_relative '../argument_parser' |
| 5 | +require_relative '../cpu_config' |
| 6 | +require_relative '../benchmark_runner' |
| 7 | +require_relative '../benchmark_suite' |
| 8 | +require_relative '../results_table_builder' |
| 9 | + |
| 10 | +module BenchmarkRunner |
| 11 | + class CLI |
| 12 | + attr_reader :args |
| 13 | + |
| 14 | + def self.run(argv = ARGV) |
| 15 | + args = ArgumentParser.parse(argv) |
| 16 | + new(args).run |
| 17 | + end |
| 18 | + |
| 19 | + def initialize(args) |
| 20 | + @args = args |
| 21 | + end |
| 22 | + |
| 23 | + def run |
| 24 | + CPUConfig.configure_for_benchmarking(turbo: args.turbo) |
| 25 | + |
| 26 | + # Create the output directory |
| 27 | + FileUtils.mkdir_p(args.out_path) |
| 28 | + |
| 29 | + ruby_descriptions = {} |
| 30 | + |
| 31 | + # Benchmark with and without YJIT |
| 32 | + bench_start_time = Time.now.to_f |
| 33 | + bench_data = {} |
| 34 | + bench_failures = {} |
| 35 | + args.executables.each do |name, executable| |
| 36 | + ruby_descriptions[name] = `#{executable.shelljoin} -v`.chomp |
| 37 | + |
| 38 | + suite = BenchmarkSuite.new( |
| 39 | + ruby: executable, |
| 40 | + ruby_description: ruby_descriptions[name], |
| 41 | + categories: args.categories, |
| 42 | + name_filters: args.name_filters, |
| 43 | + out_path: args.out_path, |
| 44 | + harness: args.harness, |
| 45 | + pre_init: args.with_pre_init, |
| 46 | + no_pinning: args.no_pinning |
| 47 | + ) |
| 48 | + bench_data[name], failures = suite.run |
| 49 | + # Make it easier to query later. |
| 50 | + bench_failures[name] = failures unless failures.empty? |
| 51 | + end |
| 52 | + |
| 53 | + bench_end_time = Time.now.to_f |
| 54 | + bench_total_time = (bench_end_time - bench_start_time).to_i |
| 55 | + puts("Total time spent benchmarking: #{bench_total_time}s") |
| 56 | + |
| 57 | + if !bench_failures.empty? |
| 58 | + puts("Failed benchmarks: #{bench_failures.map { |k, v| v.size }.sum}") |
| 59 | + end |
| 60 | + |
| 61 | + puts |
| 62 | + |
| 63 | + # Build results table |
| 64 | + builder = ResultsTableBuilder.new( |
| 65 | + executable_names: ruby_descriptions.keys, |
| 66 | + bench_data: bench_data, |
| 67 | + include_rss: args.rss |
| 68 | + ) |
| 69 | + table, format = builder.build |
| 70 | + |
| 71 | + output_path = BenchmarkRunner.output_path(args.out_path, out_override: args.out_override) |
| 72 | + |
| 73 | + # Save the raw data as JSON |
| 74 | + out_json_path = BenchmarkRunner.write_json(output_path, ruby_descriptions, bench_data) |
| 75 | + |
| 76 | + # Save data as CSV so we can produce tables/graphs in a spreasheet program |
| 77 | + # NOTE: we don't do any number formatting for the output file because |
| 78 | + # we don't want to lose any precision |
| 79 | + BenchmarkRunner.write_csv(output_path, ruby_descriptions, table) |
| 80 | + |
| 81 | + # Save the output in a text file that we can easily refer to |
| 82 | + output_str = BenchmarkRunner.build_output_text(ruby_descriptions, table, format, bench_failures) |
| 83 | + out_txt_path = output_path + ".txt" |
| 84 | + File.open(out_txt_path, "w") { |f| f.write output_str } |
| 85 | + |
| 86 | + # Print the table to the console, with numbers truncated |
| 87 | + puts(output_str) |
| 88 | + |
| 89 | + # Print JSON and PNG file names |
| 90 | + puts |
| 91 | + puts "Output:" |
| 92 | + puts out_json_path |
| 93 | + |
| 94 | + if args.graph |
| 95 | + puts BenchmarkRunner.render_graph(out_json_path) |
| 96 | + end |
| 97 | + |
| 98 | + if !bench_failures.empty? |
| 99 | + puts "\nFailed benchmarks:" |
| 100 | + bench_failures.each do |name, data| |
| 101 | + puts " #{name}: #{data.keys.join(", ")}" |
| 102 | + end |
| 103 | + exit(1) |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments