forked from tsenart/vegeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
102 lines (90 loc) · 2.16 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
99
100
101
102
package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"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, 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)
}
files := strings.Split(inputs, ",")
srcs := make([]vegeta.Decoder, len(files))
for i, f := range files {
in, err := file(f, false)
if err != nil {
return err
}
defer in.Close()
srcs[i] = vegeta.NewDecoder(in)
}
dec := vegeta.NewRoundRobinDecoder(srcs...)
out, err := file(output, true)
if err != nil {
return err
}
defer out.Close()
var (
rep vegeta.Reporter
report vegeta.Report
)
switch reporter[:4] {
case "plot":
return fmt.Errorf("The plot reporter has been deprecated and succeeded by the vegeta plot command")
case "text":
var m vegeta.Metrics
rep, report = vegeta.NewTextReporter(&m), &m
case "json":
var m vegeta.Metrics
rep, report = vegeta.NewJSONReporter(&m), &m
case "hist":
if len(reporter) < 6 {
return fmt.Errorf("bad buckets: '%s'", reporter[4:])
}
var hist vegeta.Histogram
if err := hist.Buckets.UnmarshalText([]byte(reporter[4:])); err != nil {
return err
}
rep, report = vegeta.NewHistogramReporter(&hist), &hist
default:
return fmt.Errorf("unknown reporter: %q", reporter)
}
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
decode:
for {
select {
case <-sigch:
break decode
default:
var r vegeta.Result
if err = dec.Decode(&r); err != nil {
if err == io.EOF {
break decode
}
return err
}
report.Add(&r)
}
}
if c, ok := report.(vegeta.Closer); ok {
c.Close()
}
return rep.Report(out)
}