summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/github.com/!vivid!cortex/ewma@v1.2.0/ewma_test.go
blob: a6cd37b6a107e6ddfe083b07bf5cdb796b756261 (plain)
1
2
3
4
5
6
7
8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package ewma

// Copyright (c) 2013 VividCortex, Inc. All rights reserved.
// Please see the LICENSE file for applicable license terms.

import (
	"math"
	"testing"
)

const testMargin = 0.00000001

var samples = [100]float64{
	4599, 5711, 4746, 4621, 5037, 4218, 4925, 4281, 5207, 5203, 5594, 5149,
	4948, 4994, 6056, 4417, 4973, 4714, 4964, 5280, 5074, 4913, 4119, 4522,
	4631, 4341, 4909, 4750, 4663, 5167, 3683, 4964, 5151, 4892, 4171, 5097,
	3546, 4144, 4551, 6557, 4234, 5026, 5220, 4144, 5547, 4747, 4732, 5327,
	5442, 4176, 4907, 3570, 4684, 4161, 5206, 4952, 4317, 4819, 4668, 4603,
	4885, 4645, 4401, 4362, 5035, 3954, 4738, 4545, 5433, 6326, 5927, 4983,
	5364, 4598, 5071, 5231, 5250, 4621, 4269, 3953, 3308, 3623, 5264, 5322,
	5395, 4753, 4936, 5315, 5243, 5060, 4989, 4921, 4480, 3426, 3687, 4220,
	3197, 5139, 6101, 5279,
}

func withinMargin(a, b float64) bool {
	return math.Abs(a-b) <= testMargin
}

func TestSimpleEWMA(t *testing.T) {
	var e SimpleEWMA
	for _, f := range samples {
		e.Add(f)
	}
	if !withinMargin(e.Value(), 4734.500946466118) {
		t.Errorf("e.Value() is %v, wanted %v", e.Value(), 4734.500946466118)
	}
	e.Set(1.0)
	if e.Value() != 1.0 {
		t.Errorf("e.Value() is %v", e.Value())
	}
}

func TestVariableEWMA(t *testing.T) {
	e := NewMovingAverage(30)
	for _, f := range samples {
		e.Add(f)
	}
	if !withinMargin(e.Value(), 4734.500946466118) {
		t.Errorf("e.Value() is %v, wanted %v", e.Value(), 4734.500946466118)
	}
	e.Set(1.0)
	if e.Value() != 1.0 {
		t.Errorf("e.Value() is %v", e.Value())
	}
}

func TestVariableEWMA2(t *testing.T) {
	e := NewMovingAverage(5)
	for _, f := range samples {
		e.Add(f)
	}
	if !withinMargin(e.Value(), 5015.397367486725) {
		t.Errorf("e.Value() is %v, wanted %v", e.Value(), 5015.397367486725)
	}
}

func TestVariableEWMAWarmup(t *testing.T) {
	e := NewMovingAverage(5)
	for i, f := range samples {
		e.Add(f)

		// all values returned during warmup should be 0.0
		if uint8(i) < WARMUP_SAMPLES {
			if e.Value() != 0.0 {
				t.Errorf("e.Value() is %v, expected %v", e.Value(), 0.0)
			}
		}
	}
	e = NewMovingAverage(5)
	e.Set(5)
	e.Add(1)
	if e.Value() >= 5 {
		t.Errorf("e.Value() is %v, expected it to decay towards 0", e.Value())
	}
}

func TestVariableEWMAWarmup2(t *testing.T) {
	e := NewMovingAverage(5)
	testSamples := [12]float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10000, 1}
	for i, f := range testSamples {
		e.Add(f)

		// all values returned during warmup should be 0.0
		if uint8(i) < WARMUP_SAMPLES {
			if e.Value() != 0.0 {
				t.Errorf("e.Value() is %v, expected %v", e.Value(), 0.0)
			}
		}
	}
	if val := e.Value(); val == 1.0 {
		t.Errorf("e.Value() is expected to be greater than %v", 1.0)
	}
}