Module: PerformanceHelpers
- Defined in:
- lib/tasks/performance_helpers.rb
Defined Under Namespace
Constant Summary collapse
- CLEAR =
ANSI color codes for terminal output
"\e[0m"- BOLD =
"\e[1m"- DIM =
"\e[2m"- CYAN =
"\e[36m"- GREEN =
"\e[32m"- YELLOW =
"\e[33m"- RED =
"\e[31m"- GRAY =
"\e[90m"- WHITE =
"\e[37m"- MAGENTA =
"\e[35m"
Class Method Summary collapse
-
.clone_base_repo(base, performance_dir, script) ⇒ Object
Clone base branch into a temp dir and return its path.
- .compare_metrics(label, curr, base, threshold) ⇒ Object
- .current_branch ⇒ Object
- .load_into_namespace(module_obj, file_path) ⇒ Object
- .log_new_benchmarks(new_benchmarks) ⇒ Object
- .log_regressions(regressions, threshold) ⇒ Object
- .print_comparison_table(comparison_rows, threshold) ⇒ Object
- .ruby_exec(cmd, env: {}) ⇒ Object
- .run_benchmarks(base_runner, current_runner, threshold, all_base, all_current) ⇒ Object
- .summary_report(current_results, base_results, base, run_time, threshold) ⇒ Object
Class Method Details
.clone_base_repo(base, performance_dir, script) ⇒ Object
Clone base branch into a temp dir and return its path
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/tasks/performance_helpers.rb', line 69 def clone_base_repo(base, performance_dir, script) puts "#{DIM}Cloning base #{base}...#{CLEAR}" safe_ref = base.gsub(/[^0-9A-Za-z._-]/, "-") clone_dir = File.join(performance_dir, "base-#{safe_ref}") FileUtils.rm_rf(clone_dir) repo_url, = ruby_exec("git config --get remote.origin.url") repo_url = repo_url.strip stdout, stderr, status = ruby_exec("git clone --branch #{safe_ref} --single-branch #{repo_url} #{clone_dir}") raise "git clone failed: #{stderr}\n#{stdout}" unless status.success? Dir.chdir(clone_dir) do stdout, stderr, status = ruby_exec("bundle install --quiet") raise "bundle install failed: #{stderr}\n#{stdout}" unless status.success? bench_copy_dir = File.join(clone_dir, "tmp", "performance") FileUtils.mkdir_p(bench_copy_dir) bench_copy = File.join(bench_copy_dir, "benchmark_runner.rb") File.write(bench_copy, File.read(script, encoding: "utf-8")) load_into_namespace(Base, bench_copy) end end |
.compare_metrics(label, curr, base, threshold) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/tasks/performance_helpers.rb', line 146 def compare_metrics(label, curr, base, threshold) # Skip comparison if base result is missing (e.g., new benchmark) unless base return { label: label, base_ips: nil, curr_ips: nil, change: nil, regressed: false } end base_ips = base.fetch(:lower) curr_ips = curr.fetch(:upper) change = (curr_ips - base_ips) / base_ips.to_f { label: label, base_ips: base_ips, curr_ips: curr_ips, change: change, regressed: change < -threshold, } end |
.current_branch ⇒ Object
63 64 65 66 |
# File 'lib/tasks/performance_helpers.rb', line 63 def current_branch stdout, = ruby_exec("git rev-parse --abbrev-ref HEAD") stdout.strip end |
.load_into_namespace(module_obj, file_path) ⇒ Object
54 55 56 57 |
# File 'lib/tasks/performance_helpers.rb', line 54 def load_into_namespace(module_obj, file_path) content = File.read(file_path, encoding: "utf-8") module_obj.module_eval(content, file_path) end |
.log_new_benchmarks(new_benchmarks) ⇒ Object
201 202 203 204 205 206 207 208 209 |
# File 'lib/tasks/performance_helpers.rb', line 201 def log_new_benchmarks(new_benchmarks) return if new_benchmarks.empty? puts puts "#{YELLOW}🆕 New benchmarks (not in base branch):#{CLEAR}" new_benchmarks.each do |label| puts " • #{label}" end end |
.log_regressions(regressions, threshold) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/tasks/performance_helpers.rb', line 211 def log_regressions(regressions, threshold) return if regressions.empty? puts puts "#{RED}⚠️ Performance Regressions Detected#{CLEAR}" puts "#{RED} (< -#{(threshold * 100).round(2)}% IPS)#{CLEAR}" puts regressions.each do |regression| delta = regression[:delta_fraction] base_ips = regression[:base_ips] curr_ips = regression[:curr_ips] delta_str = delta ? format("%+0.2f%%", delta * 100) : "N/A" base_str = base_ips ? format("%.2f", base_ips) : "N/A" curr_str = curr_ips ? format("%.2f", curr_ips) : "N/A" puts " #{BOLD}#{regression[:label]}#{CLEAR}" puts " #{GRAY}base: #{base_str} IPS#{CLEAR}" puts " #{RED}curr: #{curr_str} IPS#{CLEAR}" puts " #{RED}change: #{delta_str}#{CLEAR}" puts end end |
.print_comparison_table(comparison_rows, threshold) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tasks/performance_helpers.rb', line 113 def print_comparison_table(comparison_rows, threshold) rows = comparison_rows.map do |cmp| { benchmark: cmp[:label], base_ips: cmp[:base_ips]&.round(1), curr_ips: cmp[:curr_ips]&.round(1), change: cmp[:change] ? "#{(cmp[:change] * 100).round(1)}%" : "N/A", status: if cmp[:base_ips].nil? "NEW" elsif cmp[:change] < -threshold "REGRESSED" else "OK" end, } end return if rows.empty? table = TableTennis.new(rows, title: "Performance Comparison", theme: :dark, headers: { benchmark: "Benchmark", base_ips: "Base IPS", curr_ips: "Curr IPS", change: "Change", status: "Status", }) table.render puts end |
.ruby_exec(cmd, env: {}) ⇒ Object
59 60 61 |
# File 'lib/tasks/performance_helpers.rb', line 59 def ruby_exec(cmd, env: {}) Open3.capture3(env, cmd) end |
.run_benchmarks(base_runner, current_runner, threshold, all_base, all_current) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tasks/performance_helpers.rb', line 93 def run_benchmarks(base_runner, current_runner, threshold, all_base, all_current) base_results = base_runner.run_benchmarks curr_results = current_runner.run_benchmarks all_base.merge!(base_results) all_current.merge!(curr_results) # Collect comparison results for TableTennis table comparison_rows = [] curr_results.each do |label, result| base_result = base_results[label] cmp = compare_metrics(label, result, base_result, threshold) comparison_rows << cmp end print_comparison_table(comparison_rows, threshold) end |
.summary_report(current_results, base_results, base, run_time, threshold) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/tasks/performance_helpers.rb', line 166 def summary_report(current_results, base_results, base, run_time, threshold) summary = { run_time: run_time, threshold: threshold, branch: current_branch, base: base, regressions: [], new_benchmarks: [], } current_results.each do |label, metrics| base_result = base_results[label] cmp = compare_metrics(label, metrics, base_result, threshold) # Track new benchmarks that don't exist in base if base_result.nil? summary[:new_benchmarks] << label next end next unless cmp[:regressed] summary[:regressions] << { label: label, base_ips: cmp[:base_ips], curr_ips: cmp[:curr_ips], delta_fraction: cmp[:change], } end log_regressions(summary[:regressions], threshold) log_new_benchmarks(summary[:new_benchmarks]) summary end |