VictoriaMetrics/lib/timeutil/timeutil_test.go
Aliaksandr Valialkin d52fd73f18
all: add up to 10% random jitter to the interval between periodic tasks performed by various components
This should smooth CPU and RAM usage spikes related to these periodic tasks,
by reducing the probability that multiple concurrent periodic tasks are performed at the same time.
2024-01-22 18:39:16 +02:00

27 lines
535 B
Go

package timeutil
import (
"testing"
"time"
)
func TestAddJitterToDuration(t *testing.T) {
f := func(d time.Duration) {
t.Helper()
result := AddJitterToDuration(d)
if result < d {
t.Fatalf("unexpected negative jitter")
}
variance := (float64(result) - float64(d)) / float64(d)
if variance > 0.1 {
t.Fatalf("too big variance=%.2f for result=%s, d=%s; mustn't exceed 0.1", variance, result, d)
}
}
f(time.Nanosecond)
f(time.Microsecond)
f(time.Millisecond)
f(time.Second)
f(time.Hour)
f(24 * time.Hour)
}