-
Notifications
You must be signed in to change notification settings - Fork 41
/
report.go
77 lines (63 loc) · 1.52 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
package main
import (
"flag"
"log"
"strings"
stress "github.com/buaazp/stress/lib"
)
func reportCmd() command {
fs := flag.NewFlagSet("stress report", flag.ExitOnError)
opts := &reportOpts{}
fs.StringVar(&opts.reporter, "reporter", "text", "Reporter [text, json, plot]")
fs.StringVar(&opts.inputf, "input", "stdin", "Input files (comma separated)")
fs.StringVar(&opts.outputf, "output", "stdout", "Output file")
return command{fs, func(args []string) error {
fs.Parse(args)
return report(opts)
}}
}
// reportOpts aggregates the report function command options
type reportOpts struct {
reporter string
inputf string
outputf string
}
// report validates the report arguments, sets up the required resources
// and writes the report
func report(opts *reportOpts) error {
rep, ok := reporters[opts.reporter]
if !ok {
log.Println("Reporter provided is not supported. Using text")
rep = stress.ReportText
}
var all stress.Results
for _, input := range strings.Split(opts.inputf, ",") {
in, err := file(input, false)
if err != nil {
return err
}
var results stress.Results
if err = results.Decode(in); err != nil {
return err
}
in.Close()
all = append(all, results...)
}
all.Sort()
out, err := file(opts.outputf, true)
if err != nil {
return err
}
defer out.Close()
data, err := rep(all)
if err != nil {
return err
}
_, err = out.Write(data)
return err
}
var reporters = map[string]stress.Reporter{
"text": stress.ReportText,
"json": stress.ReportJSON,
"plot": stress.ReportPlot,
}