Module: Xmi::Performance::Helpers

Defined in:
lib/xmi/performance/helpers.rb

Overview

Shared helpers for the performance benchmark rake tasks. Provides terminal formatting, git/process helpers, and the dual-namespace Runner loader used by the comparison flow.

Defined Under Namespace

Modules: Base, Current, Term

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

Class Method Details

.compare_metrics(label, curr, base, threshold) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/xmi/performance/helpers.rb', line 137

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_branchObject



74
75
76
77
# File 'lib/xmi/performance/helpers.rb', line 74

def current_branch
  stdout, = ruby_exec("git rev-parse --abbrev-ref HEAD")
  stdout.strip
end

.load_runner_into(namespace) ⇒ Object

Clone Xmi::Performance::Runner into the given namespace as ::Runner. Replaces the old module_eval-of-a-top-level-file mechanism, which required require_relative and a top-level class definition incompatible with autoload.



65
66
67
68
# File 'lib/xmi/performance/helpers.rb', line 65

def load_runner_into(namespace)
  Xmi::Performance::Runner # force autoload
  namespace.const_set(:Runner, Xmi::Performance::Runner.clone)
end

.log_new_benchmarks(new_benchmarks) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/xmi/performance/helpers.rb', line 190

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/xmi/performance/helpers.rb', line 200

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


98
99
100
101
102
103
104
105
106
107
108
109
110
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
# File 'lib/xmi/performance/helpers.rb', line 98

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



70
71
72
# File 'lib/xmi/performance/helpers.rb', line 70

def ruby_exec(cmd, env: {})
  Open3.capture3(env, cmd)
end

.run_benchmarks(base_runner, current_runner, threshold, all_base, all_current) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/xmi/performance/helpers.rb', line 79

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)

  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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/xmi/performance/helpers.rb', line 156

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)

    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