forked from tsenart/vegeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
88 lines (78 loc) · 1.69 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
package main
import (
"flag"
"io"
"log"
"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]")
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 {
rep, ok := reporters[reporter]
if !ok {
log.Println("Reporter provided is not supported. Using text")
rep = vegeta.ReportText
}
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
}
var reporters = map[string]vegeta.Reporter{
"text": vegeta.ReportText,
"json": vegeta.ReportJSON,
"plot": vegeta.ReportPlot,
}