From 681a7f2d029f40acb14a87cf651b1df92800bf07 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Thu, 7 Nov 2013 11:27:05 +0000 Subject: [PATCH] modified results and metrics --- lib/metrics.go | 17 +++++++++++- lib/reporters.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ report.go | 2 ++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/lib/metrics.go b/lib/metrics.go index f0d0877..2967fdf 100644 --- a/lib/metrics.go +++ b/lib/metrics.go @@ -3,8 +3,9 @@ package vegeta import ( "strconv" "time" - "github.com/bmizerany/perks/quantile" + "fmt" + "strings" ) // Metrics holds the stats computed out of a slice of Results @@ -76,3 +77,17 @@ func NewMetrics(results []Result) *Metrics { return m } + + +func csvString(d time.Duration) string { + var result float64 = float64(d.Nanoseconds() / 1000.0 / 1000.0) // in miliseconds + return strconv.FormatFloat(result, 'f', -1, 64) +} + +func(m *Metrics) Csv(rate uint64) []string { + result := fmt.Sprintf("%d req/s,%s,%s,%s,%s,%f,%f,%f",rate, + csvString(m.Latencies.Mean), csvString(m.Latencies.P95), csvString(m.Latencies.P99), csvString(m.Latencies.Max), + m.BytesIn.Mean, m.BytesOut.Mean, m.Success) + return strings.Split(result, ",") +} + diff --git a/lib/reporters.go b/lib/reporters.go index 482ce61..f65b658 100644 --- a/lib/reporters.go +++ b/lib/reporters.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "text/tabwriter" + "encoding/csv" ) // Reporter represents any function which takes a slice of Results and @@ -88,3 +89,69 @@ var plotsTemplate = ` ` + + + +type ResultGroup struct { + from uint64 + to uint64 + rate uint64 +} + +func ReportCSV(results []Result) ([]byte, error) { + out := &bytes.Buffer{} + m := NewMetrics(results) + // result := fnmt.Sprintf("%d req/s,%s,%s,%s,%s,%f,%f,%f",rate, + // m.Latencies.Mean.CsvString(), m.Latencies.P95.CsvString(), m.Latencies.P99.CsvString(), m.Latencies.Max.CsvString(), + // m.BytesIn.Mean, m.BytesOut.Mean, m.Success) + header := []string{ "rate" , "mean" , "p95", "p99" , "max", "bytesIn", "bytesOut", "success" } + + w := csv.NewWriter(out) + w.Write(header) + + resultGroups := slicesPerAttackRate(results) + + for _,resultGroup := range resultGroups { + m := NewMetrics(results[resultGroup.from:resultGroup.to]) + w.Write(m.Csv(resultGroup.rate)) + } + + w.Flush() + + return out.Bytes(), nil +} + + + + + +func slicesPerAttackRate(results []Result) ([]ResultGroup) { + + resultGroups := []ResultGroup{} + + + if len(results) > 0 { + + resultGroup := new(ResultGroup) + resultGroup.from = 0 + resultGroup.to = 0 + resultGroup.rate = results[0].rate + + + for i, result := range results { + if result.rate != resultGroup.rate { + resultGroup.to = i + append(resultGroups, resultGroup) + resultGroup = new(ResultGroup) + resultGroup.from = i + resultGroup.rate = result.rate + + } + } + resultGroup.to = len(results) + append(resultGroups, resultGroup) + + } + + return resultGroups +} \ No newline at end of file diff --git a/report.go b/report.go index b189ae1..614fa2b 100644 --- a/report.go +++ b/report.go @@ -30,6 +30,8 @@ func report(reporter, input, output string) error { rep = vegeta.ReportJSON case "plot": rep = vegeta.ReportPlot + case "csv": + rep = vegeta.ReportCSV default: log.Println("Reporter provided is not supported. Using text") rep = vegeta.ReportText