mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
metrics: remove deprecated uses of math.rand (#26710)
This commit is contained in:
parent
76320b4b98
commit
6199c84050
2 changed files with 32 additions and 16 deletions
|
|
@ -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
|
||||
|
|
@ -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()
|
||||
|
|
@ -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) {
|
||||
|
|
@ -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
|
||||
|
|
@ -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()
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
@ -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))
|
||||
|
|
@ -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))
|
||||
|
|
@ -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)
|
||||
|
|
@ -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))
|
||||
}
|
||||
|
|
@ -195,8 +190,7 @@ 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))
|
||||
}
|
||||
|
|
@ -204,7 +198,6 @@ func TestExpDecaySampleStatistics(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUniformSample(t *testing.T) {
|
||||
rand.Seed(1)
|
||||
s := NewUniformSample(100)
|
||||
for i := 0; i < 1000; i++ {
|
||||
s.Update(int64(i))
|
||||
|
|
@ -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++ {
|
||||
|
|
@ -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))
|
||||
}
|
||||
|
|
@ -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))
|
||||
}
|
||||
|
|
@ -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])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue