Skip to content

Commit

Permalink
metrics: remove deprecated uses of math.rand (ethereum#26710)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Dec 10, 2024
1 parent 4d7058f commit 3522dc4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
29 changes: 27 additions & 2 deletions metrics/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ExpDecaySample struct {
reservoirSize int
t0, t1 time.Time
values *expDecaySampleHeap
rand *rand.Rand
}

// NewExpDecaySample constructs a new exponentially-decaying sample with the
Expand All @@ -59,6 +60,12 @@ func NewExpDecaySample(reservoirSize int, alpha float64) Sample {
return s
}

// SetRand sets the random source (useful in tests)
func (s *ExpDecaySample) SetRand(prng *rand.Rand) Sample {
s.rand = prng
return s
}

// Clear clears all samples.
func (s *ExpDecaySample) Clear() {
s.mutex.Lock()
Expand Down Expand Up @@ -168,8 +175,14 @@ func (s *ExpDecaySample) update(t time.Time, v int64) {
if s.values.Size() == s.reservoirSize {
s.values.Pop()
}
var f64 float64
if s.rand != nil {
f64 = s.rand.Float64()
} else {
f64 = rand.Float64()
}
s.values.Push(expDecaySample{
k: math.Exp(t.Sub(s.t0).Seconds()*s.alpha) / rand.Float64(),
k: math.Exp(t.Sub(s.t0).Seconds()*s.alpha) / f64,
v: v,
})
if t.After(s.t1) {
Expand Down Expand Up @@ -402,6 +415,7 @@ type UniformSample struct {
mutex sync.Mutex
reservoirSize int
values []int64
rand *rand.Rand
}

// NewUniformSample constructs a new uniform sample with the given reservoir
Expand All @@ -416,6 +430,12 @@ func NewUniformSample(reservoirSize int) Sample {
}
}

// SetRand sets the random source (useful in tests)
func (s *UniformSample) SetRand(prng *rand.Rand) Sample {
s.rand = prng
return s
}

// Clear clears all samples.
func (s *UniformSample) Clear() {
s.mutex.Lock()
Expand Down Expand Up @@ -511,7 +531,12 @@ func (s *UniformSample) Update(v int64) {
if len(s.values) < s.reservoirSize {
s.values = append(s.values, v)
} else {
r := rand.Int63n(s.count)
var r int64
if s.rand != nil {
r = s.rand.Int63n(s.count)
} else {
r = rand.Int63n(s.count)
}
if r < int64(len(s.values)) {
s.values[int(r)] = v
}
Expand Down
19 changes: 5 additions & 14 deletions metrics/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func BenchmarkUniformSample1028(b *testing.B) {
}

func TestExpDecaySample10(t *testing.T) {
rand.Seed(1)
s := NewExpDecaySample(100, 0.99)
for i := 0; i < 10; i++ {
s.Update(int64(i))
Expand All @@ -102,7 +101,6 @@ func TestExpDecaySample10(t *testing.T) {
}

func TestExpDecaySample100(t *testing.T) {
rand.Seed(1)
s := NewExpDecaySample(1000, 0.01)
for i := 0; i < 100; i++ {
s.Update(int64(i))
Expand All @@ -124,7 +122,6 @@ func TestExpDecaySample100(t *testing.T) {
}

func TestExpDecaySample1000(t *testing.T) {
rand.Seed(1)
s := NewExpDecaySample(100, 0.99)
for i := 0; i < 1000; i++ {
s.Update(int64(i))
Expand All @@ -150,7 +147,6 @@ func TestExpDecaySample1000(t *testing.T) {
// The priority becomes +Inf quickly after starting if this is done,
// effectively freezing the set of samples until a rescale step happens.
func TestExpDecaySampleNanosecondRegression(t *testing.T) {
rand.Seed(1)
s := NewExpDecaySample(100, 0.99)
for i := 0; i < 100; i++ {
s.Update(10)
Expand Down Expand Up @@ -183,8 +179,7 @@ func TestExpDecaySampleRescale(t *testing.T) {

func TestExpDecaySampleSnapshot(t *testing.T) {
now := time.Now()
rand.Seed(1)
s := NewExpDecaySample(100, 0.99)
s := NewExpDecaySample(100, 0.99).(*ExpDecaySample).SetRand(rand.New(rand.NewSource(1)))
for i := 1; i <= 10000; i++ {
s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
}
Expand All @@ -195,16 +190,14 @@ func TestExpDecaySampleSnapshot(t *testing.T) {

func TestExpDecaySampleStatistics(t *testing.T) {
now := time.Now()
rand.Seed(1)
s := NewExpDecaySample(100, 0.99)
s := NewExpDecaySample(100, 0.99).(*ExpDecaySample).SetRand(rand.New(rand.NewSource(1)))
for i := 1; i <= 10000; i++ {
s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
}
testExpDecaySampleStatistics(t, s)
}

func TestUniformSample(t *testing.T) {
rand.Seed(1)
s := NewUniformSample(100)
for i := 0; i < 1000; i++ {
s.Update(int64(i))
Expand All @@ -226,7 +219,6 @@ func TestUniformSample(t *testing.T) {
}

func TestUniformSampleIncludesTail(t *testing.T) {
rand.Seed(1)
s := NewUniformSample(100)
max := 100
for i := 0; i < max; i++ {
Expand All @@ -244,7 +236,7 @@ func TestUniformSampleIncludesTail(t *testing.T) {
}

func TestUniformSampleSnapshot(t *testing.T) {
s := NewUniformSample(100)
s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewSource(1)))
for i := 1; i <= 10000; i++ {
s.Update(int64(i))
}
Expand All @@ -254,8 +246,7 @@ func TestUniformSampleSnapshot(t *testing.T) {
}

func TestUniformSampleStatistics(t *testing.T) {
rand.Seed(1)
s := NewUniformSample(100)
s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewSource(1)))
for i := 1; i <= 10000; i++ {
s.Update(int64(i))
}
Expand Down Expand Up @@ -327,7 +318,7 @@ func testUniformSampleStatistics(t *testing.T, s Sample) {
if ps[1] != 7380.5 {
t.Errorf("75th percentile: 7380.5 != %v\n", ps[1])
}
if math.Abs(ps[2]-9986.429999999998) > epsilonPercentile {
if math.Abs(9986.429999999998-ps[2]) > epsilonPercentile {
t.Errorf("99th percentile: 9986.429999999998 != %v\n", ps[2])
}
}
Expand Down

0 comments on commit 3522dc4

Please sign in to comment.