Module: Insika::Soak::Report

Defined in:
lib/insika/soak/report.rb

Overview

The mechanical fold: a results file plus its frozen envelope becomes ONE verdict and the numbers behind it. Pure — no clock, no network, no store, no randomness. This is where "pass/fail is mechanical" lives: two people folding the same bytes get the same verdict.

Defined Under Namespace

Classes: Result

Constant Summary collapse

VERDICTS =
%i[pass fail insufficient invalid].freeze

Class Method Summary collapse

Class Method Details

.breaches(envelope, m) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/insika/soak/report.rb', line 360

def breaches(envelope, m)
  out = []
  m[:restart_events].each { |e| out << "restarts: #{e} (max #{envelope[:restarts_max]})" } if m[:restarts] > envelope[:restarts_max]
  out << "error_rate: #{m[:error_rate].round(4)} exceeds #{envelope[:error_rate_ceiling]}" if m[:error_rate] > envelope[:error_rate_ceiling]
  out << "no_usage_rate: #{m[:no_usage_rate].round(4)} exceeds #{envelope[:no_usage_rate_ceiling]} (a turn with no usage called no model)" if m[:no_usage_rate] > envelope[:no_usage_rate_ceiling]

  if m[:rss_growth_ratio] && m[:rss_growth_ratio] > envelope[:rss_growth_ratio]
    slope = m[:rss_slope_mb_per_day] || 0.0
    out << "rss_growth_ratio: #{m[:rss_growth_ratio].round(3)} exceeds #{envelope[:rss_growth_ratio]} " \
           "(fitted slope #{slope} MB/day)"
  end

  if envelope[:rss_ceiling_mb] && m[:rss_peak_mb] && m[:rss_peak_mb] > envelope[:rss_ceiling_mb]
    out << "rss_peak: #{m[:rss_peak_mb]} MB exceeds ceiling #{envelope[:rss_ceiling_mb]} MB"
  end

  if m[:prep_p95_drift_ratio] && m[:prep_p95_drift_ratio] > envelope[:prep_p95_drift_ratio]
    out << "prep_p95_drift_ratio: #{m[:prep_p95_drift_ratio]} exceeds #{envelope[:prep_p95_drift_ratio]} " \
           "(first #{m[:prep_p95_first_ms]} ms -> last #{m[:prep_p95_last_ms]} ms)"
  end

  if envelope[:prep_p95_ceiling_ms] && m[:prep_p95_ms] && m[:prep_p95_ms] > envelope[:prep_p95_ceiling_ms]
    out << "prep_p95: #{m[:prep_p95_ms]} ms exceeds ceiling #{envelope[:prep_p95_ceiling_ms]} ms"
  end

  if envelope[:total_p95_ceiling_ms] && m[:total_p95_ms] && m[:total_p95_ms] > envelope[:total_p95_ceiling_ms]
    out << "total_p95: #{m[:total_p95_ms]} ms exceeds ceiling #{envelope[:total_p95_ceiling_ms]} ms"
  end
  out
end

.compute_metrics(turns, snapshots, gaps, header, started, duration, envelope) ⇒ Object



217
218
219
220
221
222
223
224
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/insika/soak/report.rb', line 217

def compute_metrics(turns, snapshots, gaps, header, started, duration, envelope)
  warmup = envelope[:warmup_hours]
  by_hour = Hash.new { |h, k| h[k] = [] }
  turns.each do |t|
    h = hour_of(t, started)
    by_hour[h] << t if h
  end

  usable = snapshots.select do |s|
    s["vitals"].is_a?(Hash) && !s.dig("vitals", "rss_bytes").nil? && s["error"].nil?
  end

  restarts, restart_events = count_restarts(snapshots)

  rss_fit = trend(usable.select { |s| s["hour"] > warmup }.map { |s| [s["hour"].to_f, s["vitals"]["rss_bytes"].to_f] })
  rss_ratio = rss_fit && rss_fit[:intercept].positive? ? growth_ratio(rss_fit, warmup, usable.map { |s| s["hour"] }.max) : nil

  heap_fit = trend(usable.filter_map { |s|
    live = s.dig("vitals", "gc", "heap_live_slots")
    live && [s["hour"].to_f, live.to_f]
  })
  heap_ratio = heap_fit && heap_fit[:intercept].positive? ? growth_ratio(heap_fit, warmup, usable.map { |s| s["hour"] }.max) : nil

  db_fit = trend(usable.filter_map { |s|
    bytes = s.dig("vitals", "db_bytes", "db")
    bytes && [s["hour"].to_f, bytes.to_f]
  })

  prep = turns.filter_map { |t| t.dig("timing", "prep_ms") }
  totals = turns.filter_map { |t| t["total_ms"] }
  ttfts = turns.filter_map { |t| t.dig("timing", "ttft_ms") }

  # Drift windows: (warmup+1..warmup+6) vs (duration-6..duration-1). A run
  # shorter than ~12h cannot hold two non-overlapping 6h windows: the last
  # window is then empty (drift is reported as "-", never gated) and the
  # first window runs to the end — it never reaches back into the warmup.
  first_lo, first_hi = warmup + 1, warmup + 6
  last_lo, last_hi = duration - 6, duration - 1
  if last_lo <= first_hi
    first_hi = last_hi
    last_lo = last_hi + 1
  end
  first_window = (first_lo..first_hi).flat_map { |h| by_hour[h] }.filter_map { |t| t.dig("timing", "prep_ms") }
  last_window = (last_lo..last_hi).flat_map { |h| by_hour[h] }.filter_map { |t| t.dig("timing", "prep_ms") }
  first_p95 = pct(first_window.sort, 95)
  last_p95 = pct(last_window.sort, 95)

  ok_turns = turns.count { |t| t["ok"] == true }
  no_usage = turns.count { |t| t["ok"] == true && !t["usage"].is_a?(Hash) }

  gap_s = [gaps.map { |g| g["seconds"].to_f }, inter_turn_gaps(turns)].flatten.max

  covered = usable.map { |s| s["hour"] }.uniq.length
  thin = (0...duration).count { |h| by_hour[h].length < envelope[:hourly_turn_floor] }

  {
    turns: turns.length,
    snapshots: snapshots.length,
    restarts: restarts,
    restart_events: restart_events,
    error_rate: turns.empty? ? 0.0 : (turns.length - ok_turns).to_f / turns.length,
    no_usage_rate: ok_turns.zero? ? 0.0 : no_usage.to_f / ok_turns,
    rss_slope_mb_per_day: rss_fit && (rss_fit[:upper_95] * 24 / MB).round(3),
    rss_growth_ratio: rss_ratio,
    rss_peak_mb: usable.empty? ? nil : (usable.map { |s| s["vitals"]["rss_bytes"] }.max / MB).round(1),
    heap_growth_ratio: heap_ratio,
    db_growth_mb_per_day: db_fit && (db_fit[:slope] * 24 / MB).round(3),
    tokens_per_turn: tokens_per_turn(turns),
    prep_p95_first_ms: first_window.empty? ? nil : first_p95,
    prep_p95_last_ms: last_window.empty? ? nil : last_p95,
    prep_p95_drift_ratio: last_window.empty? ? nil : drift_ratio(first_p95, last_p95),
    prep_p95_ms: prep.empty? ? nil : pct(prep.sort, 95),
    total_p95_ms: totals.empty? ? nil : pct(totals.sort, 95).round(1),
    ttft_p95_ms: ttfts.empty? ? nil : pct(ttfts.sort, 95).round(1),
    coverage_ratio: duration.zero? ? 0.0 : covered.to_f / duration,
    max_gap_s: gap_s.nil? ? nil : gap_s.round,
    thin_hours: thin
  }
end

.concurrent_pids(snapshots) ⇒ Object

A boot_id whose snapshots show two DIFFERENT pids at the SAME hour: concurrent workers answering the sampler. A pid CHANGE across hours is a respawn — evidence for the restarts gate, not for this one.



199
200
201
202
203
204
205
206
207
208
# File 'lib/insika/soak/report.rb', line 199

def concurrent_pids(snapshots)
  by_hour = snapshots.each_with_object({}) do |s, acc|
    next unless s["vitals"].is_a?(Hash)

    key = [s["vitals"]["boot_id"], s["hour"]]
    (acc[key] ||= []) << s["vitals"]["pid"]
  end
  by_hour.each { |key, pids| return key[0] if pids.uniq.length > 1 }
  nil
end

.count_restarts(snapshots) ⇒ Object

restarts = distinct boot_id changes + worker respawns (pid changes inside one boot_id), each minus the first. -> [count, [event strings]].



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/insika/soak/report.rb', line 313

def count_restarts(snapshots)
  boots = snapshots.each_with_object({}) do |s, acc|
    next unless s["vitals"].is_a?(Hash)

    (acc[s["vitals"]["boot_id"]] ||= []) << [s["hour"], s["vitals"]["pid"]]
  end
  events = []
  boots.each_value do |samples|
    prev = nil
    samples.sort_by(&:first).each do |hour, pid|
      events << "worker respawned at hour #{hour} (pid #{prev[1]} -> #{pid})" if prev && pid != prev[1]
      prev = [hour, pid]
    end
  end
  first_boot = snapshots.select { |s| s["vitals"].is_a?(Hash) }
                        .min_by { |s| s["hour"] }&.dig("vitals", "boot_id")
  boots.each_key do |boot|
    next if boot == first_boot

    events << "boot_id changed at hour #{boots[boot].map(&:first).min}"
  end
  [events.length, events]
end

.drift_ratio(first, last) ⇒ Object



297
298
299
300
301
302
303
# File 'lib/insika/soak/report.rb', line 297

def drift_ratio(first, last)
  return nil if first.nil? || last.nil?
  return 1.0 if first.zero? && last.zero?
  return Float::INFINITY if first.zero?

  (last / first).round(3)
end

.fold(records, envelope:) ⇒ Object

records is any Enumerable of lines — parsed Hashes AND raw strings (lines that failed to parse travel through unparsed so the fold can count them). The file reader stays separate so the fold is testable on literals.

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
136
137
138
139
140
141
# File 'lib/insika/soak/report.rb', line 80

def fold(records, envelope:)
  raise ArgumentError, "records must be an Enumerable" unless records.respond_to?(:each)

  items = records.to_a
  return insufficient(envelope, nil, "no records", {}) if items.empty?

  parsed = items.select { |l| l.is_a?(Hash) }
  malformed = items.length - parsed.length
  header = parsed.find { |l| l["t"] == "header" }
  return invalid(envelope, nil, "missing header record", {}) if header.nil?

  run_id = header["run_id"]
  header_sha = header["envelope_sha"]
  return invalid(envelope, run_id, "header envelope_sha #{header_sha.inspect} does not match #{envelope.sha.inspect}", {}) if header_sha != envelope.sha

  started = parse_time(header["started_at"])
  duration = envelope[:duration_hours]

  if !envelope.dry_run? && !envelope.calibrated?
    return invalid(envelope, run_id, "the run is not calibrated (write the E1 values into the envelope first)", {})
  end

  if malformed.positive? && malformed.to_f / items.length > 0.005
    return invalid(envelope, run_id, "malformed lines: #{malformed} of #{items.length}", {})
  end

  snapshots = parsed.select { |l| l["t"] == "snapshot" }
  offender = snapshots.find { |s| s["envelope_sha"] != header_sha }
  unless offender.nil?
    return invalid(envelope, run_id,
                   "snapshot at hour #{offender['hour']} was stamped with a different envelope hash", {})
  end

  concurrent = concurrent_pids(snapshots)
  if !concurrent.nil? && envelope[:web_concurrency].to_i == 1
    return invalid(envelope, run_id,
                   "more than one pid inside boot_id #{concurrent.inspect} while web_concurrency == 1 " \
                   "(the RSS series is then not one process)", {})
  end

  turns = parsed.select { |l| l["t"] == "turn" }
  gaps = parsed.select { |l| l["t"] == "gap" }
  end_rec = parsed.find { |l| l["t"] == "end" }
  end_ok = end_rec && end_rec["reason"] == "complete"
  unless end_ok
    reason = end_rec ? end_rec["reason"] : "the runner never wrote an end record"
    return insufficient(envelope, run_id, "the run did not complete (end reason: #{reason})", {})
  end

  metrics = compute_metrics(turns, snapshots, gaps, header, started, duration, envelope)
  missing = missing_coverage(envelope, metrics)
  return insufficient(envelope, run_id, missing, metrics) if missing

  breaches = breaches(envelope, metrics)
  if breaches.empty?
    Result.new(verdict: :pass, reasons: [], metrics: metrics,
               envelope_sha: envelope.sha, run_id: run_id)
  else
    Result.new(verdict: :fail, reasons: breaches, metrics: metrics,
               envelope_sha: envelope.sha, run_id: run_id)
  end
end

.growth_ratio(fit, warmup, last_hour) ⇒ Object



305
306
307
308
309
# File 'lib/insika/soak/report.rb', line 305

def growth_ratio(fit, warmup, last_hour)
  return nil if last_hour.nil? || last_hour <= warmup

  (fit[:intercept] + fit[:upper_95] * last_hour) / (fit[:intercept] + fit[:upper_95] * warmup)
end

.hour_of(turn, started) ⇒ Object



210
211
212
213
214
215
# File 'lib/insika/soak/report.rb', line 210

def hour_of(turn, started)
  at = parse_time(turn["at"])
  return nil if at.nil? || started.nil?

  [((at - started) / 3600).floor, 0].max
end

.insufficient(envelope, run_id, reason, metrics) ⇒ Object



191
192
193
194
# File 'lib/insika/soak/report.rb', line 191

def insufficient(envelope, run_id, reason, metrics)
  Result.new(verdict: :insufficient, reasons: [reason], metrics: metrics,
             envelope_sha: envelope&.sha, run_id: run_id)
end

.inter_turn_gaps(turns) ⇒ Object



337
338
339
340
# File 'lib/insika/soak/report.rb', line 337

def inter_turn_gaps(turns)
  sorted = turns.filter_map { |t| parse_time(t["at"]) }.sort
  sorted.each_cons(2).map { |a, b| b - a }
end

.invalid(envelope, run_id, reason, metrics) ⇒ Object



186
187
188
189
# File 'lib/insika/soak/report.rb', line 186

def invalid(envelope, run_id, reason, metrics)
  Result.new(verdict: :invalid, reasons: [reason], metrics: metrics,
             envelope_sha: envelope&.sha, run_id: run_id)
end

.missing_coverage(envelope, m) ⇒ Object



349
350
351
352
353
354
355
356
357
358
# File 'lib/insika/soak/report.rb', line 349

def missing_coverage(envelope, m)
  return "snapshot coverage: #{m[:coverage_ratio]} below #{envelope[:coverage_min_ratio]}" if m[:coverage_ratio] < envelope[:coverage_min_ratio]
  return "thin hours: #{m[:thin_hours]} hour(s) under #{envelope[:hourly_turn_floor]} turns" if m[:thin_hours].positive?
  if m[:max_gap_s] && m[:max_gap_s] > envelope[:gap_seconds_max]
    return "gap: #{m[:max_gap_s]}s exceeds #{envelope[:gap_seconds_max]}s"
  end
  return "no timing data (INSIKA_TURN_TIMING off on the target)" if m[:prep_p95_ms].nil?

  nil
end

.parse_time(iso) ⇒ Object



180
181
182
183
184
# File 'lib/insika/soak/report.rb', line 180

def parse_time(iso)
  Time.parse(iso.to_s)
rescue ArgumentError
  nil
end

.pct(sorted, p) ⇒ Object

p-th percentile of a sorted array, linear interpolation — the SAME formula as loadtest.rb#pct (including its 1-decimal rounding), so soak and load-test numbers are comparable.



166
167
168
169
170
171
172
173
# File 'lib/insika/soak/report.rb', line 166

def pct(sorted, p)
  return 0.0 if sorted.nil? || sorted.empty?

  r = (p / 100.0) * (sorted.length - 1)
  lo = sorted[r.floor]
  hi = sorted[r.ceil]
  (lo + (hi - lo) * (r - r.floor)).round(1)
end

.tokens_per_turn(turns) ⇒ Object



342
343
344
345
346
347
# File 'lib/insika/soak/report.rb', line 342

def tokens_per_turn(turns)
  totals = turns.filter_map { |t| t.dig("usage", "total_tokens") }
  return nil if totals.empty?

  (totals.sum.to_f / totals.length).round(1)
end

.trend(points) ⇒ Object

Ordinary least squares on [[hour, value]] -> { slope:, intercept:, se:, upper_95: } where upper_95 = slope + 1.645 * se (one-sided). nil below two points.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/insika/soak/report.rb', line 146

def trend(points)
  return nil if points.nil? || points.length < 2

  n = points.length
  xs = points.map { |x, _| x.to_f }
  ys = points.map { |_, y| y.to_f }
  xbar = xs.sum / n
  ybar = ys.sum / n
  sxx = xs.sum { |x| (x - xbar)**2 }
  sxy = xs.zip(ys).sum { |x, y| (x - xbar) * (y - ybar) }
  slope = sxx.zero? ? 0.0 : sxy / sxx
  intercept = ybar - slope * xbar
  sse = xs.zip(ys).sum { |x, y| (y - (intercept + slope * x))**2 }
  se = sxx.zero? || n <= 2 ? 0.0 : Math.sqrt(sse / (n - 2) / sxx)
  { slope: slope, intercept: intercept, se: se, upper_95: slope + 1.645 * se }
end