-
Notifications
You must be signed in to change notification settings - Fork 13
/
io.go
156 lines (141 loc) · 3.4 KB
/
io.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
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
"github.com/olekukonko/tablewriter"
"github.com/spf13/pflag"
"go.bobheadxi.dev/gobenchdata/bench"
"go.bobheadxi.dev/gobenchdata/checks"
)
func statusEmoji(s checks.Status) string {
switch s {
case checks.StatusPass:
return "✅"
case checks.StatusFail:
return "❌"
case checks.StatusNotFound:
return "❓"
default:
return "🤷♂️"
}
}
func newTable(out io.Writer) *tablewriter.Table {
t := tablewriter.NewWriter(out)
t.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
t.SetCenterSeparator("|")
return t
}
func output(results []bench.Run) {
if !*noSort {
sort.Sort(bench.RunHistory(results))
}
if *prune > 0 && len(results) > *prune {
results = results[:*prune]
}
var b []byte
var err error
if *flat {
b = make([]byte, 0)
b = append(b, '[')
for i, run := range results {
runBytes, err := json.Marshal(run)
if err != nil {
panic(err)
}
b = append(b, '\n', ' ', ' ')
b = append(b, runBytes...)
if i != (len(results) - 1) {
b = append(b, ',')
}
}
b = append(b, '\n', ']', '\n')
} else {
b, err = json.MarshalIndent(results, "", " ")
if err != nil {
panic(err)
}
}
if *jsonOut != "" {
if err := os.WriteFile(*jsonOut, b, os.ModePerm); err != nil {
panic(err)
}
fmt.Printf("successfully output results as json to '%s'\n", *jsonOut)
} else {
fmt.Println(string(b))
}
}
func outputChecksReport(r *checks.Report) {
// overview conters
var (
checksPassed int
checksFailed int
checksTotal int
)
results := newTable(os.Stdout)
results.SetHeader([]string{"", "Check", "Package", "Benchmark", "Diff", "Comment"})
for checkName, check := range r.Checks {
checksTotal++
switch check.Status {
case checks.StatusPass:
checksPassed++
case checks.StatusFail:
checksFailed++
}
t := check.Thresholds
for _, bench := range check.Diffs {
var reason string
if t.Min != nil && bench.Value < *t.Min {
reason = fmt.Sprintf("exceeded minimum %f (-%.2f)", *t.Min, *t.Min-bench.Value)
} else if t.Max != nil && bench.Value > *t.Max {
reason = fmt.Sprintf("exceeded maximum %f (+%.2f)", *t.Max, bench.Value-*t.Max)
}
results.Append([]string{
statusEmoji(bench.Status), checkName, bench.Package, bench.Benchmark,
fmt.Sprintf("%.2f", bench.Value), reason,
})
}
if len(check.Diffs) == 0 {
results.Append([]string{
statusEmoji(checks.StatusNotFound), checkName, "", "",
"", "no benchmarks found",
})
}
}
meta := newTable(os.Stdout)
meta.SetHeader([]string{"", "Base", "Current", "Passed Checks", "Failed Checks", "Total"})
meta.Append([]string{
statusEmoji(r.Status), r.Base, r.Current, fmt.Sprintf("%d", checksPassed),
fmt.Sprintf("%d", checksFailed), fmt.Sprintf("%d", checksTotal),
})
// output results to stdout
meta.Render()
fmt.Println()
results.Render()
fmt.Println()
}
func load(files ...string) []bench.RunHistory {
hist := []bench.RunHistory{}
for _, f := range files {
b, err := os.ReadFile(f)
if err != nil {
panic(err)
}
var runs bench.RunHistory
if err := json.Unmarshal(b, &runs); err != nil {
panic(err)
}
hist = append(hist, runs)
}
return hist
}
func showHelp() {
fmt.Println(helpText)
fmt.Println()
fmt.Println("FLAGS:")
fmt.Println()
pflag.PrintDefaults()
fmt.Println("\nSee https://github.com/bobheadxi/gobenchdata for more documentation.")
}