From f954d4f39dff85247be819270bf4bae3b3f96911 Mon Sep 17 00:00:00 2001 From: "e.zhirov" Date: Thu, 4 Jul 2019 14:27:48 +0200 Subject: [PATCH] Add failed requests information to details --- runner/reporter.go | 100 ++++++++++++++++++---------------------- runner/reporter_test.go | 3 ++ 2 files changed, 48 insertions(+), 55 deletions(-) diff --git a/runner/reporter.go b/runner/reporter.go index 027832c3..e8cf03dc 100644 --- a/runner/reporter.go +++ b/runner/reporter.go @@ -13,12 +13,9 @@ type Reporter struct { results chan *callResult done chan bool - avgTotal float64 + totalLatenciesSec float64 - lats []float64 - errors []string - statuses []string - timestamps []time.Time + details []ResultDetail errorDist map[string]int statusCodeDist map[string]int @@ -124,38 +121,37 @@ func newReporter(results chan *callResult, c *RunConfig) *Reporter { cap := min(c.n, maxResult) return &Reporter{ - config: c, - results: results, - done: make(chan bool, 1), + config: c, + results: results, + done: make(chan bool, 1), + details: make([]ResultDetail, 0, cap), + statusCodeDist: make(map[string]int), errorDist: make(map[string]int), - lats: make([]float64, 0, cap), } } // Run runs the reporter func (r *Reporter) Run() { for res := range r.results { + errStr := "" + if res.err != nil { + errStr = res.err.Error() + } + r.totalCount++ - r.avgTotal += res.duration.Seconds() + r.totalLatenciesSec += res.duration.Seconds() + r.statusCodeDist[res.status]++ if res.err != nil { - errStr := res.err.Error() r.errorDist[errStr]++ - r.statusCodeDist[res.status]++ - - if len(r.errors) < maxResult { - r.errors = append(r.errors, errStr) - r.statuses = append(r.statuses, res.status) - } - } else { - r.statusCodeDist[res.status]++ - - if len(r.lats) < maxResult { - r.lats = append(r.lats, res.duration.Seconds()) - r.errors = append(r.errors, "") - r.statuses = append(r.statuses, res.status) - r.timestamps = append(r.timestamps, res.timestamp) - } + } + if len(r.details) < maxResult { + r.details = append(r.details, ResultDetail{ + Latency: res.duration, + Timestamp: res.timestamp, + Status: res.status, + Error: errStr, + }) } } r.done <- true @@ -204,37 +200,31 @@ func (r *Reporter) Finalize(stopReason StopReason, total time.Duration) *Report _ = json.Unmarshal(r.config.tags, &rep.Tags) - if len(r.lats) > 0 { - average := r.avgTotal / float64(r.totalCount) - avgDuration := time.Duration(average * float64(time.Second)) - rep.Average = avgDuration - - rps := float64(r.totalCount) / total.Seconds() - rep.Rps = rps - - lats := make([]float64, len(r.lats)) - copy(lats, r.lats) - sort.Float64s(lats) - - var fastestNum, slowestNum float64 - fastestNum = lats[0] - slowestNum = lats[len(lats)-1] - - rep.Fastest = time.Duration(fastestNum * float64(time.Second)) - rep.Slowest = time.Duration(slowestNum * float64(time.Second)) - rep.Histogram = histogram(lats, slowestNum, fastestNum) - rep.LatencyDistribution = latencies(lats) - - rep.Details = make([]ResultDetail, len(r.lats)) - for i, num := range r.lats { - lat := time.Duration(num * float64(time.Second)) - rep.Details[i] = ResultDetail{ - Latency: lat, - Error: r.errors[i], - Status: r.statuses[i], - Timestamp: r.timestamps[i], + if len(r.details) > 0 { + average := r.totalLatenciesSec / float64(r.totalCount) + rep.Average = time.Duration(average * float64(time.Second)) + + rep.Rps = float64(r.totalCount) / total.Seconds() + + okLats := make([]float64, 0) + for _, d := range r.details { + if d.Error == "" { + okLats = append(okLats, d.Latency.Seconds()) } } + sort.Float64s(okLats) + if len(okLats) > 0 { + var fastestNum, slowestNum float64 + fastestNum = okLats[0] + slowestNum = okLats[len(okLats)-1] + + rep.Fastest = time.Duration(fastestNum * float64(time.Second)) + rep.Slowest = time.Duration(slowestNum * float64(time.Second)) + rep.Histogram = histogram(okLats, slowestNum, fastestNum) + rep.LatencyDistribution = latencies(okLats) + } + + rep.Details = r.details } return rep diff --git a/runner/reporter_test.go b/runner/reporter_test.go index 9c102ac0..9434f2f4 100644 --- a/runner/reporter_test.go +++ b/runner/reporter_test.go @@ -50,7 +50,10 @@ func TestReport_CorrectDetails(t *testing.T) { } callResultsChan <- &cr2 + close(callResultsChan) + <-reporter.done report := reporter.Finalize("stop reason", time.Second) + assert.Equal(t, 2, len(report.Details)) assert.Equal(t, ResultDetail{Error: "", Latency: cr1.duration, Status: cr1.status, Timestamp: cr1.timestamp}, report.Details[0]) assert.Equal(t, ResultDetail{Error: cr2.err.Error(), Latency: cr2.duration, Status: cr2.status, Timestamp: cr2.timestamp}, report.Details[1])