-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
loadt.go
162 lines (149 loc) · 4.01 KB
/
loadt.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
package runn
import (
"fmt"
"io"
"text/template"
"time"
"github.com/dustin/go-humanize"
or "github.com/ryo-yamaoka/otchkiss/result"
)
const reportTemplate = `
Number of runbooks per RunN....: {{ .NumberOfRunbooks }}
Warm up time (--warm-up).......: {{ .WarmUpTime }}
Duration (--duration)..........: {{ .Duration }}
Concurrent (--load-concurrent).: {{ .MaxConcurrent }}
Max RunN per second (--max-rps): {{ .MaxRPS }}
Total..........................: {{ .TotalRequests }}
Succeeded......................: {{ .Succeeded }}
Failed.........................: {{ .Failed }}
Error rate.....................: {{ .ErrorRate }}%
RunN per second................: {{ .RPS }}
Latency .......................: max={{ .MaxLatency }}ms min={{ .MinLatency }}ms avg={{ .AvgLatency }}ms med={{ .MedLatency }}ms p(90)={{ .Latency90p }}ms p(99)={{ .Latency99p }}ms
`
type loadtResult struct {
runbookCount int64
warmUp time.Duration
duration time.Duration
concurrent int64
maxRPS int64
total int64
succeeded int64
failed int64
errorRate float64
rps float64
max float64
min float64
p99 float64
p90 float64
p50 float64
avg float64
}
func NewLoadtResult(rc int, w, d time.Duration, c, m int, r *or.Result) (*loadtResult, error) {
succeeded := r.Succeeded()
failed := r.Failed()
total := succeeded + failed
er := float64(failed) / float64(total) * 100
rps := float64(total) / d.Seconds()
max, err := r.PercentileLatency(100)
if err != nil {
return nil, err
}
min, err := r.PercentileLatency(0)
if err != nil {
return nil, err
}
p99, err := r.PercentileLatency(99)
if err != nil {
return nil, err
}
p90, err := r.PercentileLatency(90)
if err != nil {
return nil, err
}
p50, err := r.PercentileLatency(50)
if err != nil {
return nil, err
}
ll := r.Latencies()
var avg float64
for _, l := range ll {
avg += l
}
avg = avg / float64(len(ll))
return &loadtResult{
runbookCount: int64(rc),
warmUp: w,
duration: d,
concurrent: int64(c),
maxRPS: int64(m),
total: total,
succeeded: succeeded,
failed: failed,
errorRate: er,
rps: rps,
max: max,
min: min,
p99: p99,
p90: p90,
p50: p50,
avg: avg,
}, nil
}
func (r *loadtResult) Report(w io.Writer) error {
tmpl, err := template.New("report").Parse(reportTemplate)
if err != nil {
return err
}
data := map[string]any{
"NumberOfRunbooks": r.runbookCount,
"WarmUpTime": r.warmUp.String(),
"Duration": r.duration.String(),
"MaxConcurrent": r.concurrent,
"MaxRPS": r.maxRPS,
"TotalRequests": r.total,
"Succeeded": r.succeeded,
"Failed": r.failed,
"ErrorRate": humanize.CommafWithDigits(r.errorRate, 1),
"RPS": humanize.CommafWithDigits(r.rps, 1),
"MaxLatency": humanize.CommafWithDigits(r.max*1000, 1),
"MinLatency": humanize.CommafWithDigits(r.min*1000, 1),
"AvgLatency": humanize.CommafWithDigits(r.avg*1000, 1),
"MedLatency": humanize.CommafWithDigits(r.p50*1000, 1),
"Latency90p": humanize.CommafWithDigits(r.p90*1000, 1),
"Latency99p": humanize.CommafWithDigits(r.p99*1000, 1),
}
if err := tmpl.Execute(w, data); err != nil {
return err
}
return nil
}
func (r *loadtResult) CheckThreshold(threshold string) error {
if threshold == "" {
return nil
}
store := map[string]any{
"total": r.total,
"succeeded": r.succeeded,
"failed": r.failed,
"error_rate": r.errorRate,
"rps": r.rps,
"max": r.max * 1000,
"mid": r.p50 * 1000,
"min": r.min * 1000,
"p90": r.p90 * 1000,
"p99": r.p99 * 1000,
"avg": r.avg * 1000,
}
tf, err := EvalWithTrace(threshold, store)
if err != nil {
return err
}
if !tf.OutputAsBool() {
bt, err := tf.FormatTraceTree()
if err != nil {
return err
}
return fmt.Errorf("(%s) is not true\n%s", threshold, bt)
}
return nil
}