/
hdrreport.go
67 lines (53 loc) · 1.24 KB
/
hdrreport.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
package hdrreport
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/codahale/hdrhistogram"
)
// Quantiles contains common latency quantiles (p50, p95, p999)
type Quantiles struct {
Quantile50 int64 `json:"p50"`
Quantile75 int64 `json:"p75"`
Quantile90 int64 `json:"p90"`
Quantile95 int64 `json:"p95"`
Quantile99 int64 `json:"p99"`
Quantile999 int64 `json:"p999"`
}
func WriteReportCSV(filename *string, hist *hdrhistogram.Histogram) error {
f, err := os.Create(*filename)
if err != nil {
return err
}
for _, bar := range hist.Distribution() {
_, err := f.Write([]byte(bar.String()))
if err != nil {
return err
}
}
err = f.Sync()
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return nil
}
func PrintLatencySummary(hist *hdrhistogram.Histogram) {
latency := Quantiles{
Quantile50: hist.ValueAtQuantile(50),
Quantile75: hist.ValueAtQuantile(75),
Quantile90: hist.ValueAtQuantile(90),
Quantile95: hist.ValueAtQuantile(95),
Quantile99: hist.ValueAtQuantile(99),
Quantile999: hist.ValueAtQuantile(999),
}
if data, err := json.MarshalIndent(latency, "", " "); err != nil {
log.Fatal("Unable to generate report: ", err)
} else {
fmt.Println(string(data))
}
}