summaryrefslogtreecommitdiff
path: root/utilities/utilities.go
diff options
context:
space:
mode:
authorWill Hawkins <[email protected]>2023-07-10 13:45:50 -0400
committerWill Hawkins <[email protected]>2023-07-10 13:45:50 -0400
commit78d574a74665c8bc062c26755c80a8b524bce347 (patch)
tree7ad65f0052defaea63acb2f3445be00ef97e24d6 /utilities/utilities.go
parentfe17152a507bbf94a11cca7f49a51cbae9c0d67b (diff)
[Feature] Major update: Track measurements that may be delayed
Among other major feature additions, this version of the client tracks any measurements that may be long delayed and considers their presence or absence as part of a stability measurement. This version of the client also more closely tracks the spec. In particular, it performs a sinle-sided trimmed mean rather than a double-sided trimmed mean. Signed-off-by: Will Hawkins <[email protected]>
Diffstat (limited to 'utilities/utilities.go')
-rw-r--r--utilities/utilities.go61
1 files changed, 23 insertions, 38 deletions
diff --git a/utilities/utilities.go b/utilities/utilities.go
index ff04023..b976f77 100644
--- a/utilities/utilities.go
+++ b/utilities/utilities.go
@@ -21,13 +21,10 @@ import (
"math/rand"
"os"
"reflect"
- "sort"
"strings"
"sync"
"sync/atomic"
"time"
-
- "golang.org/x/exp/constraints"
)
// GitVersion is the Git revision hash
@@ -46,24 +43,6 @@ func IsInterfaceNil(ifc interface{}) bool {
(reflect.ValueOf(ifc).Kind() == reflect.Ptr && reflect.ValueOf(ifc).IsNil())
}
-func SignedPercentDifference[T constraints.Float | constraints.Integer](
- current T,
- previous T,
-) (difference float64) {
- fCurrent := float64(current)
- fPrevious := float64(previous)
- return ((fCurrent - fPrevious) / fPrevious) * 100.0
-}
-
-func AbsPercentDifference(
- current float64,
- previous float64,
-) (difference float64) {
- return (math.Abs(current-previous) / (float64(current+previous) / 2.0)) * float64(
- 100,
- )
-}
-
func Conditional[T any](condition bool, t T, f T) T {
if condition {
return t
@@ -137,13 +116,6 @@ func RandBetween(max int) int {
return rand.New(rand.NewSource(int64(time.Now().Nanosecond()))).Int() % max
}
-func Max(x, y uint64) uint64 {
- if x > y {
- return x
- }
- return y
-}
-
func ChannelToSlice[S any](channel <-chan S) (slice []S) {
slice = make([]S, 0)
for element := range channel {
@@ -152,6 +124,26 @@ func ChannelToSlice[S any](channel <-chan S) (slice []S) {
return
}
+func Reverse[T any](elements []T) []T {
+ result := make([]T, len(elements))
+ iterator := len(elements) - 1
+ for _, v := range elements {
+ result[iterator] = v
+ iterator--
+ }
+ return result
+}
+
+func Filter[S any](elements []S, filterer func(S) bool) []S {
+ result := make([]S, 0)
+ for _, s := range elements {
+ if filterer(s) {
+ result = append(result, s)
+ }
+ }
+ return result
+}
+
func Fmap[S any, F any](elements []S, mapper func(S) F) []F {
result := make([]F, 0)
for _, s := range elements {
@@ -160,13 +152,6 @@ func Fmap[S any, F any](elements []S, mapper func(S) F) []F {
return result
}
-func CalculatePercentile[S float32 | int32 | float64 | int64](elements []S, percentile int) S {
- sort.Slice(elements, func(a, b int) bool { return elements[a] < elements[b] })
- elementsCount := len(elements)
- percentileIdx := elementsCount * (percentile / 100)
- return elements[percentileIdx]
-}
-
func OrTimeout(f func(), timeout time.Duration) {
completeChannel := func() chan interface{} {
completed := make(chan interface{})
@@ -227,9 +212,9 @@ func ContextSignaler(ctxt context.Context, st time.Duration, condition *func() b
}
}
-type Pair[T any] struct {
- First T
- Second T
+type Pair[T1, T2 any] struct {
+ First T1
+ Second T2
}
func PerSecondToInterval(rate int64) time.Duration {