-
Notifications
You must be signed in to change notification settings - Fork 1
/
meters_test.go
97 lines (85 loc) · 3.08 KB
/
meters_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
package metrics
import (
"net/http"
"testing"
"time"
"github.com/nbio/st"
)
func TestMeterNumberOfRequests(t *testing.T) {
info, metrics := createMetrics()
defer metrics.Reset()
MeterNumberOfRequests(info, metrics)
st.Expect(t, len(metrics.counters), 1)
st.Expect(t, metrics.Snapshot().Counters["req.total"], uint64(1))
}
func TestMeterResponseStatus(t *testing.T) {
info, metrics := createMetrics()
MeterResponseStatus(info, metrics)
st.Expect(t, len(metrics.counters), 1)
st.Expect(t, metrics.Snapshot().Counters["res.status.ok"], uint64(1))
metrics.Reset()
info, metrics = createMetrics()
info.Status = 400
MeterResponseStatus(info, metrics)
st.Expect(t, len(metrics.counters), 1)
st.Expect(t, metrics.Snapshot().Counters["res.status.ok"], uint64(0))
st.Expect(t, metrics.Snapshot().Counters["res.status.bad"], uint64(1))
metrics.Reset()
info, metrics = createMetrics()
info.Status = 500
MeterResponseStatus(info, metrics)
st.Expect(t, len(metrics.counters), 1)
st.Expect(t, metrics.Snapshot().Counters["res.status.ok"], uint64(0))
st.Expect(t, metrics.Snapshot().Counters["res.status.bad"], uint64(0))
st.Expect(t, metrics.Snapshot().Counters["res.status.error"], uint64(1))
metrics.Reset()
}
func TestMeterRequestOperation(t *testing.T) {
info, metrics := createMetrics()
MeterRequestOperation(info, metrics)
st.Expect(t, len(metrics.counters), 1)
st.Expect(t, metrics.Snapshot().Counters["req.reads"], uint64(1))
metrics.Reset()
info, metrics = createMetrics()
info.Request.Method = "POST"
MeterRequestOperation(info, metrics)
st.Expect(t, len(metrics.counters), 1)
st.Expect(t, metrics.Snapshot().Counters["req.reads"], uint64(0))
st.Expect(t, metrics.Snapshot().Counters["req.writes"], uint64(1))
metrics.Reset()
}
func TestMeterResponseTime(t *testing.T) {
info, metrics := createMetrics()
defer metrics.Reset()
MeterResponseTime(info, metrics)
st.Expect(t, metrics.Snapshot().Gauges["res.time.P50"] >= 100, true)
st.Expect(t, metrics.Snapshot().Gauges["res.time.P99"] >= 100, true)
st.Expect(t, metrics.Snapshot().Gauges["res.time.P999"] >= 100, true)
}
func TestMeterResponseBodySize(t *testing.T) {
info, metrics := createMetrics()
defer metrics.Reset()
MeterResponseBodySize(info, metrics)
st.Expect(t, metrics.Snapshot().Gauges["res.body.size.P50"], int64(10))
st.Expect(t, metrics.Snapshot().Gauges["res.body.size.P99"], int64(10))
st.Expect(t, metrics.Snapshot().Gauges["res.body.size.P999"], int64(10))
}
func TestMeterRequestBodySize(t *testing.T) {
info, metrics := createMetrics()
defer metrics.Reset()
MeterRequestBodySize(info, metrics)
st.Expect(t, metrics.Snapshot().Gauges["req.body.size.P50"], int64(10))
st.Expect(t, metrics.Snapshot().Gauges["req.body.size.P99"], int64(10))
st.Expect(t, metrics.Snapshot().Gauges["req.body.size.P999"], int64(10))
}
func createMetrics() (*Info, *Metrics) {
metrics := NewMetrics()
info := &Info{
Status: 200,
BodyLength: 10 * 1024,
TimeStart: time.Now(),
TimeEnd: time.Now().Add(100 * time.Millisecond),
Request: &http.Request{Method: "GET", ContentLength: 10 * 1024},
}
return info, metrics
}