Skip to content

Commit

Permalink
Simplify Reporter objects to be simple functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomás Senart committed Sep 9, 2013
1 parent f689362 commit 9331099
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 185 deletions.
12 changes: 11 additions & 1 deletion lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"io/ioutil"
"net/http"
"sort"
"time"
)

Expand All @@ -14,7 +15,7 @@ func Attack(targets Targets, rate uint64, duration time.Duration) []Result {
total := rate * uint64(duration.Seconds())
hits := make(chan *http.Request, total)
res := make(chan Result, total)
results := make([]Result, total)
results := make(Results, total)
// Scatter
go drill(rate, hits, res)
for i := 0; i < cap(hits); i++ {
Expand All @@ -27,6 +28,8 @@ func Attack(targets Targets, rate uint64, duration time.Duration) []Result {
}
close(res)

sort.Sort(results)

return results
}

Expand All @@ -40,6 +43,13 @@ type Result struct {
Error error
}

// Results is a slice of Result defined only to be sortable with sort.Interface
type Results []Result

func (r Results) Len() int { return len(r) }
func (r Results) Less(i, j int) bool { return r[i].Timestamp.Before(r[j].Timestamp) }
func (r Results) Swap(i, j int) { r[i], r[j] = r[j], r[i] }

// drill loops over the passed reqs channel and executes each request.
// It is throttled to the rate specified.
func drill(rate uint64, reqs chan *http.Request, res chan Result) {
Expand Down
11 changes: 0 additions & 11 deletions lib/reporter.go

This file was deleted.

102 changes: 102 additions & 0 deletions lib/reporters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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"
"fmt"
"io"
"text/tabwriter"
"time"
)

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

// ReportText computes and prints some metrics out of results
// as formatted text. Metrics include avg time per request, success ratio,
// total number of request, avg bytes in and avg bytes out.
func ReportText(results []Result, out io.Writer) error {
totalRequests := float64(len(results))
totalTime := time.Duration(0)
totalBytesOut := uint64(0)
totalBytesIn := uint64(0)
totalSuccess := uint64(0)
histogram := map[uint64]uint64{}
errors := map[string]struct{}{}

for _, res := range results {
histogram[res.Code]++
totalTime += res.Timing
totalBytesOut += res.BytesOut
totalBytesIn += res.BytesIn
if res.Code >= 200 && res.Code < 300 {
totalSuccess++
}
if res.Error != nil {
errors[res.Error.Error()] = struct{}{}
}
}

avgTime := time.Duration(float64(totalTime) / totalRequests)
avgBytesOut := float64(totalBytesOut) / totalRequests
avgBytesIn := float64(totalBytesIn) / totalRequests
avgSuccess := float64(totalSuccess) / totalRequests

w := tabwriter.NewWriter(out, 0, 8, 2, '\t', tabwriter.StripEscape)
fmt.Fprintf(w, "Time(avg)\tRequests\tSuccess\tBytes(rx/tx)\n")
fmt.Fprintf(w, "%s\t%d\t%.2f%%\t%.2f/%.2f\n", avgTime, int(totalRequests), avgSuccess*100, avgBytesIn, avgBytesOut)

fmt.Fprintf(w, "\nCount:\t")
for _, count := range histogram {
fmt.Fprintf(w, "%d\t", count)
}
fmt.Fprintf(w, "\nStatus:\t")
for code, _ := range histogram {
fmt.Fprintf(w, "%d\t", code)
}

fmt.Fprintln(w, "\n\nError Set:")
for err, _ := range errors {
fmt.Fprintln(w, err)
}

return w.Flush()
}

// 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
}
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
}
76 changes: 0 additions & 76 deletions lib/text_reporter.go

This file was deleted.

90 changes: 0 additions & 90 deletions lib/timings_plot_reporter.go

This file was deleted.

12 changes: 5 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func run(rate uint64, duration time.Duration, targetsf, ordering, reporter, outp
var rep vegeta.Reporter
switch reporter {
case "text":
rep = vegeta.NewTextReporter()
rep = vegeta.ReportText
case "plot:timings":
rep = vegeta.NewTimingsPlotReporter()
rep = vegeta.ReportTimingsPlot
default:
log.Println("Reporter provided is not supported. Using text")
rep = vegeta.NewTextReporter()
rep = vegeta.ReportText
}

targets, err := vegeta.NewTargetsFromFile(targetsf)
Expand All @@ -94,12 +94,10 @@ func run(rate uint64, duration time.Duration, targetsf, ordering, reporter, outp
}

log.Printf("Vegeta is attacking %d targets in %s order for %s...\n", len(targets), ordering, duration)
for _, result := range vegeta.Attack(targets, rate, duration) {
rep.Add(&result)
}
results := vegeta.Attack(targets, rate, duration)
log.Println("Done!")
log.Printf("Writing report to '%s'...", output)
if err = rep.Report(out); err != nil {
if err = rep(results, out); err != nil {
return fmt.Errorf(errReportingPrefix+"%s", err)
}
return nil
Expand Down

0 comments on commit 9331099

Please sign in to comment.