forked from bobheadxi/gobenchdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
260 lines (227 loc) · 6.49 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/spf13/pflag"
"go.bobheadxi.dev/gobenchdata/bench"
"go.bobheadxi.dev/gobenchdata/checks"
"go.bobheadxi.dev/gobenchdata/internal"
"go.bobheadxi.dev/gobenchdata/web"
)
// Version is the version of gobenchdata
var Version string
var (
jsonOut = pflag.String("json", "", "output as json to file")
appendOut = pflag.BoolP("append", "a", false, "append to output file")
flat = pflag.BoolP("flat", "f", false, "flatten JSON output")
noSort = pflag.Bool("no-sort", false, "disable sorting")
prune = pflag.Int("prune", 0, "number of runs to keep (default: keep all)")
webConfigOnly = pflag.Bool("web.config-only", false, "only generate configuration for 'gobenchdata web'")
webIndexTitle = pflag.String("web.title", "gobenchdata web", "header <title> for 'gobenchdata web'")
webIndexHead = pflag.StringArray("web.head", []string{}, "additional <head> elements for 'gobenchdata web'")
checksPretty = pflag.Bool("checks.pretty", false, "output report as human-readable format instead of JSON")
checksConfigPath = pflag.String("checks.config", "gobenchdata-checks.yml", "path to checks configuraton file")
version = pflag.StringP("version", "v", "", "version to tag in your benchmark output")
tags = pflag.StringArrayP("tag", "t", nil, "array of tags to include in result")
)
const helpText = `gobenchdata is a tool for inspecting golang benchmark outputs.
BASIC USAGE:
go test -bench . -benchmem ./... | gobenchdata [flags]
COMMANDS:
merge [files] merge gobenchdata results
web generate [directory] generate web application in directory
web serve [port] serve web application using './gobenchdata-config.yml'
checks generate generate checks configuration
checks eval [base] [current] evaluate checks defined in './gobenchdata-checks.yml'
checks report [report] prints a simple report and exits with status 1 if a check failed
version show gobenchdata version
help show help text
`
func main() {
pflag.Parse()
// run command if provided
if len(pflag.Args()) > 0 {
switch cmd := pflag.Args()[0]; cmd {
// gobenchdata version
case "version":
if Version == "" {
println("gobenchdata version unknown")
} else {
println("gobenchdata " + Version)
}
// gobenchdata help
case "help":
showHelp()
os.Exit(0)
// gobenchdata merge
case "merge":
args := pflag.Args()[1:]
if len(args) < 1 {
panic("no merge targets provided")
}
merge(args...)
// gobenchdata web
case "web":
if len(pflag.Args()) < 2 {
showHelp()
os.Exit(1)
}
flags := web.Flags{
Title: *webIndexTitle,
Headers: *webIndexHead,
}
switch webCmd := pflag.Args()[1]; webCmd {
case "generate":
if len(pflag.Args()) < 3 {
panic("no output directory provided")
}
dir := pflag.Args()[2]
config, it := web.LoadDefaults(dir, flags)
if !*webConfigOnly {
if err := web.GenerateApp(dir, *it); err != nil {
panic(err)
}
println("web application generated!")
}
// only override if we are generating config only
if err := web.GenerateConfig(dir, *config, *webConfigOnly); err != nil {
if errors.Is(err, os.ErrExist) {
println("found existing web app configuration")
return
}
panic(err)
}
println("web application configuration generated!")
case "serve":
port := "8080"
if len(pflag.Args()) == 3 {
port = pflag.Args()[2]
}
config, it := web.LoadDefaults(".", flags)
addr := "localhost:" + port
fmt.Printf("serving './benchmarks.json' on '%s'\n", addr)
go internal.OpenBrowser("http://" + addr)
if err := web.ListenAndServe(addr, *config, *it); err != nil {
panic(err)
}
default:
showHelp()
os.Exit(1)
}
// gobenchdata checks
case "checks":
if len(pflag.Args()) < 2 {
showHelp()
os.Exit(1)
}
switch checksCmd := pflag.Args()[1]; checksCmd {
case "generate":
if err := checks.GenerateConfig(*checksConfigPath); err != nil {
panic(err)
}
case "eval":
cfg, err := checks.LoadConfig(*checksConfigPath)
if err != nil {
panic(err)
}
args := pflag.Args()[2:]
if len(args) != 2 {
panic("two targets required")
}
histories := load(args[0], args[1])
res, err := checks.Evaluate(cfg.Checks, histories[0], histories[1], &checks.EvaluateOptions{
Debug: false,
MustFindAll: false,
})
if err != nil {
panic(err)
}
// output report appropriately
if *checksPretty {
outputChecksReport(res)
} else {
var b []byte
if *flat {
b, err = json.Marshal(res)
} else {
b, err = json.MarshalIndent(res, "", " ")
}
if err != nil {
panic(err)
}
if *jsonOut == "" {
println(string(b))
} else {
if err := ioutil.WriteFile(*jsonOut, b, os.ModePerm); err != nil {
panic(err)
}
fmt.Printf("report output written to %s\n", *jsonOut)
}
}
case "report":
if len(pflag.Args()) < 3 {
panic("no report provided")
}
results, err := checks.LoadReport(pflag.Args()[2])
if err != nil {
panic(err)
}
outputChecksReport(results)
// exit with code corresponding to status
if results.Status != checks.StatusPass {
os.Exit(1)
}
default:
showHelp()
os.Exit(1)
}
default:
showHelp()
os.Exit(1)
}
return
}
// default behaviour
fi, err := os.Stdin.Stat()
if err != nil {
panic(err)
} else if fi.Mode()&os.ModeNamedPipe == 0 {
panic("gobenchdata should be used with a pipe - see 'gobenchdata help'")
}
parser := bench.NewParser(bufio.NewReader(os.Stdin))
suites, err := parser.Read()
if err != nil {
panic(err)
}
fmt.Printf("detected %d benchmark suites\n", len(suites))
// set up results
results := []bench.Run{{
Version: *version,
Date: time.Now().Unix(),
Tags: *tags,
Suites: suites,
}}
if *appendOut {
if *jsonOut == "" {
panic("file output needs to be set (try '--json')")
}
b, err := ioutil.ReadFile(*jsonOut)
if err != nil && !os.IsNotExist(err) {
panic(err)
} else if !os.IsNotExist(err) {
var runs []bench.Run
if err := json.Unmarshal(b, &runs); err != nil {
panic(err)
}
results = append(results, runs...)
} else {
fmt.Printf("could not find specified output file '%s' - creating a new file\n", *jsonOut)
}
}
output(results)
}