forked from tsenart/vegeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
65 lines (57 loc) · 1.24 KB
/
dump.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
package main
import (
"flag"
"fmt"
"io"
"strings"
vegeta "github.com/tsenart/vegeta/lib"
)
func dumpCmd() command {
fs := flag.NewFlagSet("vegeta dump", flag.ExitOnError)
dumper := fs.String("dumper", "json", "Dumper [json, csv]")
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 dump(*dumper, *inputs, *output)
}}
}
func dump(dumper, inputs, output string) error {
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
}
dec := vegeta.NewDecoder(srcs...)
out, err := file(output, true)
if err != nil {
return err
}
defer out.Close()
var enc vegeta.Encoder
switch dumper {
case "csv":
enc = vegeta.NewCSVEncoder(out)
case "json":
enc = vegeta.NewJSONEncoder(out)
default:
return fmt.Errorf("unsupported dumper: %s", dumper)
}
for {
var r vegeta.Result
if err = dec.Decode(&r); err != nil {
if err == io.EOF {
break
}
return err
} else if err = enc.Encode(&r); err != nil {
return err
}
}
return nil
}