Methods for random staggering

This commit is contained in:
Armon Dadgar 2014-01-15 12:34:17 -10:00
parent 177a91c3f9
commit 394e322fee
2 changed files with 16 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package agent
import (
"math"
"math/rand"
"time"
)
@ -23,3 +24,8 @@ func aeScale(interval time.Duration, n int) time.Duration {
multiplier := math.Ceil(math.Log2(float64(n))-math.Log2(aeScaleThreshold)) + 1.0
return time.Duration(multiplier) * interval
}
// Returns a random stagger interval between 0 and the duration
func randomStagger(intv time.Duration) time.Duration {
return time.Duration(uint64(rand.Int63()) % uint64(intv))
}

View File

@ -20,3 +20,13 @@ func TestAEScale(t *testing.T) {
t.Fatalf("Bad: %v", v)
}
}
func TestRandomStagger(t *testing.T) {
intv := time.Minute
for i := 0; i < 10; i++ {
stagger := randomStagger(intv)
if stagger < 0 || stagger >= intv {
t.Fatalf("Bad: %v", stagger)
}
}
}