|
3 | 3 | require 'csv' |
4 | 4 | require 'json' |
5 | 5 | require 'rbconfig' |
| 6 | +require_relative 'table_formatter' |
6 | 7 |
|
7 | 8 | # Extracted helper methods from run_benchmarks.rb for testing |
8 | 9 | module BenchmarkRunner |
9 | | - module_function |
| 10 | + class << self |
| 11 | + # Determine output path - either use the override or find a free file number |
| 12 | + def output_path(out_path_dir, out_override: nil) |
| 13 | + if out_override |
| 14 | + out_override |
| 15 | + else |
| 16 | + # If no out path is specified, find a free file index for the output files |
| 17 | + file_no = free_file_no(out_path_dir) |
| 18 | + File.join(out_path_dir, "output_%03d" % file_no) |
| 19 | + end |
| 20 | + end |
10 | 21 |
|
11 | | - # Find the first available file number for output files |
12 | | - def free_file_no(directory) |
13 | | - (1..).each do |file_no| |
14 | | - out_path = File.join(directory, "output_%03d.csv" % file_no) |
15 | | - return file_no unless File.exist?(out_path) |
| 22 | + # Write benchmark data to JSON file |
| 23 | + def write_json(output_path, ruby_descriptions, bench_data) |
| 24 | + out_json_path = "#{output_path}.json" |
| 25 | + out_data = { |
| 26 | + metadata: ruby_descriptions, |
| 27 | + raw_data: bench_data, |
| 28 | + } |
| 29 | + File.write(out_json_path, JSON.generate(out_data)) |
| 30 | + out_json_path |
16 | 31 | end |
17 | | - end |
18 | 32 |
|
19 | | - # Render a graph from JSON benchmark data |
20 | | - def render_graph(json_path) |
21 | | - png_path = json_path.sub(/\.json$/, '.png') |
22 | | - require_relative 'graph_renderer' |
23 | | - GraphRenderer.render(json_path, png_path) |
24 | | - end |
| 33 | + # Write benchmark results to CSV file |
| 34 | + def write_csv(output_path, ruby_descriptions, table) |
| 35 | + out_csv_path = "#{output_path}.csv" |
| 36 | + |
| 37 | + CSV.open(out_csv_path, "wb") do |csv| |
| 38 | + ruby_descriptions.each do |key, value| |
| 39 | + csv << [key, value] |
| 40 | + end |
| 41 | + csv << [] |
| 42 | + table.each do |row| |
| 43 | + csv << row |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + out_csv_path |
| 48 | + end |
| 49 | + |
| 50 | + # Build output text string with metadata, table, and legend |
| 51 | + def build_output_text(ruby_descriptions, table, format, bench_failures) |
| 52 | + base_name, *other_names = ruby_descriptions.keys |
| 53 | + |
| 54 | + output_str = +"" |
| 55 | + |
| 56 | + ruby_descriptions.each do |key, value| |
| 57 | + output_str << "#{key}: #{value}\n" |
| 58 | + end |
25 | 59 |
|
26 | | - # Checked system - error or return info if the command fails |
27 | | - def check_call(command, env: {}, raise_error: true, quiet: false) |
28 | | - puts("+ #{command}") unless quiet |
| 60 | + output_str << "\n" |
| 61 | + output_str << TableFormatter.new(table, format, bench_failures).to_s + "\n" |
29 | 62 |
|
30 | | - result = {} |
| 63 | + unless other_names.empty? |
| 64 | + output_str << "Legend:\n" |
| 65 | + other_names.each do |name| |
| 66 | + output_str << "- #{name} 1st itr: ratio of #{base_name}/#{name} time for the first benchmarking iteration.\n" |
| 67 | + output_str << "- #{base_name}/#{name}: ratio of #{base_name}/#{name} time. Higher is better for #{name}. Above 1 represents a speedup.\n" |
| 68 | + end |
| 69 | + end |
31 | 70 |
|
32 | | - result[:success] = system(env, command) |
33 | | - result[:status] = $? |
| 71 | + output_str |
| 72 | + end |
34 | 73 |
|
35 | | - unless result[:success] |
36 | | - puts "Command #{command.inspect} failed with exit code #{result[:status].exitstatus} in directory #{Dir.pwd}" |
37 | | - raise RuntimeError.new if raise_error |
| 74 | + # Render a graph from JSON benchmark data |
| 75 | + def render_graph(json_path) |
| 76 | + png_path = json_path.sub(/\.json$/, '.png') |
| 77 | + require_relative 'graph_renderer' |
| 78 | + GraphRenderer.render(json_path, png_path) |
38 | 79 | end |
39 | 80 |
|
40 | | - result |
| 81 | + # Checked system - error or return info if the command fails |
| 82 | + def check_call(command, env: {}, raise_error: true, quiet: false) |
| 83 | + puts("+ #{command}") unless quiet |
| 84 | + |
| 85 | + result = {} |
| 86 | + |
| 87 | + result[:success] = system(env, command) |
| 88 | + result[:status] = $? |
| 89 | + |
| 90 | + unless result[:success] |
| 91 | + puts "Command #{command.inspect} failed with exit code #{result[:status].exitstatus} in directory #{Dir.pwd}" |
| 92 | + raise RuntimeError.new if raise_error |
| 93 | + end |
| 94 | + |
| 95 | + result |
| 96 | + end |
| 97 | + |
| 98 | + private |
| 99 | + |
| 100 | + def free_file_no(directory) |
| 101 | + (1..).each do |file_no| |
| 102 | + out_path = File.join(directory, "output_%03d.csv" % file_no) |
| 103 | + return file_no unless File.exist?(out_path) |
| 104 | + end |
| 105 | + end |
41 | 106 | end |
42 | 107 | end |
0 commit comments