diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
commit | b09c6d56832eb1718c07d74abf3bc6ae3fe4e030 (patch) | |
tree | d2caec2610d4ea887803ec9e9c3cd77136c448ba /dependencies/pkg/mod/github.com/!vivid!cortex/ewma@v1.2.0/ewma_test.go | |
parent | Initial commit. (diff) | |
download | icingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.tar.xz icingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.zip |
Adding upstream version 1.1.0.upstream/1.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dependencies/pkg/mod/github.com/!vivid!cortex/ewma@v1.2.0/ewma_test.go')
-rw-r--r-- | dependencies/pkg/mod/github.com/!vivid!cortex/ewma@v1.2.0/ewma_test.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/dependencies/pkg/mod/github.com/!vivid!cortex/ewma@v1.2.0/ewma_test.go b/dependencies/pkg/mod/github.com/!vivid!cortex/ewma@v1.2.0/ewma_test.go new file mode 100644 index 0000000..a6cd37b --- /dev/null +++ b/dependencies/pkg/mod/github.com/!vivid!cortex/ewma@v1.2.0/ewma_test.go @@ -0,0 +1,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) + } +} |