-
Notifications
You must be signed in to change notification settings - Fork 45
/
out.go
127 lines (101 loc) · 2.54 KB
/
out.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
package main
import (
"encoding/json"
"fmt"
"math"
"os"
"strconv"
"strings"
"text/tabwriter"
"time"
"github.com/TylerBrock/colorjson"
"github.com/logrusorgru/aurora"
"github.com/rcrowley/go-metrics"
)
var (
TypeColor = aurora.White
TitleColor = aurora.Cyan
Formatter = colorjson.NewFormatter()
w = tabwriter.NewWriter(os.Stderr, 0, 2, 2, ' ', 0)
)
func init() {
Formatter.Indent = 4
}
func MarshalToMap(obj interface{}) interface{} {
var b []byte
switch v := obj.(type) {
case []byte:
b = v
default:
bytes, err := json.Marshal(obj)
if err != nil {
panic(err)
}
b = bytes
}
var ret interface{}
if err := json.Unmarshal(b, &ret); err != nil {
panic(err)
}
return ret
}
func PrintColoredJSON(msg string, obj interface{}) {
obj = MarshalToMap(obj)
PrintTitle(msg)
b, err := Formatter.Marshal(obj)
if err != nil {
panic(err)
}
fmt.Println()
fmt.Println(string(b))
fmt.Println()
}
func PrintTitle(name string) {
fmt.Fprintln(w, aurora.Bold(TitleColor(name)))
}
func Print(name string, v1 interface{}) {
printRow(TypeColor(name), v1)
}
func printRow(name aurora.Value, v1 interface{}) {
_, _ = fmt.Fprintf(w, "\t%s\t%v\t\n", name, v1)
}
func printSubRow(name aurora.Value, v1 interface{}) {
_, _ = fmt.Fprintf(w, "\t %s\t%v\t\n", name, v1)
}
func PrintPercentiles(t metrics.Timer, p ...float64) {
Print("Percentiles:", "")
for _, percentile := range p {
PrintPercentile(percentile, t)
}
}
func PrintPercentile(percentile float64, timer metrics.Timer) {
percentileInt := percentile * 100
strValue := strconv.FormatFloat(percentileInt, 'f', 2, 64) + "% :"
printSubRow(aurora.White(strValue), time.Duration(timer.Percentile(percentile)))
}
func PrintRate(t metrics.Timer) {
Print("Rate:", "")
printSubRow(aurora.White("1 Minute:"), t.Rate1())
printSubRow(aurora.White("5 Minute:"), t.Rate5())
printSubRow(aurora.White("15 Minute:"), t.Rate15())
printSubRow(aurora.White("Mean:"), t.RateMean())
}
func PrintMetrics(action string, timer metrics.Timer) {
if timer.Count() == 0 {
if opts.ShowAll {
fmt.Println()
fmt.Println(aurora.Bold(aurora.Cyan(strings.Title(action))), aurora.Red(" Not run."))
}
return
}
PrintTitle(strings.Title(action))
Print("Mean:", time.Duration(timer.Mean()))
Print("Total:", timer.Count())
Print("Max:", time.Duration(timer.Max()))
Print("Min:", time.Duration(timer.Min()))
Print("Variance:", time.Duration(math.Round(timer.Variance()/float64(timer.Count()))))
PrintRate(timer)
PrintPercentiles(timer, 0.5, 0.75, 0.8, 0.9, 0.95, 0.99, 0.999)
w.Flush()
fmt.Println()
}