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:
-input="stdin": Input files (comma separated)
-output="stdout": Output file
-reporter="text": Reporter [text, json, plot:timings]
-reporter="text": Reporter [text, json, plot]
```
#### -input
@@ -145,8 +145,15 @@ Page Not Found
"errors": []
}
```
##### plot:timings
![plot](https://dl.dropboxusercontent.com/u/83217940/plot.svg)
##### plot
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)

View File

@@ -68,7 +68,7 @@ func attack(rate uint64, duration time.Duration, targetsf, ordering, output stri
results := vegeta.Attack(targets, rate, duration)
log.Println("Done!")
log.Printf("Writing results to '%s'...", output)
if err := results.WriteTo(out); err != nil {
if err := results.Encode(out); err != nil {
return err
}
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
import (
"code.google.com/p/plotinum/plot"
"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"
"bytes"
"encoding/json"
"fmt"
"io"
"text/tabwriter"
)
// Reporter represents any function which takes a slice of Results and
// generates a report, writing it to an io.Writer and returning an error
// in case of failure
type Reporter func([]Result, io.Writer) error
// generates a report returned as a slice of bytes and an error in case
// of failure
type Reporter func([]Result) ([]byte, error)
// ReportText writes a computed Metrics struct to out as aligned, formatted text
func ReportText(results []Result, out io.Writer) error {
// ReportText returns a computed Metrics struct as aligned, formatted text
func ReportText(results []Result) ([]byte, error) {
m := NewMetrics(results)
out := &bytes.Buffer{}
w := tabwriter.NewWriter(out, 0, 8, 2, '\t', tabwriter.StripEscape)
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)
}
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
func ReportJSON(results []Result, out io.Writer) error {
return json.NewEncoder(out).Encode(NewMetrics(results))
// ReportJSON writes a computed Metrics struct to as JSON
func ReportJSON(results []Result) ([]byte, error) {
return json.Marshal(NewMetrics(results))
}
// ReportTimingsPlot builds up a plot of the response times of the requests
// in SVG format and writes it to out
func ReportTimingsPlot(results []Result, out io.Writer) error {
p, err := plot.New()
if err != nil {
return err
// ReportPlot builds up a self contained HTML page with an interactive plot
// of the latencies of the requests. Built with http://dygraphs.com/
func ReportPlot(results []Result) ([]byte, error) {
out := &bytes.Buffer{}
for _, result := range results {
fmt.Fprintf(out, "[%f,%f],",
result.Timestamp.Sub(results[0].Timestamp).Seconds(),
result.Timing.Seconds()*1000,
)
}
pts := make(plotter.XYs, len(results))
for i := 0; i < len(pts); i++ {
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
out.Truncate(out.Len() - 1) // Remove trailing comma
return []byte(fmt.Sprintf(plotsTemplate, dygraphJSLibSrc(), out)), nil
}
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
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
func (r Results) WriteTo(out io.Writer) error {
func (r Results) Encode(out io.Writer) error {
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
func (r *Results) ReadFrom(in io.Reader) error {
func (r *Results) Decode(in io.Reader) error {
return gob.NewDecoder(in).Decode(r)
}

View File

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

View File

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