-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathavgratecounter_test.go
116 lines (99 loc) · 3.04 KB
/
avgratecounter_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
package ratecounter
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAvgRateCounter(t *testing.T) {
interval := 50 * time.Millisecond
r := NewAvgRateCounter(interval)
assert.Equal(t, float64(0), r.Rate())
assert.Equal(t, int64(0), r.Hits())
r.Incr(1) // counter = 1, hits = 1
assert.Equal(t, float64(1.0), r.Rate())
assert.Equal(t, int64(1), r.Hits())
r.Incr(3) // counter = 4, hits = 2
assert.Equal(t, float64(2.0), r.Rate())
assert.Equal(t, int64(2), r.Hits())
time.Sleep(2 * interval)
assert.Equal(t, float64(0), r.Rate())
assert.Equal(t, int64(0), r.Hits())
}
func TestAvgRateCounterAdvanced(t *testing.T) {
interval := 50 * time.Millisecond
almost := 45 * time.Millisecond
gap := 1 * time.Millisecond
r := NewAvgRateCounter(interval)
assert.Equal(t, float64(0), r.Rate())
assert.Equal(t, int64(0), r.Hits())
r.Incr(1) // counter = 1, hits = 1
assert.Equal(t, float64(1.0), r.Rate())
assert.Equal(t, int64(1), r.Hits())
time.Sleep(interval - almost)
r.Incr(3) // counter = 4, hits = 2
assert.Equal(t, float64(2.0), r.Rate())
assert.Equal(t, int64(2), r.Hits())
time.Sleep(almost + gap)
assert.Equal(t, float64(3.0), r.Rate())
assert.Equal(t, int64(1), r.Hits()) // counter = 3, hits = 1
time.Sleep(2 * interval)
assert.Equal(t, float64(0), r.Rate())
assert.Equal(t, int64(0), r.Hits())
}
func TestAvgRateCounterMinResolution(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Resolution < 1 did not panic")
}
}()
NewAvgRateCounter(500 * time.Millisecond).WithResolution(0)
}
func TestAvgRateCounterNoResolution(t *testing.T) {
interval := 50 * time.Millisecond
almost := 45 * time.Millisecond
gap := 1 * time.Millisecond
r := NewAvgRateCounter(interval).WithResolution(1)
assert.Equal(t, float64(0), r.Rate())
assert.Equal(t, int64(0), r.Hits())
r.Incr(1) // counter = 1, hits = 1
assert.Equal(t, float64(1.0), r.Rate())
assert.Equal(t, int64(1), r.Hits())
time.Sleep(interval - almost)
r.Incr(3) // counter = 4, hits = 2
assert.Equal(t, float64(2.0), r.Rate())
assert.Equal(t, int64(2), r.Hits())
time.Sleep(almost + gap)
assert.Equal(t, float64(0), r.Rate())
assert.Equal(t, int64(0), r.Hits()) // counter = 0, hits = 0, r.Hits())
time.Sleep(2 * interval)
assert.Equal(t, float64(0), r.Rate())
assert.Equal(t, int64(0), r.Hits())
}
func TestAvgRateCounter_String(t *testing.T) {
r := NewAvgRateCounter(1 * time.Second)
if r.String() != "0.00000e+00" {
t.Error("Expected ", r.String(), " to equal ", "0.00000e+00")
}
r.Incr(1)
if r.String() != "1.00000e+00" {
t.Error("Expected ", r.String(), " to equal ", "1.00000e+00")
}
}
func TestAvgRateCounter_Incr_ReturnsImmediately(t *testing.T) {
interval := 1 * time.Second
r := NewAvgRateCounter(interval)
start := time.Now()
r.Incr(-1)
duration := time.Since(start)
if duration >= 1*time.Second {
t.Error("incr took", duration, "to return")
}
}
func BenchmarkAvgRateCounter(b *testing.B) {
interval := 1 * time.Millisecond
r := NewAvgRateCounter(interval)
for i := 0; i < b.N; i++ {
r.Incr(1)
r.Rate()
}
}