forked from streamingfast/dmetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_counters_test.go
70 lines (61 loc) · 1.46 KB
/
rate_counters_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
package dmetrics
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewPerSecondLocalCounter(t *testing.T) {
type args struct {
unit string
}
tests := []struct {
name string
args args
want *RateCounter
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, NewPerSecondLocalRateCounter(tt.args.unit))
})
}
}
func TestRateCounter(t *testing.T) {
t.Skip("this is more of an example, but usefull to understand")
interval := 1 * time.Second
r := NewRateCounter(interval, "event")
// this will increment the rate counter and wait 200 ms.
// up until we cross the 1-second mark, the rate increase linearly
// after the 1-second mark rate will stabilize, since it will increment
// events at a constant rate
for i := 0; i < 10; i++ {
r.Inc()
time.Sleep(200 * time.Millisecond)
fmt.Println(r.String())
}
}
func TestRateCounter_GoldenPath(t *testing.T) {
interval := 1 * time.Second
r := NewRateCounter(interval, "event")
r.Inc()
r.Inc()
r.Inc()
r.Inc()
r.Inc()
assert.EqualValues(t, 5, r.Rate())
assert.Equal(t, "5 event/s (5 total)", r.String())
}
func TestRateCounter_ExceedsIntervalWindow(t *testing.T) {
interval := 1 * time.Millisecond
r := NewRateCounter(interval, "event")
r.Inc()
r.Inc()
r.Inc()
r.Inc()
r.Inc()
time.Sleep(10 * interval)
assert.EqualValues(t, 0, r.Rate())
assert.Equal(t, "0 event/ms (5 total)", r.String())
}