-
Notifications
You must be signed in to change notification settings - Fork 244
/
stats_test.go
165 lines (137 loc) · 5.09 KB
/
stats_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package boomer
import (
"testing"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func BenchmarkLogRequest(b *testing.B) {
newStats := newRequestStats()
for i := 0; i < b.N; i++ {
newStats.logRequest("http", "success", 2, 30)
}
}
func BenchmarkLogError(b *testing.B) {
newStats := newRequestStats()
for i := 0; i < b.N; i++ {
// LogError use md5 to calculate hash keys, it may slow down the only goroutine,
// which consumes both requestSuccessChannel and requestFailureChannel.
newStats.logError("http", "failure", "500 error")
}
}
var _ = Describe("Test states", func() {
It("test log request", func() {
newStats := newRequestStats()
newStats.logRequest("http", "success", 2, 30)
newStats.logRequest("http", "success", 3, 40)
newStats.logRequest("http", "success", 2, 40)
newStats.logRequest("http", "success", 1, 20)
entry := newStats.get("success", "http")
Expect(entry.NumRequests).To(BeEquivalentTo(4))
Expect(entry.MinResponseTime).To(BeEquivalentTo(1))
Expect(entry.MaxResponseTime).To(BeEquivalentTo(3))
Expect(entry.TotalResponseTime).To(BeEquivalentTo(8))
Expect(entry.TotalContentLength).To(BeEquivalentTo(130))
Expect(newStats.total.NumRequests).To(BeEquivalentTo(4))
Expect(newStats.total.MinResponseTime).To(BeEquivalentTo(1))
Expect(newStats.total.MaxResponseTime).To(BeEquivalentTo(3))
Expect(newStats.total.TotalResponseTime).To(BeEquivalentTo(8))
Expect(newStats.total.TotalContentLength).To(BeEquivalentTo(130))
})
It("test rounded response time", func() {
newStats := newRequestStats()
newStats.logRequest("http", "success", 147, 1)
newStats.logRequest("http", "success", 3432, 1)
newStats.logRequest("http", "success", 58760, 1)
entry := newStats.get("success", "http")
responseTimes := entry.ResponseTimes
Expect(responseTimes).To(HaveLen(3))
Expect(responseTimes).To(HaveKeyWithValue(int64(150), int64(1)))
Expect(responseTimes).To(HaveKeyWithValue(int64(3400), int64(1)))
Expect(responseTimes).To(HaveKeyWithValue(int64(59000), int64(1)))
})
It("test log error", func() {
newStats := newRequestStats()
newStats.logError("http", "failure", "500 error")
newStats.logError("http", "failure", "400 error")
newStats.logError("http", "failure", "400 error")
entry := newStats.get("failure", "http")
Expect(entry.NumFailures).To(BeEquivalentTo(3))
Expect(newStats.total.NumFailures).To(BeEquivalentTo(3))
// md5("httpfailure500 error") = 547c38e4e4742c1c581f9e2809ba4f55
err500 := newStats.errors["547c38e4e4742c1c581f9e2809ba4f55"]
Expect(err500.error).To(Equal("500 error"))
Expect(err500.occurrences).To(BeEquivalentTo(1))
err400 := newStats.errors["f391c310401ad8e10e929f2ee1a614e4"]
Expect(err400.error).To(Equal("400 error"))
Expect(err400.occurrences).To(BeEquivalentTo(2))
})
It("test clear all", func() {
newStats := newRequestStats()
newStats.logRequest("http", "success", 1, 20)
newStats.clearAll()
Expect(newStats.total.NumRequests).To(BeEquivalentTo(0))
})
It("test clear all by channel", func() {
newStats := newRequestStats()
newStats.start()
defer newStats.close()
newStats.logRequest("http", "success", 1, 20)
newStats.clearStatsChan <- true
Expect(newStats.total.NumRequests).To(BeEquivalentTo(0))
})
It("test serialize stats", func() {
newStats := newRequestStats()
newStats.logRequest("http", "success", 1, 20)
serialized := newStats.serializeStats()
Expect(serialized).To(HaveLen(1))
first := serialized[0]
entry, err := deserializeStatsEntry(first)
Expect(err).NotTo(HaveOccurred())
Expect(entry.Name).To(Equal("success"))
Expect(entry.Method).To(Equal("http"))
Expect(entry.NumRequests).To(BeEquivalentTo(1))
Expect(entry.NumFailures).To(BeEquivalentTo(0))
})
It("test serialize errors", func() {
newStats := newRequestStats()
newStats.logError("http", "failure", "500 error")
newStats.logError("http", "failure", "400 error")
newStats.logError("http", "failure", "400 error")
serialized := newStats.serializeErrors()
Expect(serialized).To(HaveLen(2))
Expect(serialized).To(HaveKeyWithValue("f391c310401ad8e10e929f2ee1a614e4", map[string]interface{}{
"error": "400 error",
"occurrences": int64(2),
"name": "failure",
"method": "http",
}))
})
It("test collect report data", func() {
newStats := newRequestStats()
newStats.logRequest("http", "success", 2, 30)
newStats.logError("http", "failure", "500 error")
result := newStats.collectReportData()
Expect(result).To(HaveKey("stats"))
Expect(result).To(HaveKey("stats_total"))
Expect(result).To(HaveKey("errors"))
})
It("test stats start", func() {
newStats := newRequestStats()
newStats.start()
defer newStats.close()
newStats.requestSuccessChan <- &requestSuccess{
requestType: "http",
name: "success",
responseTime: 2,
responseLength: 30,
}
newStats.requestFailureChan <- &requestFailure{
requestType: "http",
name: "failure",
responseTime: 1,
error: "500 error",
}
Eventually(newStats.messageToRunnerChan).WithTimeout(slaveReportInterval + 500*time.Millisecond).Should(Receive())
})
})