Skip to content

Commit

Permalink
Factor out round robin decoding logic from Decoder
Browse files Browse the repository at this point in the history
Fixes #145
  • Loading branch information
tsenart committed May 18, 2018
1 parent a744dc7 commit ed86504
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ func dumpCmd() command {

func dump(dumper, inputs, output string) error {
files := strings.Split(inputs, ",")
srcs := make([]io.Reader, len(files))
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] = in
srcs[i] = vegeta.NewDecoder(in)
}
dec := vegeta.NewDecoder(srcs...)
dec := vegeta.NewRoundRobinDecoder(srcs...)

out, err := file(output, true)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions lib/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ func (rs Results) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
// A Decoder decodes a Result and returns an error in case of failure.
type Decoder func(*Result) error

// NewDecoder returns a new Result decoder closure for the given io.Readers.
// It round robins across the io.Readers on every invocation and decoding error.
func NewDecoder(readers ...io.Reader) Decoder {
dec := make([]*gob.Decoder, len(readers))
for i := range readers {
dec[i] = gob.NewDecoder(readers[i])
}
// NewRoundRobinDecoder returns a new Decoder that round robins across the
// given Decoders on every invocation or decoding error.
func NewRoundRobinDecoder(dec ...Decoder) Decoder {
var seq uint64
return func(r *Result) (err error) {
for range dec {
Expand All @@ -67,6 +63,12 @@ func NewDecoder(readers ...io.Reader) Decoder {
}
}

// NewDecoder returns a new gob Decoder for the given io.Reader.
func NewDecoder(rd io.Reader) Decoder {
dec := gob.NewDecoder(rd)
return func(r *Result) error { return dec.Decode(r) }
}

// Decode is an an adapter method calling the Decoder function itself with the
// given parameters.
func (dec Decoder) Decode(r *Result) error { return dec(r) }
Expand All @@ -77,9 +79,7 @@ type Encoder func(*Result) error
// NewEncoder returns a new Result encoder closure for the given io.Writer
func NewEncoder(r io.Writer) Encoder {
enc := gob.NewEncoder(r)
return func(r *Result) error {
return enc.Encode(r)
}
return func(r *Result) error { return enc.Encode(r) }
}

// Encode is an an adapter method calling the Encoder function itself with the
Expand Down
7 changes: 6 additions & 1 deletion lib/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ func TestDecoding(t *testing.T) {
}

got := make([]uint16, 10)
dec := NewDecoder(&b2, &bytes.Reader{}, &b1)
dec := NewRoundRobinDecoder(
NewDecoder(&b2),
NewDecoder(&bytes.Reader{}),
NewDecoder(&b1),
)

for i := range got {
var r Result
if err := dec(&r); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ func report(reporter, inputs, output string) error {
}

files := strings.Split(inputs, ",")
srcs := make([]io.Reader, len(files))
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] = in
srcs[i] = vegeta.NewDecoder(in)
}
dec := vegeta.NewDecoder(srcs...)
dec := vegeta.NewRoundRobinDecoder(srcs...)

out, err := file(output, true)
if err != nil {
Expand Down

0 comments on commit ed86504

Please sign in to comment.