Skip to content

Commit

Permalink
chore: fixes and drop percentile calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriBeck committed Sep 27, 2023
1 parent a0b49d9 commit 801ad9e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 64 deletions.
10 changes: 5 additions & 5 deletions endpoint_randomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type EndpointRandomizer struct {
func NewEndpointRandomizer(endpoints []Endpoint) *EndpointRandomizer {
randomizedEndpoints := make([]randomizedEndpoint, len(endpoints))
var total int32

for i, endpoint := range endpoints {
newTotal := total + endpoint.GetRequestsPerMinute()
randomizedEndpoints[i] = randomizedEndpoint{
start: total,
end: newTotal,
start: total + 1,
end: total + endpoint.GetRequestsPerMinute(),
endpoint: endpoint,
}

total = newTotal
total += endpoint.GetRequestsPerMinute()
}

return &EndpointRandomizer{
Expand All @@ -41,7 +41,7 @@ func (r *EndpointRandomizer) PickRandomEndpoint() Endpoint {
pickedRange := r.rand.Int31n(r.total) + 1

for _, endpoint := range r.endpoints {
if endpoint.start < pickedRange && pickedRange < endpoint.end {
if endpoint.start <= pickedRange && pickedRange <= endpoint.end {
return endpoint.endpoint
}
}
Expand Down
4 changes: 2 additions & 2 deletions loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ loop:

startTime := time.Now()
err := endpoint.Execute(ctx)
endTime := time.Now()
duration := time.Since(startTime)

loadtest.Results.SaveEndpointResult(
endpoint,
EndpointResult{
Failed: err != nil,
Duration: endTime.Sub(startTime),
Duration: duration,
},
)
}()
Expand Down
22 changes: 3 additions & 19 deletions results.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package goload

import (
"sort"
"sync"
"sync/atomic"
"time"

"github.com/spenczar/tdigest"
)

type EndpointResult struct {
Expand All @@ -16,11 +13,9 @@ type EndpointResult struct {

type EndpointResults struct {
Name string
lock sync.Mutex
total uint64
failed uint64
totalDuration uint64
td *tdigest.TDigest
}

func (e *EndpointResults) GetTotalRequests() uint64 {
Expand All @@ -35,10 +30,6 @@ func (e *EndpointResults) GetAverageDuration() float64 {
return float64(atomic.LoadUint64(&e.totalDuration)) / float64(atomic.LoadUint64(&e.total))
}

func (e *EndpointResults) GetPercentileDuration(p float64) float64 {
return e.td.Quantile(p)
}

type LoadTestResults struct {
endpoints map[string]*EndpointResults
}
Expand All @@ -51,7 +42,6 @@ func NewResults(endpoints []Endpoint) *LoadTestResults {
for _, endpoint := range endpoints {
results.endpoints[endpoint.Name()] = &EndpointResults{
Name: endpoint.Name(),
td: tdigest.New(),
}
}

Expand All @@ -72,19 +62,13 @@ func (results *LoadTestResults) Iter() []*EndpointResults {
}

func (results *LoadTestResults) SaveEndpointResult(Endpoint Endpoint, result EndpointResult) {
e := results.endpoints[Endpoint.Name()]

atomic.AddUint64(&e.total, 1)
atomic.AddUint64(&results.endpoints[Endpoint.Name()].total, 1)
if result.Failed {
atomic.AddUint64(&e.failed, 1)
atomic.AddUint64(&results.endpoints[Endpoint.Name()].failed, 1)
}

atomic.AddUint64(
&e.totalDuration,
&results.endpoints[Endpoint.Name()].totalDuration,
uint64(result.Duration.Milliseconds()),
)

e.lock.Lock()
e.td.Add(float64(result.Duration.Milliseconds()), 1)
e.lock.Unlock()
}
38 changes: 0 additions & 38 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ func (ui *UI) ReportResults(results *LoadTestResults) {
addFailedRequestsColumn(rows, results)
addAverageResponseTimeColumn(rows, results)

addPercentileResponseTimeColumn(rows, results, 0.90)
addPercentileResponseTimeColumn(rows, results, 0.95)
addPercentileResponseTimeColumn(rows, results, 0.99)

for _, row := range rows {
fmt.Fprintln(
ui.output,
Expand Down Expand Up @@ -184,37 +180,3 @@ func addAverageResponseTimeColumn(columns [][]string, results *LoadTestResults)
)
}
}

func addPercentileResponseTimeColumn(columns [][]string, results *LoadTestResults, p float64) {
items := []string{}
for _, endpoint := range results.Iter() {
percentile := endpoint.GetPercentileDuration(p)
if math.IsNaN(percentile) {
items = append(items, "")
continue
}

items = append(
items,
fmt.Sprintf("p(%.0f)=%.2fms", p*100, percentile),
)
}

maxLen := 0
for _, item := range items {
if maxLen < len(item) {
maxLen = len(item)
}
}

if maxLen == 0 {
return
}

for index, item := range items {
columns[index] = append(
columns[index],
fmt.Sprintf("%s%s", item, strings.Repeat(" ", maxLen-len(item))),
)
}
}

0 comments on commit 801ad9e

Please sign in to comment.