-
Notifications
You must be signed in to change notification settings - Fork 30
/
http_results.go
203 lines (176 loc) · 5.1 KB
/
http_results.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"encoding/json"
"fmt"
"sort"
"sync"
)
const scaleNum = 10000
var pctls = []int{10, 25, 50, 75, 90, 95, 99}
var resultRdMutex sync.RWMutex
// StressResult record result
type StressResult struct {
ErrCode int `json:"err_code"`
ErrMsg string `json:"err_msg"`
AvgTotal int64 `json:"avg_total"`
Fastest int64 `json:"fastest"`
Slowest int64 `json:"slowest"`
Average int64 `json:"average"`
Rps int64 `json:"rps"`
ErrorDist map[string]int `json:"error_dist"`
StatusCodeDist map[int]int `json:"status_code_dist"`
Lats map[string]int64 `json:"lats"`
LatsTotal int64 `json:"lats_total"`
SizeTotal int64 `json:"size_total"`
Duration int64 `json:"duration"`
Output string `json:"output"`
}
func toByteSizeStr(size float64) string {
switch {
case size > 1073741824:
return fmt.Sprintf("%4.3f GB", size/1073741824)
case size > 1048576:
return fmt.Sprintf("%4.3f MB", size/1048576)
case size > 1024:
return fmt.Sprintf("%4.3f KB", size/1024)
}
return fmt.Sprintf("%4.3f bytes", size)
}
func println(vfmt string, args ...interface{}) {
fmt.Printf(vfmt+"\n", args...)
}
func GetStressResult() *StressResult {
return &StressResult{
ErrorDist: make(map[string]int, 0),
StatusCodeDist: make(map[int]int, 0),
Lats: make(map[string]int64, 0),
Slowest: int64(IntMin),
Fastest: int64(IntMax),
}
}
func (result *StressResult) print() {
resultRdMutex.RLock()
defer resultRdMutex.RUnlock()
switch result.Output {
case "csv":
println("Duration,Count")
for duration, val := range result.Lats {
println("%s,%d", duration, val/scaleNum)
}
return
}
if len(result.Lats) > 0 {
println("Summary:")
println(" Total:\t%4.3f secs", float32(result.Duration))
println(" Slowest:\t%4.3f secs", float32(result.Slowest)/scaleNum)
println(" Fastest:\t%4.3f secs", float32(result.Fastest)/scaleNum)
println(" Average:\t%4.3f secs", float32(result.Average)/scaleNum)
println(" Requests/sec:\t%4.3f", float32(result.Rps)/scaleNum)
println(" Total data:\t%s", toByteSizeStr(float64(result.SizeTotal)))
println(" Size/request:\t%d bytes", result.SizeTotal/result.LatsTotal)
result.printStatusCodes()
result.printLatencies()
}
if len(result.ErrorDist) > 0 {
result.printErrors()
}
}
// printLatencies Print latency distribution.
func (result *StressResult) printLatencies() {
data := make([]string, len(pctls))
durationLats := make([]string, 0)
for duration := range result.Lats {
durationLats = append(durationLats, duration)
}
sort.Strings(durationLats)
for i, j, dCounts := 0, 0, int64(0); i < len(durationLats) && j < len(pctls); i = i + 1 {
dCounts = dCounts + result.Lats[durationLats[i]]
if int(dCounts*100/result.LatsTotal) >= pctls[j] {
data[j] = durationLats[i]
j++
}
}
println("\nLatency distribution:")
for i := 0; i < len(pctls); i++ {
println(" %v%% in %s secs", pctls[i], data[i])
}
}
// printStatusCodes Print status code distribution.
func (result *StressResult) printStatusCodes() {
println("\nStatus code distribution:")
for code, num := range result.StatusCodeDist {
println(" [%d]\t%d responses", code, num)
}
}
// printErrors Print response errors
func (result *StressResult) printErrors() {
println("\nError distribution:")
for err, num := range result.ErrorDist {
fmt.Printf(" [%d]\t%s", num, err)
}
}
func (result *StressResult) marshal() ([]byte, error) {
resultRdMutex.RLock()
defer resultRdMutex.RUnlock()
return json.Marshal(result)
}
func (result *StressResult) append(res *result) {
resultRdMutex.Lock()
defer resultRdMutex.Unlock()
if res.err != nil {
result.ErrorDist[res.err.Error()]++
} else {
result.Lats[fmt.Sprintf("%4.3f", res.duration.Seconds())]++
duration := int64(res.duration.Seconds() * scaleNum)
result.LatsTotal++
if result.Slowest < duration {
result.Slowest = duration
}
if result.Fastest > duration {
result.Fastest = duration
}
result.AvgTotal += duration
result.StatusCodeDist[res.statusCode]++
if res.contentLength > 0 {
result.SizeTotal += res.contentLength
}
}
}
// calMutliStressResult calculate mutli stress result
func calMutliStressResult(result *StressResult, resultList ...StressResult) *StressResult {
if result == nil {
result = GetStressResult()
}
var duration int64 = result.Duration
for _, v := range resultList {
if result.Slowest < v.Slowest {
result.Slowest = v.Slowest
}
if result.Fastest > v.Fastest {
result.Fastest = v.Fastest
}
result.LatsTotal += v.LatsTotal
result.AvgTotal += v.AvgTotal
for code, c := range v.StatusCodeDist {
result.StatusCodeDist[code] += c
}
result.SizeTotal += v.SizeTotal
for code, c := range v.ErrorDist {
result.ErrorDist[code] += c
}
for lats, c := range v.Lats {
result.Lats[lats] += c
}
if duration < v.Duration {
duration = v.Duration
}
}
if duration > 0 {
result.Duration = duration
result.Rps = int64((result.LatsTotal * scaleNum) / duration)
}
if result.LatsTotal > 0 {
result.Average = result.AvgTotal / result.LatsTotal
}
return result
}