Module: HeapScope::Growth

Defined in:
lib/heapscope/growth.rb

Overview

Detects growth patterns across samples. Evidence-based, not magical.

Constant Summary collapse

PATTERNS =
%i[
  stable bursty linear_growth monotonic_growth sawtooth
  exponential_like bounded_plateau insufficient_data
].freeze

Class Method Summary collapse

Class Method Details

.analyze(samples) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/heapscope/growth.rb', line 13

def analyze(samples)
  values = samples.map(&:to_f)
  return result(:insufficient_data, values) if values.size < 3

  slope = linear_slope(values)
  variance = sample_variance(values)
  mean = values.sum / values.size
  recovering = sawtooth?(values)
  plateau = plateau?(values)
  monotonic = monotonic?(values)
  exponential = exponential_like?(values)

  relative_span = mean.positive? ? (values.max - values.min) / mean : 0

  pattern =
    if recovering
      :sawtooth
    elsif plateau && values.max > values.first * 1.5
      :bounded_plateau
    elsif exponential
      :exponential_like
    elsif monotonic && slope > 0 && !exponential
      :monotonic_growth
    elsif relative_span < 0.05 || (slope.abs < [mean * 0.02, 1.0].max && variance < (mean * 0.05)**2)
      :stable
    elsif slope > 0
      :linear_growth
    elsif variance > (mean * 0.3)**2
      :bursty
    else
      :stable
    end

  result(pattern, values, slope: slope)
end

.exponential_like?(values) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'lib/heapscope/growth.rb', line 99

def exponential_like?(values)
  return false unless monotonic?(values) && values.first.positive?
  return false if values.size < 4

  ratios = values.each_cons(2).map { |a, b| a.positive? ? b / a : 0 }
  # Require accelerating growth, not merely a steady linear climb.
  ratios.size >= 3 && ratios.all? { |r| r >= 1.4 } && ratios.last >= ratios.first
end

.linear_slope(values) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/heapscope/growth.rb', line 61

def linear_slope(values)
  n = values.size
  xs = (0...n).map(&:to_f)
  x_mean = xs.sum / n
  y_mean = values.sum / n
  num = xs.zip(values).sum { |x, y| (x - x_mean) * (y - y_mean) }
  den = xs.sum { |x| (x - x_mean)**2 }
  return 0.0 if den.zero?

  num / den
end

.monotonic?(values) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/heapscope/growth.rb', line 80

def monotonic?(values)
  values.each_cons(2).all? { |a, b| b >= a }
end

.plateau?(values) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
# File 'lib/heapscope/growth.rb', line 90

def plateau?(values)
  return false if values.size < 5

  last = values.last(3)
  span = last.max - last.min
  early_growth = values[2] >= values.first * 1.5
  span <= [last.max * 0.05, 2].max && early_growth
end

.result(pattern, values, slope: 0.0) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/heapscope/growth.rb', line 49

def result(pattern, values, slope: 0.0)
  {
    pattern: pattern,
    slope: slope.round(3),
    samples: values,
    min: values.min,
    max: values.max,
    mean: values.empty? ? 0.0 : (values.sum / values.size).round(3),
    recovery_after_gc: pattern == :sawtooth ? :observed : :minimal
  }
end

.sample_variance(values) ⇒ Object



73
74
75
76
77
78
# File 'lib/heapscope/growth.rb', line 73

def sample_variance(values)
  return 0.0 if values.size < 2

  mean = values.sum / values.size
  values.sum { |v| (v - mean)**2 } / (values.size - 1)
end

.sawtooth?(values) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/heapscope/growth.rb', line 84

def sawtooth?(values)
  drops = values.each_cons(2).count { |a, b| b < a * 0.7 }
  rises = values.each_cons(2).count { |a, b| b > a }
  drops >= 1 && rises >= 2
end