Improve Reporter interface
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package vegeta
|
package vegeta
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"code.google.com/p/plotinum/plot"
|
"code.google.com/p/plotinum/plot"
|
||||||
"code.google.com/p/plotinum/plotter"
|
"code.google.com/p/plotinum/plotter"
|
||||||
"code.google.com/p/plotinum/plotutil"
|
"code.google.com/p/plotinum/plotutil"
|
||||||
@@ -8,18 +9,18 @@ import (
|
|||||||
"code.google.com/p/plotinum/vg/vgsvg"
|
"code.google.com/p/plotinum/vg/vgsvg"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Reporter represents any function which takes a slice of Results and
|
// Reporter represents any function which takes a slice of Results and
|
||||||
// generates a report, writing it to an io.Writer and returning an error
|
// generates a report returned as a slice of bytes and an error in case
|
||||||
// in case of failure
|
// of failure
|
||||||
type Reporter func([]Result, io.Writer) error
|
type Reporter func([]Result) ([]byte, error)
|
||||||
|
|
||||||
// ReportText writes a computed Metrics struct to out as aligned, formatted text
|
// ReportText returns a computed Metrics struct as aligned, formatted text
|
||||||
func ReportText(results []Result, out io.Writer) error {
|
func ReportText(results []Result) ([]byte, error) {
|
||||||
m := NewMetrics(results)
|
m := NewMetrics(results)
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
|
||||||
w := tabwriter.NewWriter(out, 0, 8, 2, '\t', tabwriter.StripEscape)
|
w := tabwriter.NewWriter(out, 0, 8, 2, '\t', tabwriter.StripEscape)
|
||||||
fmt.Fprintf(w, "Time(avg)\tRequests\tSuccess\tBytes(rx/tx)\n")
|
fmt.Fprintf(w, "Time(avg)\tRequests\tSuccess\tBytes(rx/tx)\n")
|
||||||
@@ -39,20 +40,23 @@ func ReportText(results []Result, out io.Writer) error {
|
|||||||
fmt.Fprintln(w, err)
|
fmt.Fprintln(w, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return w.Flush()
|
if err := w.Flush(); err != nil {
|
||||||
|
return []byte{}, err
|
||||||
|
}
|
||||||
|
return out.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReportJSON writes a computed Metrics struct to out as JSON
|
// ReportJSON writes a computed Metrics struct to as JSON
|
||||||
func ReportJSON(results []Result, out io.Writer) error {
|
func ReportJSON(results []Result) ([]byte, error) {
|
||||||
return json.NewEncoder(out).Encode(NewMetrics(results))
|
return json.Marshal(NewMetrics(results))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReportTimingsPlot builds up a plot of the response times of the requests
|
// ReportTimingsPlot builds up a plot of the response times of the requests
|
||||||
// in SVG format and writes it to out
|
// in SVG format and returns it
|
||||||
func ReportTimingsPlot(results []Result, out io.Writer) error {
|
func ReportTimingsPlot(results []Result) ([]byte, error) {
|
||||||
p, err := plot.New()
|
p, err := plot.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
pts := make(plotter.XYs, len(results))
|
pts := make(plotter.XYs, len(results))
|
||||||
for i := 0; i < len(pts); i++ {
|
for i := 0; i < len(pts); i++ {
|
||||||
@@ -62,7 +66,7 @@ func ReportTimingsPlot(results []Result, out io.Writer) error {
|
|||||||
|
|
||||||
line, err := plotter.NewLine(pts)
|
line, err := plotter.NewLine(pts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
line.Color = plotutil.Color(1)
|
line.Color = plotutil.Color(1)
|
||||||
|
|
||||||
@@ -76,6 +80,9 @@ func ReportTimingsPlot(results []Result, out io.Writer) error {
|
|||||||
canvas := vgsvg.New(w, h)
|
canvas := vgsvg.New(w, h)
|
||||||
p.Draw(plot.MakeDrawArea(canvas))
|
p.Draw(plot.MakeDrawArea(canvas))
|
||||||
|
|
||||||
_, err = canvas.WriteTo(out)
|
out := &bytes.Buffer{}
|
||||||
return err
|
if _, err = canvas.WriteTo(out); err != nil {
|
||||||
|
return []byte{}, err
|
||||||
|
}
|
||||||
|
return out.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ func report(reporter, input, output string) error {
|
|||||||
}
|
}
|
||||||
all = append(all, results...)
|
all = append(all, results...)
|
||||||
}
|
}
|
||||||
|
all.Sort()
|
||||||
|
|
||||||
out, err := file(output, true)
|
out, err := file(output, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -55,7 +56,9 @@ func report(reporter, input, output string) error {
|
|||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
if err := rep(all.Sort(), out); err != nil {
|
if data, err := rep(all); err != nil {
|
||||||
|
return err
|
||||||
|
} else if _, err := out.Write(data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user