forked from Netflix/spectator-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_test.go
83 lines (71 loc) · 2 KB
/
timer_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
package spectator
import (
"reflect"
"testing"
)
func getTimer(name string) *Timer {
id := newId(name, nil)
return NewTimer(id)
}
func TestTimer_Record(t *testing.T) {
c := getTimer("inc")
if c.Count() != 0 {
t.Error("Count should start at 0, got ", c.Count())
}
c.Record(100)
if c.Count() != 1 {
t.Error("Count should be 1, got ", c.Count())
}
if c.TotalTime() != 100 {
t.Error("TotalTime should be 100, got ", c.TotalTime())
}
c.Record(0)
if c.Count() != 2 {
t.Error("Count should be 2, got ", c.Count())
}
if c.TotalTime() != 100 {
t.Error("TotalTime should be 100, got ", c.TotalTime())
}
c.Record(100)
if c.Count() != 3 {
t.Error("Count should be 3, got ", c.Count())
}
if c.TotalTime() != 200 {
t.Error("TotalTime should be 200, got ", c.TotalTime())
}
c.Record(-1)
if c.Count() != 3 && c.TotalTime() != 200 {
t.Error("Negative times should be ignored")
}
}
func assertTimer(t *testing.T, timer *Timer, count int64, total int64, totalSq float64, max int64) {
ms := timer.Measure()
if len(ms) != 4 {
t.Error("Expected 4 measurements from a Timer, got ", len(ms))
}
expected := make(map[uint64]float64)
expected[timer.id.WithStat("count").Hash()] = float64(count)
expected[timer.id.WithStat("totalTime").Hash()] = float64(total)
expected[timer.id.WithStat("totalOfSquares").Hash()] = totalSq
expected[timer.id.WithStat("max").Hash()] = float64(max)
got := make(map[uint64]float64)
for _, v := range ms {
got[v.id.Hash()] = v.value
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected measurements (count=%d, total=%d, totalSq=%.0f, max=%d)", count, total, totalSq, max)
for _, m := range ms {
t.Error("Got ", m.id.name, " ", m.id.tags, "=", m.value)
}
}
// ensure timer is reset after being measured
if timer.Count() != 0 || timer.TotalTime() != 0 {
t.Error("Timer should be reset after being measured")
}
}
func TestTimer_Measure(t *testing.T) {
c := getTimer("measure")
c.Record(100)
c.Record(200)
assertTimer(t, c, 2, 300, 100*100+200*200, 200)
}