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"- 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tasks/performance_helpers.rb', line 67 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, "lib", "tasks") FileUtils.mkdir_p(bench_copy_dir) bench_copy = File.join(bench_copy_dir, "benchmark_runner.rb") File.write(bench_copy, File.read(script)) load_into_namespace(Base, bench_copy) end end |
.compare_metrics(label, curr, base, threshold) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/tasks/performance_helpers.rb', line 150 def compare_metrics(label, curr, base, threshold) 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
61 62 63 64 |
# File 'lib/tasks/performance_helpers.rb', line 61 def current_branch stdout, = ruby_exec("git rev-parse --abbrev-ref HEAD") stdout.strip end |
.load_into_namespace(module_obj, file_path) ⇒ Object
52 53 54 55 |
# File 'lib/tasks/performance_helpers.rb', line 52 def load_into_namespace(module_obj, file_path) content = File.read(file_path) module_obj.module_eval(content, file_path) end |
.log_new_benchmarks(new_benchmarks) ⇒ Object
204 205 206 207 208 209 210 211 212 |
# File 'lib/tasks/performance_helpers.rb', line 204 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
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/tasks/performance_helpers.rb', line 214 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
111 112 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 145 146 147 148 |
# File 'lib/tasks/performance_helpers.rb', line 111 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? puts " #{'Benchmark'.ljust(40)} #{'Base IPS'.rjust(12)} #{'Curr IPS'.rjust(12)} #{'Change'.rjust(10)} #{'Status'.rjust(10)}" puts " #{DIM}#{'─' * 86}#{CLEAR}" rows.each do |row| status_color = case row[:status] when "REGRESSED" then RED when "NEW" then YELLOW else GREEN end row[:status] == "REGRESSED" ? RED : DIM puts " #{row[:benchmark].ljust(40)} #{format('%-12.1f', row[:base_ips] || 0)} #{format('%-12.1f', row[:curr_ips] || 0)} #{format('%-10s', row[:change]).gsub('%', '%%')} #{status_color}#{row[:status].rjust(10)}#{CLEAR}" end puts end |
.ruby_exec(cmd, env: {}) ⇒ Object
57 58 59 |
# File 'lib/tasks/performance_helpers.rb', line 57 def ruby_exec(cmd, env: {}) Open3.capture3(env, cmd) end |
.run_benchmarks(base_runner, current_runner, threshold, all_base, all_current) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/tasks/performance_helpers.rb', line 91 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 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
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 200 201 202 |
# File 'lib/tasks/performance_helpers.rb', line 169 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 |