From 9fdda5aed16f2a148e5020363b3d40e4a02abc7d Mon Sep 17 00:00:00 2001 From: Erwin van Eyk Date: Wed, 5 Sep 2018 02:18:46 +0200 Subject: [PATCH] Print status code of each request in csv output (#122) --- requester/print.go | 18 ++++--- requester/report.go | 116 +++++++++++++++++++++++--------------------- 2 files changed, 73 insertions(+), 61 deletions(-) diff --git a/requester/print.go b/requester/print.go index 37bcf51b..7bf65358 100644 --- a/requester/print.go +++ b/requester/print.go @@ -34,9 +34,10 @@ func newTemplate(output string) *template.Template { } var tmplFuncMap = template.FuncMap{ - "formatNumber": formatNumber, - "histogram": histogram, - "jsonify": jsonify, + "formatNumber": formatNumber, + "formatNumberInt": formatNumberInt, + "histogram": histogram, + "jsonify": jsonify, } func jsonify(v interface{}) string { @@ -48,6 +49,10 @@ func formatNumber(duration float64) string { return fmt.Sprintf("%4.4f", duration) } +func formatNumberInt(duration int) string { + return fmt.Sprintf("%d", duration) +} + func histogram(buckets []Bucket) string { max := 0 for _, b := range buckets { @@ -98,8 +103,7 @@ Status code distribution:{{ range $code, $num := .StatusCodeDist }} {{ if gt (len .ErrorDist) 0 }}Error distribution:{{ range $err, $num := .ErrorDist }} [{{ $num }}] {{ $err }}{{ end }}{{ end }} ` - csvTmpl = `{{ $connLats := .ConnLats }}{{ $dnsLats := .DnsLats }}{{ $dnsLats := .DnsLats }}{{ $reqLats := .ReqLats }}{{ $delayLats := .DelayLats }}{{ $resLats := .ResLats }} -response-time,DNS+dialup,DNS,Request-write,Response-delay,Response-read{{ range $i, $v := .Lats }} -{{ formatNumber $v }},{{ formatNumber (index $connLats $i) }},{{ formatNumber (index $dnsLats $i) }},{{ formatNumber (index $reqLats $i) }},{{ formatNumber (index $delayLats $i) }},{{ formatNumber (index $resLats $i) }}{{ end }} -` + csvTmpl = `{{ $connLats := .ConnLats }}{{ $dnsLats := .DnsLats }}{{ $dnsLats := .DnsLats }}{{ $reqLats := .ReqLats }}{{ $delayLats := .DelayLats }}{{ $resLats := .ResLats }}{{ $statusCodeLats := .StatusCodes }} +response-time,DNS+dialup,DNS,Request-write,Response-delay,Response-read,status-code{{ range $i, $v := .Lats }} +{{ formatNumber $v }},{{ formatNumber (index $connLats $i) }},{{ formatNumber (index $dnsLats $i) }},{{ formatNumber (index $reqLats $i) }},{{ formatNumber (index $delayLats $i) }},{{ formatNumber (index $resLats $i) }},{{ formatNumberInt (index $statusCodeLats $i) }}{{ end }}` ) diff --git a/requester/report.go b/requester/report.go index d490c9c5..b3104772 100644 --- a/requester/report.go +++ b/requester/report.go @@ -37,27 +37,27 @@ type report struct { average float64 rps float64 - avgConn float64 - avgDNS float64 - avgReq float64 - avgRes float64 - avgDelay float64 - connLats []float64 - dnsLats []float64 - reqLats []float64 - resLats []float64 - delayLats []float64 + avgConn float64 + avgDNS float64 + avgReq float64 + avgRes float64 + avgDelay float64 + connLats []float64 + dnsLats []float64 + reqLats []float64 + resLats []float64 + delayLats []float64 + statusCodes []int results chan *result done chan bool total time.Duration - errorDist map[string]int - statusCodeDist map[int]int - lats []float64 - sizeTotal int64 - numRes int64 - output string + errorDist map[string]int + lats []float64 + sizeTotal int64 + numRes int64 + output string w io.Writer } @@ -65,18 +65,18 @@ type report struct { func newReport(w io.Writer, results chan *result, output string, n int) *report { cap := min(n, maxRes) return &report{ - output: output, - results: results, - done: make(chan bool, 1), - statusCodeDist: make(map[int]int), - errorDist: make(map[string]int), - w: w, - connLats: make([]float64, 0, cap), - dnsLats: make([]float64, 0, cap), - reqLats: make([]float64, 0, cap), - resLats: make([]float64, 0, cap), - delayLats: make([]float64, 0, cap), - lats: make([]float64, 0, cap), + output: output, + results: results, + done: make(chan bool, 1), + errorDist: make(map[string]int), + w: w, + connLats: make([]float64, 0, cap), + dnsLats: make([]float64, 0, cap), + reqLats: make([]float64, 0, cap), + resLats: make([]float64, 0, cap), + delayLats: make([]float64, 0, cap), + lats: make([]float64, 0, cap), + statusCodes: make([]int, 0, cap), } } @@ -100,8 +100,8 @@ func runReporter(r *report) { r.reqLats = append(r.reqLats, res.reqDuration.Seconds()) r.delayLats = append(r.delayLats, res.delayDuration.Seconds()) r.resLats = append(r.resLats, res.resDuration.Seconds()) + r.statusCodes = append(r.statusCodes, res.statusCode) } - r.statusCodeDist[res.statusCode]++ if res.contentLength > 0 { r.sizeTotal += res.contentLength } @@ -140,25 +140,25 @@ func (r *report) printf(s string, v ...interface{}) { func (r *report) snapshot() Report { snapshot := Report{ - AvgTotal: r.avgTotal, - Average: r.average, - Rps: r.rps, - SizeTotal: r.sizeTotal, - AvgConn: r.avgConn, - AvgDNS: r.avgDNS, - AvgReq: r.avgReq, - AvgRes: r.avgRes, - AvgDelay: r.avgDelay, - Total: r.total, - ErrorDist: r.errorDist, - StatusCodeDist: r.statusCodeDist, - NumRes: r.numRes, - Lats: make([]float64, len(r.lats)), - ConnLats: make([]float64, len(r.lats)), - DnsLats: make([]float64, len(r.lats)), - ReqLats: make([]float64, len(r.lats)), - ResLats: make([]float64, len(r.lats)), - DelayLats: make([]float64, len(r.lats)), + AvgTotal: r.avgTotal, + Average: r.average, + Rps: r.rps, + SizeTotal: r.sizeTotal, + AvgConn: r.avgConn, + AvgDNS: r.avgDNS, + AvgReq: r.avgReq, + AvgRes: r.avgRes, + AvgDelay: r.avgDelay, + Total: r.total, + ErrorDist: r.errorDist, + NumRes: r.numRes, + Lats: make([]float64, len(r.lats)), + ConnLats: make([]float64, len(r.lats)), + DnsLats: make([]float64, len(r.lats)), + ReqLats: make([]float64, len(r.lats)), + ResLats: make([]float64, len(r.lats)), + DelayLats: make([]float64, len(r.lats)), + StatusCodes: make([]int, len(r.lats)), } if len(r.lats) == 0 { @@ -173,6 +173,7 @@ func (r *report) snapshot() Report { copy(snapshot.ReqLats, r.reqLats) copy(snapshot.ResLats, r.resLats) copy(snapshot.DelayLats, r.delayLats) + copy(snapshot.StatusCodes, r.statusCodes) sort.Float64s(r.lats) r.fastest = r.lats[0] @@ -200,6 +201,12 @@ func (r *report) snapshot() Report { snapshot.ResMax = r.resLats[0] snapshot.ResMin = r.resLats[len(r.resLats)-1] + statusCodeDist := make(map[int]int, len(snapshot.StatusCodes)) + for _, statusCode := range snapshot.StatusCodes { + statusCodeDist[statusCode]++ + } + snapshot.StatusCodeDist = statusCodeDist + return snapshot } @@ -279,12 +286,13 @@ type Report struct { DelayMax float64 DelayMin float64 - Lats []float64 - ConnLats []float64 - DnsLats []float64 - ReqLats []float64 - ResLats []float64 - DelayLats []float64 + Lats []float64 + ConnLats []float64 + DnsLats []float64 + ReqLats []float64 + ResLats []float64 + DelayLats []float64 + StatusCodes []int Total time.Duration