-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwindowedthroughput_test.go
117 lines (97 loc) · 2.79 KB
/
windowedthroughput_test.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package dynsampler
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type TestIndexGenerator struct {
CurrentIndex int64
}
func (g *TestIndexGenerator) GetCurrentIndex() int64 {
return g.CurrentIndex
}
func (g *TestIndexGenerator) DurationToIndexes(duration time.Duration) int64 {
return int64(duration.Seconds())
}
func TestHappyPath(t *testing.T) {
indexGenerator := &TestIndexGenerator{}
sampler := WindowedThroughput{
UpdateFrequencyDuration: 1 * time.Second,
LookbackFrequencyDuration: 5 * time.Second,
GoalThroughputPerSec: 2,
indexGenerator: indexGenerator,
countList: NewUnboundedBlockList(),
}
key := "test_key"
// Time 0: 20 traces seen.
for i := 0; i < 20; i++ {
assert.Equal(t, 0, sampler.GetSampleRate(key))
}
indexGenerator.CurrentIndex += 1
sampler.updateMaps()
// Time 1: 10 traces seen
for i := 0; i < 10; i++ {
assert.Equal(t, 2, sampler.GetSampleRate(key))
}
indexGenerator.CurrentIndex += 1
sampler.updateMaps()
// Time 2: 50 traces seen
for i := 0; i < 50; i++ {
assert.Equal(t, 3, sampler.GetSampleRate(key))
}
indexGenerator.CurrentIndex += 1
sampler.updateMaps()
// Time 3 & 4 & 5: 0 traces seen
for i := 0; i < 3; i++ {
indexGenerator.CurrentIndex += 1
sampler.updateMaps()
}
// Time 6: 40 traces seen.
for i := 0; i < 40; i++ {
// This should look back from time (0 - 5]
assert.Equal(t, 6, sampler.GetSampleRate(key))
}
indexGenerator.CurrentIndex += 1
sampler.updateMaps()
// Time 7: 5 traces seen.
for i := 0; i < 5; i++ {
// This should look back from time (1 - 6]
assert.Equal(t, 9, sampler.GetSampleRate(key))
}
}
func TestDropsOldBlocks(t *testing.T) {
indexGenerator := &TestIndexGenerator{}
sampler := WindowedThroughput{
UpdateFrequencyDuration: 1 * time.Second,
LookbackFrequencyDuration: 5 * time.Second,
GoalThroughputPerSec: 2,
indexGenerator: indexGenerator,
countList: NewUnboundedBlockList(),
}
key := "test_key"
// Time 0: 20 traces seen.
for i := 0; i < 20; i++ {
assert.Equal(t, 0, sampler.GetSampleRate(key))
}
for i := 0; i < 7; i++ {
indexGenerator.CurrentIndex += 1
sampler.updateMaps()
}
// Time 6: 20 traces seen.
for i := 0; i < 20; i++ {
assert.Equal(t, 0, sampler.GetSampleRate(key))
}
}
func TestSetsDefaultsCorrectly(t *testing.T) {
sampler1 := WindowedThroughput{}
sampler1.Start()
assert.Equal(t, time.Second, sampler1.UpdateFrequencyDuration)
assert.Equal(t, 30*time.Second, sampler1.LookbackFrequencyDuration)
sampler2 := WindowedThroughput{
UpdateFrequencyDuration: 5 * time.Second,
LookbackFrequencyDuration: 18 * time.Second,
}
sampler2.Start()
assert.Equal(t, 5*time.Second, sampler2.UpdateFrequencyDuration)
assert.Equal(t, 15*time.Second, sampler2.LookbackFrequencyDuration)
}