Skip to content

Commit

Permalink
Merge pull request #217 from bojand/lat_fix
Browse files Browse the repository at this point in the history
Percentile Latencies Fix
  • Loading branch information
bojand committed Aug 23, 2020
2 parents f3b2108 + dabc0d8 commit 4e9c2f7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
23 changes: 17 additions & 6 deletions runner/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,25 @@ func (r *Reporter) Finalize(stopReason StopReason, total time.Duration) *Report
func latencies(latencies []float64) []LatencyDistribution {
pctls := []int{10, 25, 50, 75, 90, 95, 99}
data := make([]float64, len(pctls))
j := 0
for i := 0; i < len(latencies) && j < len(pctls); i++ {
current := i * 100 / len(latencies)
if current >= pctls[j] {
data[j] = latencies[i]
j++
lt := float64(len(latencies))
for i, p := range pctls {
ip := (float64(p) / 100.0) * lt
di := int(ip)

// since we're dealing with 0th based ranks we need to
// check if ordinal is a whole number that lands on the percentile
// if so adjust accordingly
if ip == float64(di) {
di = di - 1
}

if di < 0 {
di = 0
}

data[i] = latencies[di]
}

res := make([]LatencyDistribution, len(pctls))
for i := 0; i < len(pctls); i++ {
if data[i] > 0 {
Expand Down
64 changes: 64 additions & 0 deletions runner/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runner
import (
"context"
"encoding/json"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -58,3 +59,66 @@ func TestReport_CorrectDetails(t *testing.T) {
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])
}

func TestReport_latencies(t *testing.T) {
var tests = []struct {
input []float64
expected []LatencyDistribution
}{
{
input: []float64{15, 20, 35, 40, 50},
expected: []LatencyDistribution{
{Percentage: 10, Latency: 15 * time.Second},
{Percentage: 25, Latency: 20 * time.Second},
{Percentage: 50, Latency: 35 * time.Second},
{Percentage: 75, Latency: 40 * time.Second},
{Percentage: 90, Latency: 50 * time.Second},
{Percentage: 95, Latency: 50 * time.Second},
{Percentage: 99, Latency: 50 * time.Second},
},
},
{
input: []float64{3, 6, 7, 8, 8, 10, 13, 15, 16, 20},
expected: []LatencyDistribution{
{Percentage: 10, Latency: 3 * time.Second},
{Percentage: 25, Latency: 7 * time.Second},
{Percentage: 50, Latency: 8 * time.Second},
{Percentage: 75, Latency: 15 * time.Second},
{Percentage: 90, Latency: 16 * time.Second},
{Percentage: 95, Latency: 20 * time.Second},
{Percentage: 99, Latency: 20 * time.Second},
},
},
{
input: []float64{3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20},
expected: []LatencyDistribution{
{Percentage: 10, Latency: 6 * time.Second},
{Percentage: 25, Latency: 7 * time.Second},
{Percentage: 50, Latency: 9 * time.Second},
{Percentage: 75, Latency: 15 * time.Second},
{Percentage: 90, Latency: 16 * time.Second},
{Percentage: 95, Latency: 20 * time.Second},
{Percentage: 99, Latency: 20 * time.Second},
},
},
{
input: []float64{2.1, 3.2, 4.5, 6.3, 7.4, 8.5, 9.6, 10.7, 13.8, 15.9, 16.11, 18.17, 20.11, 22.34},
expected: []LatencyDistribution{
{Percentage: 10, Latency: time.Duration(3.2 * float64(time.Second))},
{Percentage: 25, Latency: time.Duration(6.3 * float64(time.Second))},
{Percentage: 50, Latency: time.Duration(9.6 * float64(time.Second))},
{Percentage: 75, Latency: time.Duration(16.11 * float64(time.Second))},
{Percentage: 90, Latency: time.Duration(20.11 * float64(time.Second))},
{Percentage: 95, Latency: time.Duration(22.34 * float64(time.Second))},
{Percentage: 99, Latency: time.Duration(22.34 * float64(time.Second))},
},
},
}

for i, tt := range tests {
t.Run("latencies "+strconv.FormatInt(int64(i), 10), func(t *testing.T) {
lats := latencies(tt.input)
assert.Equal(t, tt.expected, lats)
})
}
}

0 comments on commit 4e9c2f7

Please sign in to comment.