Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report details fix #109

Merged
merged 3 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, time.Now())
}
}
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
32 changes: 32 additions & 0 deletions runner/reporter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"context"
"encoding/json"
"testing"
"time"
Expand All @@ -26,3 +27,34 @@ func TestReport_MarshalJSON(t *testing.T) {
expected := `{"date":"2006-01-02T15:04:00-07:00","options":{"insecure":false,"binary":false,"CPUs":0},"count":1000,"total":10000000000,"average":500000000,"fastest":10000000,"slowest":1000000000,"rps":34567.89,"errorDistribution":null,"statusCodeDistribution":null,"latencyDistribution":null,"histogram":null,"details":null}`
assert.Equal(t, expected, string(json))
}

func TestReport_CorrectDetails(t *testing.T) {
callResultsChan := make(chan *callResult)
config, _ := newConfig("call", "host")
reporter := newReporter(callResultsChan, config)

go reporter.Run()

cr1 := callResult{
status: "OK",
duration: time.Millisecond * 100,
err: nil,
timestamp: time.Now(),
}
callResultsChan <- &cr1
cr2 := callResult{
status: "DeadlineExceeded",
duration: time.Millisecond * 500,
err: context.DeadlineExceeded,
timestamp: time.Now(),
}
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])
}
7 changes: 4 additions & 3 deletions runner/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ const maxResult = 1000000

// result of a call
type callResult struct {
err error
status string
duration time.Duration
err error
status string
duration time.Duration
timestamp time.Time
}

// Requester is used for doing the requests
Expand Down
2 changes: 1 addition & 1 deletion runner/stats_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *statsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
st = s.Code().String()
}

c.results <- &callResult{rpcStats.Error, st, duration}
c.results <- &callResult{rpcStats.Error, st, duration, end}
ezsilmar marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down