forked from tsenart/vegeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
98 lines (89 loc) · 1.89 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"sort"
"strings"
vegeta "github.com/tsenart/vegeta/lib"
)
func reportCmd() command {
fs := flag.NewFlagSet("vegeta report", flag.ExitOnError)
reporter := fs.String("reporter", "text", "Reporter [text, json, plot, hist[buckets]]")
inputs := fs.String("inputs", "stdin", "Input files (comma separated)")
output := fs.String("output", "stdout", "Output file")
return command{fs, func(args []string) error {
fs.Parse(args)
return report(*reporter, *inputs, *output)
}}
}
// report validates the report arguments, sets up the required resources
// and writes the report
func report(reporter, inputs, output string) error {
if len(reporter) < 4 {
return fmt.Errorf("bad reporter: %s", reporter)
}
var rep vegeta.Reporter
switch reporter[:4] {
case "text":
rep = vegeta.ReportText
case "json":
rep = vegeta.ReportJSON
case "plot":
rep = vegeta.ReportPlot
case "hist":
if len(reporter) < 6 {
return fmt.Errorf("bad buckets: '%s'", reporter[4:])
}
var hist vegeta.HistogramReporter
if err := hist.Set(reporter[4:]); err != nil {
return err
}
rep = hist
}
files := strings.Split(inputs, ",")
srcs := make([]io.Reader, len(files))
for i, f := range files {
in, err := file(f, false)
if err != nil {
return err
}
defer in.Close()
srcs[i] = in
}
out, err := file(output, true)
if err != nil {
return err
}
defer out.Close()
var results vegeta.Results
res, errs := vegeta.Collect(srcs...)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
outer:
for {
select {
case _ = <-sig:
break outer
case r, ok := <-res:
if !ok {
break outer
}
results = append(results, r)
case err, ok := <-errs:
if !ok {
break outer
}
return err
}
}
sort.Sort(results)
data, err := rep.Report(results)
if err != nil {
return err
}
_, err = out.Write(data)
return err
}