forked from rcrowley/go-metrics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrate_counter_test.go
124 lines (97 loc) · 2.62 KB
/
rate_counter_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
118
119
120
121
122
123
124
package metrics
import (
"testing"
"time"
"github.com/bountylabs/go-metrics/clock"
)
func TestRateCounterZero(t *testing.T) {
m := clock.NewMock()
rc := NewStandardRateCounter(60, 1000, m)
if v := rc.Rate1(); v != 0.0 {
t.Errorf("rc.Rate1(): 1.0 != %v\n", v)
}
if v := rc.Count(); v != 0.0 {
t.Errorf("rc.Count(): 1.0 != %v\n", v)
}
}
func TestRateCounter(t *testing.T) {
m := clock.NewMock()
rc := NewStandardRateCounter(60, 1000, m)
rc.Mark(1)
m.Add(1 * time.Second)
if v := rc.Rate1(); v != 1.0 {
t.Errorf("rc.Rate1(): 1.0 != %v\n", v)
}
if v := rc.Count(); v != 1.0 {
t.Errorf("rc.Count(): 1.0 != %v\n", v)
}
}
func TestShouldNotTakeIntoAccountDataFromOverAMinuteAgo(t *testing.T) {
m := clock.NewMock()
rc := NewStandardRateCounter(60, 1000, m)
// Mark only takes up to one sample per second so must increment by a second so a sample is taken
m.Add(1 * time.Second)
rc.Mark(500)
m.Add(60 * time.Second)
rc.Mark(60)
if v := rc.Rate1(); v != 1.0 {
t.Errorf("rc.Rate1(): 1.0 != %v\n", v)
}
if v := rc.Count(); v != 560.0 {
t.Errorf("rc.Count(): 560.0 != %v\n", v)
}
}
func TestShouldClearRateCounter(t *testing.T) {
m := clock.NewMock()
rc := NewStandardRateCounter(60, 1000, m)
rc.Mark(1)
m.Add(1 * time.Second)
rc.Clear()
if v := rc.Rate1(); v != 0.0 {
t.Errorf("rc.Rate1(): 0.0 != %v\n", v)
}
if v := rc.Count(); v != 0.0 {
t.Errorf("rc.Count(): 0.0 != %v\n", v)
}
}
func TestSnapshot(t *testing.T) {
m := clock.NewMock()
rc := NewStandardRateCounter(60, 1000, m)
rc.Mark(1)
m.Add(1 * time.Second)
s := rc.Snapshot()
rc.Mark(100)
if v := s.Rate1(); v != 1.0 {
t.Errorf("s.Rate1(): 1.0 != %v\n", v)
}
if v := s.Count(); v != 1.0 {
t.Errorf("s.Count(): 1.0 != %v\n", v)
}
}
func TestRateAndLastCountInSnapshotShouldBeConsistent(t *testing.T) {
// If new data isn't present in Snapshot.Rate1() it shouldn't be present in counter.lastCount but it should
// in Snapshot.Count()
m := clock.NewMock()
rc := NewStandardRateCounter(60, 1000, m)
rc.Mark(1)
s := rc.Snapshot()
// since we don't advance the time, the sampling period is still current, and there is nothing finalized for rate or .lastCount
// but this doesn't matter for Count()
if v := s.Rate1(); v != 0.0 {
t.Errorf("s.Rate1(): 0.0 != %v\n", v)
}
if v := rc.(*StandardRateCounter).lastCount; v != 0.0 {
t.Errorf("s.lastCount: 0.0 != %v\n", v)
}
if v := s.Count(); v != 1.0 {
t.Errorf("s.Count(): 1.0 != %v\n", v)
}
m.Add(1 * time.Second)
s = rc.Snapshot()
if v := s.Rate1(); v != 1.0 {
t.Errorf("s.Rate1(): 1.0 != %v\n", v)
}
if v := s.Count(); v != 1.0 {
t.Errorf("s.Count(): 1.0 != %v\n", v)
}
}