Module: Polyrun::SpecQuality::Report
- Defined in:
- lib/polyrun/spec_quality/report.rb
Overview
Human-readable spec quality report from merged JSON.
Class Method Summary collapse
-
.analyze(merged, cfg = {}, plan_shards: nil) ⇒ Object
rubocop:disable Metrics/AbcSize -- merged example analysis.
-
.build_outliers(examples, cfg) ⇒ Object
rubocop:disable Metrics/AbcSize -- outlier row filter.
- .default_cfg(cfg) ⇒ Object
- .emit_warnings!(merged, cfg = {}) ⇒ Object
- .format_churn_section(churn_rows, top) ⇒ Object
- .format_hot_lines_section(hot_lines, top) ⇒ Object
-
.format_outliers_section(outliers, top, profile) ⇒ Object
rubocop:disable Metrics/AbcSize -- outlier text formatting.
- .format_partition_hints_section(hints, top) ⇒ Object
-
.format_report(merged, cfg: {}, top: 30, profile: nil, plan_shards: nil) ⇒ Object
rubocop:enable Metrics/AbcSize.
- .format_shard_summary_section(shard_summary) ⇒ Object
- .format_zero_hit_section(zero_hit, top) ⇒ Object
- .gate_violations(merged, cfg = {}) ⇒ Object
- .partition_hints_for(hot_lines, examples, plan_shards) ⇒ Object
Class Method Details
.analyze(merged, cfg = {}, plan_shards: nil) ⇒ Object
rubocop:disable Metrics/AbcSize -- merged example analysis
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/polyrun/spec_quality/report.rb', line 9 def analyze(merged, cfg = {}, plan_shards: nil) cfg = default_cfg(cfg) examples = merged["examples"] || {} hot_lines = merged["hot_lines"] || {} shard_summary = merged["shard_summary"] || Merge.shard_summary(examples) zero_hit = examples.select { |_loc, row| row["unique_lines"].to_i.zero? } churn = examples.select { |_loc, row| row["line_churn"].to_i >= cfg["min_line_churn"] } .sort_by { |_loc, row| -row["line_churn"].to_i } hot = hot_lines.select { |_line, h| h["example_count"].to_i >= cfg["hot_line_example_overlap"] } .sort_by { |_line, h| [-h["example_count"].to_i, -h["total_hits"].to_i] } outliers = build_outliers(examples, cfg) partition_hints = partition_hints_for(hot, examples, plan_shards) if plan_shards && !plan_shards.empty? { zero_hit: zero_hit, line_churn: churn, hot_lines: hot, outliers: outliers, shard_summary: shard_summary, partition_hints: partition_hints, config: cfg } end |
.build_outliers(examples, cfg) ⇒ Object
rubocop:disable Metrics/AbcSize -- outlier row filter
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/polyrun/spec_quality/report.rb', line 181 def build_outliers(examples, cfg) examples.filter_map do |loc, row| prof = row["profile"] || {} unique = row["unique_lines"].to_i wall = prof["wall"].to_f alloc = prof["gc_allocated"].to_i cpu = prof["cpu_user"].to_f + prof["cpu_system"].to_f sql = row["sql_count"].to_i factories = (row["factory_counts"] || {}).values.sum score = 0 reasons = [] if unique.zero? score += 10 reasons << "zero_lines" end if wall > 1.0 && unique < 3 score += 5 reasons << "slow_low_coverage" end if alloc > 50_000 && wall > 0.5 score += 3 reasons << "high_alloc" end if cpu > 0.5 && unique < 3 score += 3 reasons << "high_cpu_low_coverage" end if sql >= cfg["min_query_count"] score += 4 reasons << "high_sql_count" end if factories >= 10 score += 2 reasons << "many_factories" end next if score.zero? {location: loc, score: score, reasons: reasons, row: row} end.sort_by { |h| -h[:score] } end |
.default_cfg(cfg) ⇒ Object
91 92 93 94 |
# File 'lib/polyrun/spec_quality/report.rb', line 91 def default_cfg(cfg) h = cfg.is_a?(Hash) ? cfg.transform_keys(&:to_s) : {} SpecQuality::Config::DEFAULTS.merge(h) end |
.emit_warnings!(merged, cfg = {}) ⇒ Object
85 86 87 88 89 |
# File 'lib/polyrun/spec_quality/report.rb', line 85 def emit_warnings!(merged, cfg = {}) analyze(merged, cfg).fetch(:line_churn, {}).each do |loc, row| Polyrun::Log.warn "polyrun spec-quality line_churn: #{loc} churn=#{row["line_churn"]}" end end |
.format_churn_section(churn_rows, top) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/polyrun/spec_quality/report.rb', line 167 def format_churn_section(churn_rows, top) lines = ["Per-example line churn (top #{[top, churn_rows.size].min}):"] if churn_rows.empty? lines << " (none)" return lines end churn_rows.first(top).each do |loc, row| lines << format(" %s — churn=%d max_line=%d", loc, row["line_churn"], row["max_line_churn"]) end lines end |
.format_hot_lines_section(hot_lines, top) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/polyrun/spec_quality/report.rb', line 153 def format_hot_lines_section(hot_lines, top) lines = ["Hot lines (shared across examples):"] if hot_lines.empty? lines << " (none)" return lines end hot_lines.first(top).each do |line, h| lines << format(" %s — %d examples, %d cumulative hits", line, h["example_count"], h["total_hits"]) end lines << " …" if hot_lines.size > top lines end |
.format_outliers_section(outliers, top, profile) ⇒ Object
rubocop:disable Metrics/AbcSize -- outlier text formatting
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/polyrun/spec_quality/report.rb', line 225 def format_outliers_section(outliers, top, profile) lines = ["Correlated outliers (slow / empty / heavy):"] if outliers.empty? lines << " (none)" return lines end dims = profile ? profile.to_s.split(",").map(&:strip) : nil outliers.first(top).each do |o| row = o[:row] prof = row["profile"] || {} detail = o[:reasons].join(", ") if dims.nil? || dims.empty? lines << format(" %s — score=%d (%s)", o[:location], o[:score], detail) else prof_bits = dims.filter_map do |d| case d when "wall" then "wall=#{format("%.2f", prof["wall"])}" if prof["wall"] when "cpu" then "cpu=#{format("%.2f", prof["cpu_user"].to_f + prof["cpu_system"].to_f)}" when "mem" then "alloc=#{prof["gc_allocated"]}" if prof["gc_allocated"] when "io" r = prof["io_read_bytes"] w = prof["io_write_bytes"] "io=#{r}/#{w}" if r || w end end lines << format(" %s — score=%d (%s) %s", o[:location], o[:score], detail, prof_bits.join(" ")) end end lines << " …" if outliers.size > top lines end |
.format_partition_hints_section(hints, top) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/polyrun/spec_quality/report.rb', line 115 def format_partition_hints_section(hints, top) return [] if hints.nil? || hints.empty? lines = ["Partition hints (hot lines × shard):"] hints.first(top).each do |h| lines << format(" %s — shard %s (%d examples)", h[:line], h[:shard], h[:example_count]) end lines << " …" if hints.size > top lines end |
.format_report(merged, cfg: {}, top: 30, profile: nil, plan_shards: nil) ⇒ Object
rubocop:enable Metrics/AbcSize
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/polyrun/spec_quality/report.rb', line 36 def format_report(merged, cfg: {}, top: 30, profile: nil, plan_shards: nil) analysis = analyze(merged, cfg, plan_shards: plan_shards) lines = ["Polyrun spec quality report", ""] lines.concat(format_shard_summary_section(analysis[:shard_summary])) lines << "" lines.concat(format_zero_hit_section(analysis[:zero_hit], top)) lines << "" lines.concat(format_hot_lines_section(analysis[:hot_lines], top)) lines << "" lines.concat(format_churn_section(analysis[:line_churn], top)) hints_section = format_partition_hints_section(analysis[:partition_hints], top) unless hints_section.empty? lines << "" lines.concat(hints_section) end lines << "" lines.concat(format_outliers_section(analysis[:outliers], top, profile)) lines.join("\n") + "\n" end |
.format_shard_summary_section(shard_summary) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/polyrun/spec_quality/report.rb', line 96 def format_shard_summary_section(shard_summary) lines = ["Shard attribution:"] if shard_summary.nil? || shard_summary.empty? lines << " (none)" return lines end shard_summary.sort_by { |k, _| k.to_s }.each do |shard, stats| lines << format( " shard %s — examples=%d zero_hit=%d line_churn=%d", shard, stats["examples"], stats["zero_hit"], stats["line_churn"] ) end lines end |
.format_zero_hit_section(zero_hit, top) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/polyrun/spec_quality/report.rb', line 141 def format_zero_hit_section(zero_hit, top) lines = ["Zero production lines (#{zero_hit.size} examples):"] if zero_hit.empty? lines << " (none)" return lines end zero_hit.keys.sort.first(top).each { |loc| lines << " #{loc}" } lines << " …" if zero_hit.size > top lines end |
.gate_violations(merged, cfg = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/polyrun/spec_quality/report.rb', line 58 def gate_violations(merged, cfg = {}) cfg = default_cfg(cfg) analysis = analyze(merged, cfg) violations = [] max_zero = cfg["max_zero_hit_examples"] if max_zero && analysis[:zero_hit].size > max_zero.to_i violations << "zero_hit_examples=#{analysis[:zero_hit].size} max=#{max_zero}" end min_unique = cfg["minimum_unique_lines_per_example"] if min_unique bad = analysis[:zero_hit].keys if bad.size.positive? violations << "examples_below_minimum_unique_lines=#{bad.size} minimum=#{min_unique}" end end max_hot = cfg["max_hot_line_overlap"] if max_hot over = analysis[:hot_lines].count { |_k, h| h["example_count"].to_i > max_hot.to_i } violations << "hot_line_overlap_count=#{over} max=#{max_hot}" if over.positive? end violations end |
.partition_hints_for(hot_lines, examples, plan_shards) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/polyrun/spec_quality/report.rb', line 126 def partition_hints_for(hot_lines, examples, plan_shards) hot_lines.filter_map do |line, h| example_locs = h["examples"] || [] shard_counts = Hash.new(0) example_locs.each do |loc| s = PlanLoader.shard_for_example(loc, plan_shards) || examples.dig(loc, "polyrun_shard_index")&.to_s shard_counts[s] += 1 if s end next if shard_counts.empty? shard, count = shard_counts.max_by { |_s, n| n } {line: line, shard: shard, example_count: count, total_hits: h["total_hits"]} end.sort_by { |h| [-h[:example_count], -h[:total_hits]] } end |