Skip to content

Commit

Permalink
Add failed requests information to details
Browse files Browse the repository at this point in the history
  • Loading branch information
ezsilmar committed Jul 4, 2019
1 parent b5753d6 commit f954d4f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
100 changes: 45 additions & 55 deletions runner/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions runner/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit f954d4f

Please sign in to comment.