ReporterPlot: Interactive plot with HTML5

* Uses Dygraphs JS lib to plot
* Embeds dygraphs in binary to remove run-time dependencies
* Removes dependency on Plotinum
This commit is contained in:
Tomás Senart
2013-09-23 01:37:10 +02:00
parent f0579e67e4
commit 0e268023aa
3 changed files with 3484 additions and 40 deletions

3441
lib/dygraph.js.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -2,11 +2,6 @@ package vegeta
import (
"bytes"
"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"
"encoding/json"
"fmt"
"text/tabwriter"
@@ -51,38 +46,46 @@ 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 returns it
func ReportTimingsPlot(results []Result) ([]byte, error) {
p, err := plot.New()
if err != nil {
return []byte{}, err
}
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 []byte{}, 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))
// 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{}
if _, err = canvas.WriteTo(out); err != nil {
return []byte{}, err
for _, result := range results {
fmt.Fprintf(out, "[%f,%f],",
result.Timestamp.Sub(results[0].Timestamp).Seconds(),
result.Timing.Seconds()*1000,
)
}
return out.Bytes(), nil
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

@@ -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