-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor_test.go
109 lines (86 loc) · 1.84 KB
/
monitor_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
// +build !js
// Copyright (c) 2009-2020 Misakai Ltd. and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package stats
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func BenchmarkMeasure(b *testing.B) {
m := New()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Measure("abc", 15423)
}
}
func BenchmarkEncode(b *testing.B) {
m := New()
for i := 0; i < 50; i++ {
for j := 0; j < 100; j++ {
m.Measure(fmt.Sprintf("%d", j), int32(i))
}
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Snapshot()
}
}
func BenchmarkRuntime(b *testing.B) {
m := New()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.MeasureRuntime()
}
}
func TestMeasureElapsed(t *testing.T) {
m := New()
measureDelay(m)
elapsed := m.Get("a").Max()
assert.NotZero(t, elapsed)
}
func measureDelay(m *Monitor) {
defer m.MeasureElapsed("a", time.Now())
time.Sleep(1 * time.Millisecond)
}
func TestMonitorTag(t *testing.T) {
m := New()
m.Tag("a", "roman")
assert.Equal(t, "roman", m.Get("a").Tag())
}
func TestMeasureRuntime(t *testing.T) {
m := New()
m.MeasureRuntime()
assert.NotZero(t, m.Get("go.procs").Max())
}
func TestHistogramEncodeMany(t *testing.T) {
m := New()
for i := 0; i < 1000; i++ {
for j := 0; j < 100; j++ {
m.Measure(fmt.Sprintf("%d", j), rand.Int31n(10000))
}
}
v := m.Snapshot()
assert.True(t, len(v) > 1000)
}
func TestHistogram(t *testing.T) {
m := New()
for i := 0; i < 5000; i++ {
m.MeasureElapsed("b", time.Unix(0, 0))
m.Measure("a", int32(i))
}
// Snapshot
v := m.Snapshot()
assert.True(t, len(v) > 50)
// Restore
h, err := Restore(v)
assert.NoError(t, err)
assert.Len(t, h, 2)
assert.Equal(t, 5000, h[0].Count())
assert.Equal(t, 5000, h[1].Count())
}