-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
76 lines (66 loc) · 1.46 KB
/
result.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
package hit
import (
"fmt"
"io"
"net/http"
"strings"
"time"
)
type Result struct {
RPS float64
Requests int
Errors int
Bytes int64
Duration time.Duration
Fastest time.Duration
Slowest time.Duration
Status int
Error error
}
func (r Result) Merge(other Result) Result {
r.Requests++
r.Bytes += other.Bytes
if r.Fastest == 0 || other.Duration < r.Fastest {
r.Fastest = other.Duration
}
if other.Duration > r.Slowest {
r.Slowest = other.Duration
}
if other.Error != nil || other.Status != http.StatusOK {
r.Errors++
}
return r
}
func (r Result) Finalize(total time.Duration) Result {
r.Duration = total
r.RPS = float64(r.Requests / int(total.Seconds()))
return r
}
func (r Result) Fprint(out io.Writer) {
p := func(format string, args ...any) {
fmt.Fprintf(out, format, args...)
}
p("\nSummary:\n")
p("\tSuccess \t: %.0f%%\n", r.successRatio())
p("\tRPS \t\t: %.1f\n", r.RPS)
p("\tRequests \t: %d\n", r.Requests)
p("\tErrors \t\t: %d\n", r.Errors)
p("\tBytes \t\t: %d\n", r.Bytes)
p("\tDuration \t: %s\n", round(r.Duration))
if r.Requests > 1 {
p("\tFastest \t: %s\n", round(r.Fastest))
p("\tSlowest \t: %s\n", round(r.Slowest))
}
}
func (r Result) String() string {
var s strings.Builder
r.Fprint(&s)
return s.String()
}
func (r Result) successRatio() float64 {
rr, e := float64(r.Requests), float64(r.Errors)
return (rr - e) / rr * 100
}
func round(t time.Duration) time.Duration {
return t.Round(time.Microsecond)
}