Merge pull request #26 from tsenart/better-plotting

Better plotting
This commit is contained in:
Tomás Senart
2013-09-22 17:09:38 -07:00
7 changed files with 3521 additions and 60 deletions

View File

@@ -100,7 +100,7 @@ $ vegeta report -h
Usage of report: Usage of report:
-input="stdin": Input files (comma separated) -input="stdin": Input files (comma separated)
-output="stdout": Output file -output="stdout": Output file
-reporter="text": Reporter [text, json, plot:timings] -reporter="text": Reporter [text, json, plot]
``` ```
#### -input #### -input
@@ -145,8 +145,15 @@ Page Not Found
"errors": [] "errors": []
} }
``` ```
##### plot:timings ##### plot
![plot](https://dl.dropboxusercontent.com/u/83217940/plot.svg) Generates an HTML5 page with an interactive plot based on
[Dygraphs](http://dygraphs.com).
Click and drag to select a region to zoom into. Double click to zoom
out.
Input a different number on the bottom left corner input field
to change the moving average window size (in data points).
![Plot](https://dl.dropboxusercontent.com/u/83217940/plot.png)
## Usage (Library) ## Usage (Library)

View File

@@ -68,7 +68,7 @@ func attack(rate uint64, duration time.Duration, targetsf, ordering, output stri
results := vegeta.Attack(targets, rate, duration) results := vegeta.Attack(targets, rate, duration)
log.Println("Done!") log.Println("Done!")
log.Printf("Writing results to '%s'...", output) log.Printf("Writing results to '%s'...", output)
if err := results.WriteTo(out); err != nil { if err := results.Encode(out); err != nil {
return err return err
} }
return nil return nil

3441
lib/dygraph.js.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,25 +1,21 @@
package vegeta package vegeta
import ( import (
"code.google.com/p/plotinum/plot" "bytes"
"code.google.com/p/plotinum/plotter"
"code.google.com/p/plotinum/plotutil"
"code.google.com/p/plotinum/vg"
"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,43 +35,57 @@ 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 // ReportPlot builds up a self contained HTML page with an interactive plot
// in SVG format and writes it to out // of the latencies of the requests. Built with http://dygraphs.com/
func ReportTimingsPlot(results []Result, out io.Writer) error { func ReportPlot(results []Result) ([]byte, error) {
p, err := plot.New() out := &bytes.Buffer{}
if err != nil { for _, result := range results {
return err fmt.Fprintf(out, "[%f,%f],",
result.Timestamp.Sub(results[0].Timestamp).Seconds(),
result.Timing.Seconds()*1000,
)
} }
pts := make(plotter.XYs, len(results)) out.Truncate(out.Len() - 1) // Remove trailing comma
for i := 0; i < len(pts); i++ { return []byte(fmt.Sprintf(plotsTemplate, dygraphJSLibSrc(), out)), nil
pts[i].X = results[i].Timestamp.Sub(results[0].Timestamp).Seconds()
pts[i].Y = results[i].Timing.Seconds() * 1000
}
line, err := plotter.NewLine(pts)
if err != nil {
return err
}
line.Color = plotutil.Color(1)
p.Add(line)
p.X.Padding = vg.Length(3.0)
p.X.Label.Text = "Time elapsed"
p.Y.Padding = vg.Length(3.0)
p.Y.Label.Text = "Latency (ms)"
w, h := vg.Millimeters(float64(len(results))), vg.Centimeters(12.0)
canvas := vgsvg.New(w, h)
p.Draw(plot.MakeDrawArea(canvas))
_, err = canvas.WriteTo(out)
return err
} }
var plotsTemplate = `<!doctype>
<html>
<head>
<title>Vegeta Plots</title>
</head>
<body>
<div id="latencies" style="font-family: Courier; width: 100%%; height: 600px"></div>
<script>
%s
</script>
<script>
new Dygraph(
document.getElementById("latencies"),
[%s],
{
title: 'Vegeta Plot',
labels: ['Seconds', 'Latency (ms)'],
ylabel: 'Latency (ms)',
xlabel: 'Seconds elapsed',
showRoller: true,
colors: ['#8AE234'],
fillGraph: true,
legend: 'always',
logscale: true
}
);
</script>
</body>
</html>`

View File

@@ -22,15 +22,15 @@ type Result struct {
// decoding and sorting behavior attached // decoding and sorting behavior attached
type Results []Result type Results []Result
// WriteTo encodes the results and writes it to an io.Writer // Encode encodes the results and writes it to an io.Writer
// returning an error in case of failure // returning an error in case of failure
func (r Results) WriteTo(out io.Writer) error { func (r Results) Encode(out io.Writer) error {
return gob.NewEncoder(out).Encode(r) return gob.NewEncoder(out).Encode(r)
} }
// ReadFrom reads data from an io.Reader and decodes it into a Results struct // Decode reads data from an io.Reader and decodes it into a Results struct
// returning an error in case of failure // returning an error in case of failure
func (r *Results) ReadFrom(in io.Reader) error { func (r *Results) Decode(in io.Reader) error {
return gob.NewDecoder(in).Decode(r) return gob.NewDecoder(in).Decode(r)
} }

View File

@@ -15,12 +15,12 @@ func TestEncoding(t *testing.T) {
} }
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
if err := results.WriteTo(buffer); err != nil { if err := results.Encode(buffer); err != nil {
t.Fatalf("Failed WriteTo: %s", err) t.Fatalf("Failed WriteTo: %s", err)
} }
decoded := Results{} decoded := Results{}
if err := decoded.ReadFrom(buffer); err != nil { if err := decoded.Decode(buffer); err != nil {
t.Fatalf("Failed ReadFrom: %s", err) t.Fatalf("Failed ReadFrom: %s", err)
} }

View File

@@ -9,7 +9,7 @@ import (
func reportCmd(args []string) command { func reportCmd(args []string) command {
fs := flag.NewFlagSet("report", flag.ExitOnError) fs := flag.NewFlagSet("report", flag.ExitOnError)
reporter := fs.String("reporter", "text", "Reporter [text, json, plot:timings]") reporter := fs.String("reporter", "text", "Reporter [text, json, plot]")
input := fs.String("input", "stdin", "Input files (comma separated)") input := fs.String("input", "stdin", "Input files (comma separated)")
output := fs.String("output", "stdout", "Output file") output := fs.String("output", "stdout", "Output file")
fs.Parse(args) fs.Parse(args)
@@ -28,8 +28,8 @@ func report(reporter, input, output string) error {
rep = vegeta.ReportText rep = vegeta.ReportText
case "json": case "json":
rep = vegeta.ReportJSON rep = vegeta.ReportJSON
case "plot:timings": case "plot":
rep = vegeta.ReportTimingsPlot rep = vegeta.ReportPlot
default: default:
log.Println("Reporter provided is not supported. Using text") log.Println("Reporter provided is not supported. Using text")
rep = vegeta.ReportText rep = vegeta.ReportText
@@ -43,11 +43,12 @@ func report(reporter, input, output string) error {
} }
defer in.Close() defer in.Close()
results := vegeta.Results{} results := vegeta.Results{}
if err := results.ReadFrom(in); err != nil { if err := results.Decode(in); err != nil {
return err return err
} }
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
} }