-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
90 lines (71 loc) · 1.78 KB
/
report.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
package qube
import (
"runtime"
"strings"
"time"
"github.com/neilotoole/jsoncolor"
"github.com/jamiealquiza/tachymeter"
)
type JSONDuration time.Duration
func (jd JSONDuration) MarshalJSON() (b []byte, err error) {
d := time.Duration(jd)
return []byte(`"` + d.String() + `"`), nil
}
type Report struct {
ID string
StartedAt time.Time
FinishedAt time.Time
ElapsedTime JSONDuration
Options *Options
GOMAXPROCS int
QueryCount int
ErrorQueryCount int
AvgQPS float64
MaxQPS float64
MinQPS float64
MedianQPS float64
Duration *tachymeter.Metrics
}
func NewReport(rec *Recorder) *Report {
dps := rec.DataPoints()
dpOkLen := len(dps)
if rec.CountAll() == 0 {
return nil
}
nanoElapsed := rec.FinishedAt.Sub(rec.StartedAt)
report := &Report{
ID: rec.ID,
StartedAt: rec.StartedAt,
FinishedAt: rec.FinishedAt,
ElapsedTime: JSONDuration(nanoElapsed),
Options: rec.Options,
GOMAXPROCS: runtime.GOMAXPROCS(0),
QueryCount: rec.CountAll(),
ErrorQueryCount: rec.ErrorQueryCount(),
}
if dpOkLen > 0 {
report.AvgQPS = float64(time.Duration(dpOkLen) * time.Second / nanoElapsed)
t := tachymeter.New(&tachymeter.Config{
Size: dpOkLen,
HBins: 10,
})
for _, v := range dps {
t.AddTime(v.Duration)
}
report.Duration = t.Calc()
qpsSet := NewQPSSet(dps)
report.MinQPS, report.MaxQPS, report.MedianQPS = qpsSet.Stats()
}
return report
}
func (report *Report) JSON() string {
var buf strings.Builder
enc := jsoncolor.NewEncoder(&buf)
if report.Options.Color {
clrs := jsoncolor.DefaultColors()
enc.SetColors(clrs)
}
enc.SetIndent("", " ")
enc.Encode(report) //nolint:errcheck
return strings.TrimSpace(buf.String())
}