Skip to content

Commit caf8412

Browse files
gzliudanJukLee0ira
authored andcommitted
metrics: remove deprecated uses of math.rand (ethereum#26710)
1 parent e7b5da3 commit caf8412

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

metrics/sample.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type ExpDecaySample struct {
4141
reservoirSize int
4242
t0, t1 time.Time
4343
values *expDecaySampleHeap
44+
rand *rand.Rand
4445
}
4546

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

63+
// SetRand sets the random source (useful in tests)
64+
func (s *ExpDecaySample) SetRand(prng *rand.Rand) Sample {
65+
s.rand = prng
66+
return s
67+
}
68+
6269
// Clear clears all samples.
6370
func (s *ExpDecaySample) Clear() {
6471
s.mutex.Lock()
@@ -168,8 +175,14 @@ func (s *ExpDecaySample) update(t time.Time, v int64) {
168175
if s.values.Size() == s.reservoirSize {
169176
s.values.Pop()
170177
}
178+
var f64 float64
179+
if s.rand != nil {
180+
f64 = s.rand.Float64()
181+
} else {
182+
f64 = rand.Float64()
183+
}
171184
s.values.Push(expDecaySample{
172-
k: math.Exp(t.Sub(s.t0).Seconds()*s.alpha) / rand.Float64(),
185+
k: math.Exp(t.Sub(s.t0).Seconds()*s.alpha) / f64,
173186
v: v,
174187
})
175188
if t.After(s.t1) {
@@ -402,6 +415,7 @@ type UniformSample struct {
402415
mutex sync.Mutex
403416
reservoirSize int
404417
values []int64
418+
rand *rand.Rand
405419
}
406420

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

433+
// SetRand sets the random source (useful in tests)
434+
func (s *UniformSample) SetRand(prng *rand.Rand) Sample {
435+
s.rand = prng
436+
return s
437+
}
438+
419439
// Clear clears all samples.
420440
func (s *UniformSample) Clear() {
421441
s.mutex.Lock()
@@ -511,7 +531,12 @@ func (s *UniformSample) Update(v int64) {
511531
if len(s.values) < s.reservoirSize {
512532
s.values = append(s.values, v)
513533
} else {
514-
r := rand.Int63n(s.count)
534+
var r int64
535+
if s.rand != nil {
536+
r = s.rand.Int63n(s.count)
537+
} else {
538+
r = rand.Int63n(s.count)
539+
}
515540
if r < int64(len(s.values)) {
516541
s.values[int(r)] = v
517542
}

metrics/sample_test.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func BenchmarkUniformSample1028(b *testing.B) {
8080
}
8181

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

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

126124
func TestExpDecaySample1000(t *testing.T) {
127-
rand.Seed(1)
128125
s := NewExpDecaySample(100, 0.99)
129126
for i := 0; i < 1000; i++ {
130127
s.Update(int64(i))
@@ -150,7 +147,6 @@ func TestExpDecaySample1000(t *testing.T) {
150147
// The priority becomes +Inf quickly after starting if this is done,
151148
// effectively freezing the set of samples until a rescale step happens.
152149
func TestExpDecaySampleNanosecondRegression(t *testing.T) {
153-
rand.Seed(1)
154150
s := NewExpDecaySample(100, 0.99)
155151
for i := 0; i < 100; i++ {
156152
s.Update(10)
@@ -183,8 +179,7 @@ func TestExpDecaySampleRescale(t *testing.T) {
183179

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

196191
func TestExpDecaySampleStatistics(t *testing.T) {
197192
now := time.Now()
198-
rand.Seed(1)
199-
s := NewExpDecaySample(100, 0.99)
193+
s := NewExpDecaySample(100, 0.99).(*ExpDecaySample).SetRand(rand.New(rand.NewSource(1)))
200194
for i := 1; i <= 10000; i++ {
201195
s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
202196
}
203197
testExpDecaySampleStatistics(t, s)
204198
}
205199

206200
func TestUniformSample(t *testing.T) {
207-
rand.Seed(1)
208201
s := NewUniformSample(100)
209202
for i := 0; i < 1000; i++ {
210203
s.Update(int64(i))
@@ -226,7 +219,6 @@ func TestUniformSample(t *testing.T) {
226219
}
227220

228221
func TestUniformSampleIncludesTail(t *testing.T) {
229-
rand.Seed(1)
230222
s := NewUniformSample(100)
231223
max := 100
232224
for i := 0; i < max; i++ {
@@ -244,7 +236,7 @@ func TestUniformSampleIncludesTail(t *testing.T) {
244236
}
245237

246238
func TestUniformSampleSnapshot(t *testing.T) {
247-
s := NewUniformSample(100)
239+
s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewSource(1)))
248240
for i := 1; i <= 10000; i++ {
249241
s.Update(int64(i))
250242
}
@@ -254,8 +246,7 @@ func TestUniformSampleSnapshot(t *testing.T) {
254246
}
255247

256248
func TestUniformSampleStatistics(t *testing.T) {
257-
rand.Seed(1)
258-
s := NewUniformSample(100)
249+
s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewSource(1)))
259250
for i := 1; i <= 10000; i++ {
260251
s.Update(int64(i))
261252
}
@@ -327,7 +318,7 @@ func testUniformSampleStatistics(t *testing.T, s Sample) {
327318
if ps[1] != 7380.5 {
328319
t.Errorf("75th percentile: 7380.5 != %v\n", ps[1])
329320
}
330-
if math.Abs(ps[2]-9986.429999999998) > epsilonPercentile {
321+
if math.Abs(9986.429999999998-ps[2]) > epsilonPercentile {
331322
t.Errorf("99th percentile: 9986.429999999998 != %v\n", ps[2])
332323
}
333324
}

0 commit comments

Comments
 (0)