Module: Polyrun::Timing::Stats

Defined in:
lib/polyrun/timing/stats.rb

Overview

Normalizes scalar or object timing entries for merge and binpack weight lookup.

Constant Summary collapse

STAT_KEYS =
%w[last_seconds min max mean p95 runs failures timeouts].freeze

Class Method Summary collapse

Class Method Details

.binpack_weight(entry) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



46
47
48
49
# File 'lib/polyrun/timing/stats.rb', line 46

def binpack_weight(entry)
  h = normalize_entry(entry)
  h["last_seconds"].positive? ? h["last_seconds"] : h["mean"]
end

.merge_entries(a, b) ⇒ Object

rubocop:disable Metrics/AbcSize -- weighted mean merge



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/polyrun/timing/stats.rb', line 52

def merge_entries(a, b)
  ha = normalize_entry(a)
  hb = normalize_entry(b)
  runs = ha["runs"] + hb["runs"]
  mean =
    if runs.positive?
      ((ha["mean"] * ha["runs"]) + (hb["mean"] * hb["runs"])) / runs.to_f
    else
      0.0
    end
  {
    "last_seconds" => [ha["last_seconds"], hb["last_seconds"]].max,
    "min" => [ha["min"], hb["min"]].min,
    "max" => [ha["max"], hb["max"]].max,
    "mean" => mean,
    "p95" => [ha["p95"], hb["p95"]].max,
    "runs" => runs,
    "failures" => ha["failures"] + hb["failures"],
    "timeouts" => ha["timeouts"] + hb["timeouts"]
  }
end

.normalize_entry(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/polyrun/timing/stats.rb', line 9

def normalize_entry(value)
  case value
  when Hash
    normalize_hash(value)
  else
    sec = value.to_f
    {
      "last_seconds" => sec,
      "min" => sec,
      "max" => sec,
      "mean" => sec,
      "p95" => sec,
      "runs" => 1,
      "failures" => 0,
      "timeouts" => 0
    }
  end
end

.normalize_hash(h) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity -- timing hash key coercion



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/polyrun/timing/stats.rb', line 29

def normalize_hash(h)
  out = {}
  sec = h["last_seconds"] || h[:last_seconds] || h["seconds"] || h[:seconds]
  sec = sec.to_f if sec
  mean = (h["mean"] || h[:mean] || sec)&.to_f
  out["last_seconds"] = (sec || mean || 0.0).to_f
  out["min"] = (h["min"] || h[:min] || out["last_seconds"]).to_f
  out["max"] = (h["max"] || h[:max] || out["last_seconds"]).to_f
  out["mean"] = (mean || out["last_seconds"]).to_f
  out["p95"] = (h["p95"] || h[:p95] || out["max"]).to_f
  out["runs"] = Integer(h["runs"] || h[:runs] || 1)
  out["failures"] = Integer(h["failures"] || h[:failures] || 0)
  out["timeouts"] = Integer(h["timeouts"] || h[:timeouts] || 0)
  out
end