-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmetrics_test.go
More file actions
147 lines (122 loc) · 2.63 KB
/
metrics_test.go
File metadata and controls
147 lines (122 loc) · 2.63 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package measure
import (
"log"
"strconv"
"testing"
"time"
)
var enablePrint = false
func printf(format string, a ...interface{}) {
if enablePrint {
log.Printf(format, a...)
}
}
func TestMeasure(t *testing.T) {
const key = "test"
Reset()
m := Start(key)
m.Stop()
stats := GetStats()
if stats == nil || len(stats) < 1 {
t.Fatal("GetStats returns nil")
}
if stats[0].Count != 1 {
t.Errorf("Stats.Count got %d expect %d", stats[0].Count, 1)
}
}
func TestMeasureMulti(t *testing.T) {
const key = "test_multi"
const loop = 100
Reset()
for i := 0; i < loop; i++ {
m := Start(key)
time.Sleep(time.Microsecond)
m.Stop()
}
stats := GetStats()
if stats == nil || len(stats) < 1 {
t.Fatal("GetStats returns nil")
}
if stats[0].Count != loop {
t.Errorf("Stats.Count got %d expect %d", stats[0].Count, loop)
}
if stats[0].Min > stats[0].Max {
t.Errorf("Stats.Min %f > Stats.Max %f", stats[0].Min, stats[0].Max)
}
if stats[0].Min > stats[0].Avg {
t.Errorf("Stats.Min %f > Stats.Max %f", stats[0].Min, stats[0].Avg)
}
if stats[0].Max < stats[0].Avg {
t.Errorf("Stats.Min %f < Stats.Max %f", stats[0].Max, stats[0].Avg)
}
if stats[0].Sum < stats[0].Max {
t.Errorf("Stats.Sum %f < Stats.Max %f", stats[0].Sum, stats[0].Max)
}
}
func TestMeasureSort(t *testing.T) {
const loop = 100
Reset()
for i := 0; i < loop; i++ {
m := Start(strconv.Itoa(i))
time.Sleep(time.Microsecond)
m.Stop()
}
stats := GetStats()
if stats == nil || len(stats) < 1 {
t.Fatal("GetStats returns nil")
}
stats.SortAsc("sum")
for i := 0; i < len(stats)-1; i++ {
n, m := stats[i], stats[i+1]
if n.Sum > m.Sum {
t.Fatal("SortAsc fail")
}
printf("%f <= %f", n.Sum, m.Sum)
}
stats.SortDesc("sum")
for i := 0; i < len(stats)-1; i++ {
n, m := stats[i], stats[i+1]
if n.Sum < m.Sum {
t.Fatal("SortDesc fail")
}
printf("%f >= %f", n.Sum, m.Sum)
}
}
func TestMeasureMetrics(t *testing.T) {
m1 := NewMetrics()
m2 := NewMetrics()
s1 := m1.Start("test")
s2 := m2.Start("test")
s1.Stop()
s2.Stop()
if len(m1.metrics) != 1 {
t.Error("invalid len")
} else if _, ok := m1.metrics["test"]; !ok {
t.Error("invalid map")
}
if len(m2.metrics) != 1 {
t.Error("invalid len")
} else if _, ok := m2.metrics["test"]; !ok {
t.Error("invalid map")
}
log.Print(m1.GetStats())
log.Print(m2.GetStats())
}
func BenchmarkMeasure(b *testing.B) {
const key = "test"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := Start(key)
m.Stop()
}
}
func BenchmarkMeasureDisabled(b *testing.B) {
const key = "test"
Disabled = true
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := Start(key)
m.Stop()
}
Disabled = false
}