|
| 1 | +require_relative '../misc/stats' |
| 2 | +require 'yaml' |
| 3 | + |
| 4 | +class ResultsTableBuilder |
| 5 | + SECONDS_TO_MS = 1000.0 |
| 6 | + BYTES_TO_MIB = 1024.0 * 1024.0 |
| 7 | + |
| 8 | + def initialize(executable_names:, bench_data:, include_rss: false) |
| 9 | + @executable_names = executable_names |
| 10 | + @bench_data = bench_data |
| 11 | + @include_rss = include_rss |
| 12 | + @base_name = executable_names.first |
| 13 | + @other_names = executable_names[1..] |
| 14 | + @bench_names = compute_bench_names |
| 15 | + end |
| 16 | + |
| 17 | + def build |
| 18 | + table = [build_header] |
| 19 | + format = build_format |
| 20 | + |
| 21 | + @bench_names.each do |bench_name| |
| 22 | + next unless has_complete_data?(bench_name) |
| 23 | + |
| 24 | + row = build_row(bench_name) |
| 25 | + table << row |
| 26 | + end |
| 27 | + |
| 28 | + [table, format] |
| 29 | + end |
| 30 | + |
| 31 | + private |
| 32 | + |
| 33 | + def has_complete_data?(bench_name) |
| 34 | + @bench_data.all? { |(_k, v)| v[bench_name] } |
| 35 | + end |
| 36 | + |
| 37 | + def build_header |
| 38 | + header = ["bench"] |
| 39 | + |
| 40 | + @executable_names.each do |name| |
| 41 | + header << "#{name} (ms)" << "stddev (%)" |
| 42 | + header << "RSS (MiB)" if @include_rss |
| 43 | + end |
| 44 | + |
| 45 | + @other_names.each do |name| |
| 46 | + header << "#{name} 1st itr" |
| 47 | + end |
| 48 | + |
| 49 | + @other_names.each do |name| |
| 50 | + header << "#{@base_name}/#{name}" |
| 51 | + end |
| 52 | + |
| 53 | + header |
| 54 | + end |
| 55 | + |
| 56 | + def build_format |
| 57 | + format = ["%s"] |
| 58 | + |
| 59 | + @executable_names.each do |_name| |
| 60 | + format << "%.1f" << "%.1f" |
| 61 | + format << "%.1f" if @include_rss |
| 62 | + end |
| 63 | + |
| 64 | + @other_names.each do |_name| |
| 65 | + format << "%.3f" |
| 66 | + end |
| 67 | + |
| 68 | + @other_names.each do |_name| |
| 69 | + format << "%.3f" |
| 70 | + end |
| 71 | + |
| 72 | + format |
| 73 | + end |
| 74 | + |
| 75 | + def build_row(bench_name) |
| 76 | + t0s = extract_first_iteration_times(bench_name) |
| 77 | + times_no_warmup = extract_benchmark_times(bench_name) |
| 78 | + rsss = extract_rss_values(bench_name) |
| 79 | + |
| 80 | + base_t0, *other_t0s = t0s |
| 81 | + base_t, *other_ts = times_no_warmup |
| 82 | + base_rss, *other_rsss = rsss |
| 83 | + |
| 84 | + row = [bench_name] |
| 85 | + build_base_columns(row, base_t, base_rss) |
| 86 | + build_comparison_columns(row, other_ts, other_rsss) |
| 87 | + build_ratio_columns(row, base_t0, other_t0s, base_t, other_ts) |
| 88 | + |
| 89 | + row |
| 90 | + end |
| 91 | + |
| 92 | + def build_base_columns(row, base_t, base_rss) |
| 93 | + row << mean(base_t) |
| 94 | + row << stddev_percent(base_t) |
| 95 | + row << base_rss if @include_rss |
| 96 | + end |
| 97 | + |
| 98 | + def build_comparison_columns(row, other_ts, other_rsss) |
| 99 | + other_ts.zip(other_rsss).each do |other_t, other_rss| |
| 100 | + row << mean(other_t) |
| 101 | + row << stddev_percent(other_t) |
| 102 | + row << other_rss if @include_rss |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + def build_ratio_columns(row, base_t0, other_t0s, base_t, other_ts) |
| 107 | + ratio_1sts = other_t0s.map { |other_t0| base_t0 / other_t0 } |
| 108 | + ratios = other_ts.map { |other_t| mean(base_t) / mean(other_t) } |
| 109 | + row.concat(ratio_1sts) |
| 110 | + row.concat(ratios) |
| 111 | + end |
| 112 | + |
| 113 | + def extract_first_iteration_times(bench_name) |
| 114 | + @executable_names.map do |name| |
| 115 | + data = bench_data_for(name, bench_name) |
| 116 | + (data['warmup'][0] || data['bench'][0]) * SECONDS_TO_MS |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + def extract_benchmark_times(bench_name) |
| 121 | + @executable_names.map do |name| |
| 122 | + bench_data_for(name, bench_name)['bench'].map { |v| v * SECONDS_TO_MS } |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + def extract_rss_values(bench_name) |
| 127 | + @executable_names.map do |name| |
| 128 | + bench_data_for(name, bench_name)['rss'] / BYTES_TO_MIB |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + def bench_data_for(name, bench_name) |
| 133 | + @bench_data[name][bench_name] |
| 134 | + end |
| 135 | + |
| 136 | + def mean(values) |
| 137 | + Stats.new(values).mean |
| 138 | + end |
| 139 | + |
| 140 | + def stddev(values) |
| 141 | + Stats.new(values).stddev |
| 142 | + end |
| 143 | + |
| 144 | + def stddev_percent(values) |
| 145 | + 100 * stddev(values) / mean(values) |
| 146 | + end |
| 147 | + |
| 148 | + def compute_bench_names |
| 149 | + benchmarks_metadata = YAML.load_file('benchmarks.yml') |
| 150 | + sort_benchmarks(all_benchmark_names, benchmarks_metadata) |
| 151 | + end |
| 152 | + |
| 153 | + def all_benchmark_names |
| 154 | + @bench_data.values.flat_map(&:keys).uniq |
| 155 | + end |
| 156 | + |
| 157 | + # Sort benchmarks with headlines first, then others, then micro |
| 158 | + def sort_benchmarks(bench_names, metadata) |
| 159 | + bench_names.sort_by { |name| [category_priority(name, metadata), name] } |
| 160 | + end |
| 161 | + |
| 162 | + def category_priority(bench_name, metadata) |
| 163 | + category = metadata.dig(bench_name, 'category') || 'other' |
| 164 | + case category |
| 165 | + when 'headline' then 0 |
| 166 | + when 'micro' then 2 |
| 167 | + else 1 |
| 168 | + end |
| 169 | + end |
| 170 | +end |
0 commit comments