-
Notifications
You must be signed in to change notification settings - Fork 432
/
benchmark.go
38 lines (33 loc) · 1.13 KB
/
benchmark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package transforms
import (
"sort"
"time"
"github.com/solo-io/gloo/test/helpers"
)
// WithPercentile returns a function that extracts the value at the given percentile from a slice of durations
// The Nearest Rank Method is used to determine percentiles (https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method)
// Valid inputs are 0 < n <= 100
func WithPercentile(percentile int) func(durations []time.Duration) time.Duration {
return func(durations []time.Duration) time.Duration {
sort.Slice(durations, func(i, j int) bool {
return durations[i] < durations[j]
})
idx := helpers.PercentileIndex(len(durations), percentile)
return durations[idx]
}
}
// WithMedian returns a function that extracts the value at the median from a slice of durations
func WithMedian() func([]time.Duration) time.Duration {
return func(durations []time.Duration) time.Duration {
sort.Slice(durations, func(i, j int) bool {
return durations[i] < durations[j]
})
var median time.Duration
if l := len(durations); l%2 == 1 {
median = durations[l/2]
} else {
median = (durations[l/2] + durations[l/2-1]) / 2
}
return median
}
}