mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
metrics: split ewma, create dedicated nil snapshot type
This commit is contained in:
parent
40f291b043
commit
2ed1f05ff5
13 changed files with 104 additions and 264 deletions
|
|
@ -83,19 +83,10 @@ func (c counterSnapshot) Count() int64 { return int64(c) }
|
|||
type NilCounter struct{}
|
||||
|
||||
// Clear is a no-op.
|
||||
func (NilCounter) Clear() {}
|
||||
|
||||
// Count is a no-op.
|
||||
func (NilCounter) Count() int64 { return 0 }
|
||||
|
||||
// Dec is a no-op.
|
||||
func (NilCounter) Dec(i int64) {}
|
||||
|
||||
// Inc is a no-op.
|
||||
func (NilCounter) Inc(i int64) {}
|
||||
|
||||
// Snapshot is a no-op.
|
||||
func (NilCounter) Snapshot() CounterSnapshot { return NilCounter{} }
|
||||
func (NilCounter) Clear() {}
|
||||
func (NilCounter) Dec(i int64) {}
|
||||
func (NilCounter) Inc(i int64) {}
|
||||
func (NilCounter) Snapshot() CounterSnapshot { return (*emptySnapshot)(nil) }
|
||||
|
||||
// StandardCounter is the standard implementation of a Counter and uses the
|
||||
// sync/atomic package to manage a single int64 value.
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
package metrics
|
||||
|
||||
const epsilon = 0.0000000000000001
|
||||
const epsilonPercentile = .00000000001
|
||||
|
|
@ -7,11 +7,14 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type EWMASnapshot interface {
|
||||
Rate() float64
|
||||
}
|
||||
|
||||
// EWMAs continuously calculate an exponentially-weighted moving average
|
||||
// based on an outside source of clock ticks.
|
||||
type EWMA interface {
|
||||
Rate() float64
|
||||
Snapshot() EWMA
|
||||
Snapshot() EWMASnapshot
|
||||
Tick()
|
||||
Update(int64)
|
||||
}
|
||||
|
|
@ -36,40 +39,19 @@ func NewEWMA15() EWMA {
|
|||
return NewEWMA(1 - math.Exp(-5.0/60.0/15))
|
||||
}
|
||||
|
||||
// EWMASnapshot is a read-only copy of another EWMA.
|
||||
type EWMASnapshot float64
|
||||
// eWMASnapshot is a read-only copy of another EWMA.
|
||||
type eWMASnapshot float64
|
||||
|
||||
// Rate returns the rate of events per second at the time the snapshot was
|
||||
// taken.
|
||||
func (a EWMASnapshot) Rate() float64 { return float64(a) }
|
||||
|
||||
// Snapshot returns the snapshot.
|
||||
func (a EWMASnapshot) Snapshot() EWMA { return a }
|
||||
|
||||
// Tick panics.
|
||||
func (EWMASnapshot) Tick() {
|
||||
panic("Tick called on an EWMASnapshot")
|
||||
}
|
||||
|
||||
// Update panics.
|
||||
func (EWMASnapshot) Update(int64) {
|
||||
panic("Update called on an EWMASnapshot")
|
||||
}
|
||||
func (a eWMASnapshot) Rate() float64 { return float64(a) }
|
||||
|
||||
// NilEWMA is a no-op EWMA.
|
||||
type NilEWMA struct{}
|
||||
|
||||
// Rate is a no-op.
|
||||
func (NilEWMA) Rate() float64 { return 0.0 }
|
||||
|
||||
// Snapshot is a no-op.
|
||||
func (NilEWMA) Snapshot() EWMA { return NilEWMA{} }
|
||||
|
||||
// Tick is a no-op.
|
||||
func (NilEWMA) Tick() {}
|
||||
|
||||
// Update is a no-op.
|
||||
func (NilEWMA) Update(n int64) {}
|
||||
func (NilEWMA) Snapshot() EWMASnapshot { return (*emptySnapshot)(nil) }
|
||||
func (NilEWMA) Tick() {}
|
||||
func (NilEWMA) Update(n int64) {}
|
||||
|
||||
// StandardEWMA is the standard implementation of an EWMA and tracks the number
|
||||
// of uncounted events and processes them on each tick. It uses the
|
||||
|
|
@ -82,14 +64,10 @@ type StandardEWMA struct {
|
|||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
// Rate returns the moving average rate of events per second.
|
||||
func (a *StandardEWMA) Rate() float64 {
|
||||
return math.Float64frombits(a.rate.Load()) * float64(time.Second)
|
||||
}
|
||||
|
||||
// Snapshot returns a read-only copy of the EWMA.
|
||||
func (a *StandardEWMA) Snapshot() EWMA {
|
||||
return EWMASnapshot(a.Rate())
|
||||
func (a *StandardEWMA) Snapshot() EWMASnapshot {
|
||||
r := math.Float64frombits(a.rate.Load()) * float64(time.Second)
|
||||
return eWMASnapshot(r)
|
||||
}
|
||||
|
||||
// Tick ticks the clock to update the moving average. It assumes it is called
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
const epsilon = 0.0000000000000001
|
||||
|
||||
func BenchmarkEWMA(b *testing.B) {
|
||||
a := NewEWMA1()
|
||||
b.ResetTimer()
|
||||
|
|
|
|||
|
|
@ -52,22 +52,11 @@ func (g gaugeSnapshot) Value() int64 { return int64(g) }
|
|||
// NilGauge is a no-op Gauge.
|
||||
type NilGauge struct{}
|
||||
|
||||
// Snapshot is a no-op.
|
||||
func (NilGauge) Snapshot() GaugeSnapshot { return NilGauge{} }
|
||||
|
||||
// Update is a no-op.
|
||||
func (NilGauge) Update(v int64) {}
|
||||
|
||||
func (NilGauge) UpdateIfGt(v int64) {}
|
||||
|
||||
// Dec is a no-op.
|
||||
func (NilGauge) Dec(i int64) {}
|
||||
|
||||
// Inc is a no-op.
|
||||
func (NilGauge) Inc(i int64) {}
|
||||
|
||||
// Value is a no-op.
|
||||
func (NilGauge) Value() int64 { return 0 }
|
||||
func (NilGauge) Snapshot() GaugeSnapshot { return (*emptySnapshot)(nil) }
|
||||
func (NilGauge) Update(v int64) {}
|
||||
func (NilGauge) UpdateIfGt(v int64) {}
|
||||
func (NilGauge) Dec(i int64) {}
|
||||
func (NilGauge) Inc(i int64) {}
|
||||
|
||||
// StandardGauge is the standard implementation of a Gauge and uses the
|
||||
// sync/atomic package to manage a single int64 value.
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ type HistogramSnapshot interface {
|
|||
Min() int64
|
||||
Percentile(float64) float64
|
||||
Percentiles([]float64) []float64
|
||||
Sample() SampleSnapshot
|
||||
StdDev() float64
|
||||
Sum() int64
|
||||
Variance() float64
|
||||
|
|
@ -90,9 +89,6 @@ func (h *histogramSnapshot) Percentiles(ps []float64) []float64 {
|
|||
return h.sample.Percentiles(ps)
|
||||
}
|
||||
|
||||
// Sample returns the Sample underlying the histogram.
|
||||
func (h *histogramSnapshot) Sample() SampleSnapshot { return h.sample }
|
||||
|
||||
// Snapshot returns the snapshot.
|
||||
func (h *histogramSnapshot) Snapshot() HistogramSnapshot { return h }
|
||||
|
||||
|
|
@ -109,46 +105,9 @@ func (h *histogramSnapshot) Variance() float64 { return h.sample.Variance() }
|
|||
// NilHistogram is a no-op Histogram.
|
||||
type NilHistogram struct{}
|
||||
|
||||
// Clear is a no-op.
|
||||
func (NilHistogram) Clear() {}
|
||||
|
||||
// Count is a no-op.
|
||||
func (NilHistogram) Count() int64 { return 0 }
|
||||
|
||||
// Max is a no-op.
|
||||
func (NilHistogram) Max() int64 { return 0 }
|
||||
|
||||
// Mean is a no-op.
|
||||
func (NilHistogram) Mean() float64 { return 0.0 }
|
||||
|
||||
// Min is a no-op.
|
||||
func (NilHistogram) Min() int64 { return 0 }
|
||||
|
||||
// Percentile is a no-op.
|
||||
func (NilHistogram) Percentile(p float64) float64 { return 0.0 }
|
||||
|
||||
// Percentiles is a no-op.
|
||||
func (NilHistogram) Percentiles(ps []float64) []float64 {
|
||||
return make([]float64, len(ps))
|
||||
}
|
||||
|
||||
// Sample is a no-op.
|
||||
func (NilHistogram) Sample() SampleSnapshot { return NilSample{} }
|
||||
|
||||
// Snapshot is a no-op.
|
||||
func (NilHistogram) Snapshot() HistogramSnapshot { return NilHistogram{} }
|
||||
|
||||
// StdDev is a no-op.
|
||||
func (NilHistogram) StdDev() float64 { return 0.0 }
|
||||
|
||||
// Sum is a no-op.
|
||||
func (NilHistogram) Sum() int64 { return 0 }
|
||||
|
||||
// Update is a no-op.
|
||||
func (NilHistogram) Update(v int64) {}
|
||||
|
||||
// Variance is a no-op.
|
||||
func (NilHistogram) Variance() float64 { return 0.0 }
|
||||
func (NilHistogram) Clear() {}
|
||||
func (NilHistogram) Snapshot() HistogramSnapshot { return (*emptySnapshot)(nil) }
|
||||
func (NilHistogram) Update(v int64) {}
|
||||
|
||||
// StandardHistogram is the standard implementation of a Histogram and uses a
|
||||
// Sample to bound its memory use.
|
||||
|
|
|
|||
48
metrics/inactive.go
Normal file
48
metrics/inactive.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
// Copyright 2023 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package metrics
|
||||
|
||||
// compile-time checks that interfaces are implemented.
|
||||
var (
|
||||
_ SampleSnapshot = (*emptySnapshot)(nil)
|
||||
_ HistogramSnapshot = (*emptySnapshot)(nil)
|
||||
_ CounterSnapshot = (*emptySnapshot)(nil)
|
||||
_ GaugeSnapshot = (*emptySnapshot)(nil)
|
||||
_ MeterSnapshot = (*emptySnapshot)(nil)
|
||||
_ EWMASnapshot = (*emptySnapshot)(nil)
|
||||
_ TimerSnapshot = (*emptySnapshot)(nil)
|
||||
)
|
||||
|
||||
type emptySnapshot struct{}
|
||||
|
||||
func (*emptySnapshot) Count() int64 { return 0 }
|
||||
func (*emptySnapshot) Max() int64 { return 0 }
|
||||
func (*emptySnapshot) Mean() float64 { return 0.0 }
|
||||
func (*emptySnapshot) Min() int64 { return 0 }
|
||||
func (*emptySnapshot) Percentile(p float64) float64 { return 0.0 }
|
||||
func (*emptySnapshot) Percentiles(ps []float64) []float64 { return make([]float64, len(ps)) }
|
||||
func (*emptySnapshot) Size() int { return 0 }
|
||||
func (*emptySnapshot) StdDev() float64 { return 0.0 }
|
||||
func (*emptySnapshot) Sum() int64 { return 0 }
|
||||
func (*emptySnapshot) Values() []int64 { return []int64{} }
|
||||
func (*emptySnapshot) Variance() float64 { return 0.0 }
|
||||
func (*emptySnapshot) Value() int64 { return 0 }
|
||||
func (*emptySnapshot) Rate() float64 { return 0.0 }
|
||||
func (*emptySnapshot) Rate1() float64 { return 0.0 }
|
||||
func (*emptySnapshot) Rate5() float64 { return 0.0 }
|
||||
func (*emptySnapshot) Rate15() float64 { return 0.0 }
|
||||
func (*emptySnapshot) RateMean() float64 { return 0.0 }
|
||||
|
|
@ -61,10 +61,10 @@ func (rep *Reporter) Run() {
|
|||
|
||||
// calculate sum of squares from data provided by metrics.Histogram
|
||||
// see http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
|
||||
func sumSquares(s metrics.SampleSnapshot) float64 {
|
||||
count := float64(s.Count())
|
||||
sumSquared := math.Pow(count*s.Mean(), 2)
|
||||
sumSquares := math.Pow(count*s.StdDev(), 2) + sumSquared/count
|
||||
func sumSquares(icount, mean, stDev) float64 {
|
||||
count := float64(icount)
|
||||
sumSquared := math.Pow(count*mean, 2)
|
||||
sumSquares := math.Pow(count*stDev, 2) + sumSquared/count
|
||||
if math.IsNaN(sumSquares) {
|
||||
return 0.0
|
||||
}
|
||||
|
|
@ -135,13 +135,12 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (snapshot B
|
|||
ms := m.Snapshot()
|
||||
if ms.Count() > 0 {
|
||||
gauges := make([]Measurement, histogramGaugeCount)
|
||||
s := ms.Sample()
|
||||
measurement[Name] = fmt.Sprintf("%s.%s", name, "hist")
|
||||
measurement[Count] = uint64(s.Count())
|
||||
measurement[Max] = float64(s.Max())
|
||||
measurement[Min] = float64(s.Min())
|
||||
measurement[Sum] = float64(s.Sum())
|
||||
measurement[SumSquares] = sumSquares(s)
|
||||
measurement[Count] = uint64(ms.Count())
|
||||
measurement[Max] = float64(ms.Max())
|
||||
measurement[Min] = float64(ms.Min())
|
||||
measurement[Sum] = float64(ms.Sum())
|
||||
measurement[SumSquares] = sumSquares(ms.Count(), ms.Mean(), ms.StdDev())
|
||||
gauges[0] = measurement
|
||||
for i, p := range rep.Percentiles {
|
||||
gauges[i+1] = Measurement{
|
||||
|
|
|
|||
|
|
@ -97,29 +97,10 @@ func (m *meterSnapshot) RateMean() float64 { return m.rateMean }
|
|||
// NilMeter is a no-op Meter.
|
||||
type NilMeter struct{}
|
||||
|
||||
// Count is a no-op.
|
||||
func (NilMeter) Count() int64 { return 0 }
|
||||
|
||||
// Mark is a no-op.
|
||||
func (NilMeter) Mark(n int64) {}
|
||||
|
||||
// Rate1 is a no-op.
|
||||
func (NilMeter) Rate1() float64 { return 0.0 }
|
||||
|
||||
// Rate5 is a no-op.
|
||||
func (NilMeter) Rate5() float64 { return 0.0 }
|
||||
|
||||
// Rate15 is a no-op.
|
||||
func (NilMeter) Rate15() float64 { return 0.0 }
|
||||
|
||||
// RateMean is a no-op.
|
||||
func (NilMeter) RateMean() float64 { return 0.0 }
|
||||
|
||||
// Snapshot is a no-op.
|
||||
func (NilMeter) Snapshot() MeterSnapshot { return NilMeter{} }
|
||||
|
||||
// Stop is a no-op.
|
||||
func (NilMeter) Stop() {}
|
||||
func (NilMeter) Count() int64 { return 0 }
|
||||
func (NilMeter) Mark(n int64) {}
|
||||
func (NilMeter) Snapshot() MeterSnapshot { return (*emptySnapshot)(nil) }
|
||||
func (NilMeter) Stop() {}
|
||||
|
||||
// StandardMeter is the standard implementation of a Meter.
|
||||
type StandardMeter struct {
|
||||
|
|
@ -159,9 +140,9 @@ func (m *StandardMeter) Mark(n int64) {
|
|||
func (m *StandardMeter) Snapshot() MeterSnapshot {
|
||||
return &meterSnapshot{
|
||||
count: m.count.Load() + m.uncounted.Load(),
|
||||
rate1: m.a1.Rate(),
|
||||
rate5: m.a5.Rate(),
|
||||
rate15: m.a15.Rate(),
|
||||
rate1: m.a1.Snapshot().Rate(),
|
||||
rate5: m.a5.Snapshot().Rate(),
|
||||
rate15: m.a15.Snapshot().Rate(),
|
||||
rateMean: math.Float64frombits(m.rateMean.Load()),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,10 +65,6 @@ func (h *runtimeHistogram) Snapshot() HistogramSnapshot {
|
|||
|
||||
type runtimeHistogramSnapshot metrics.Float64Histogram
|
||||
|
||||
func (h *runtimeHistogramSnapshot) Sample() SampleSnapshot {
|
||||
return NilSample{}
|
||||
}
|
||||
|
||||
func (h *runtimeHistogramSnapshot) Snapshot() HistogramSnapshot {
|
||||
return h
|
||||
}
|
||||
|
|
|
|||
|
|
@ -145,49 +145,9 @@ func (s *ExpDecaySample) update(t time.Time, v int64) {
|
|||
// NilSample is a no-op Sample.
|
||||
type NilSample struct{}
|
||||
|
||||
// Clear is a no-op.
|
||||
func (NilSample) Clear() {}
|
||||
|
||||
// Count is a no-op.
|
||||
func (NilSample) Count() int64 { return 0 }
|
||||
|
||||
// Max is a no-op.
|
||||
func (NilSample) Max() int64 { return 0 }
|
||||
|
||||
// Mean is a no-op.
|
||||
func (NilSample) Mean() float64 { return 0.0 }
|
||||
|
||||
// Min is a no-op.
|
||||
func (NilSample) Min() int64 { return 0 }
|
||||
|
||||
// Percentile is a no-op.
|
||||
func (NilSample) Percentile(p float64) float64 { return 0.0 }
|
||||
|
||||
// Percentiles is a no-op.
|
||||
func (NilSample) Percentiles(ps []float64) []float64 {
|
||||
return make([]float64, len(ps))
|
||||
}
|
||||
|
||||
// Size is a no-op.
|
||||
func (NilSample) Size() int { return 0 }
|
||||
|
||||
// Sample is a no-op.
|
||||
func (NilSample) Snapshot() SampleSnapshot { return NilSample{} }
|
||||
|
||||
// StdDev is a no-op.
|
||||
func (NilSample) StdDev() float64 { return 0.0 }
|
||||
|
||||
// Sum is a no-op.
|
||||
func (NilSample) Sum() int64 { return 0 }
|
||||
|
||||
// Update is a no-op.
|
||||
func (NilSample) Update(v int64) {}
|
||||
|
||||
// Values is a no-op.
|
||||
func (NilSample) Values() []int64 { return []int64{} }
|
||||
|
||||
// Variance is a no-op.
|
||||
func (NilSample) Variance() float64 { return 0.0 }
|
||||
func (NilSample) Clear() {}
|
||||
func (NilSample) Snapshot() SampleSnapshot { return (*emptySnapshot)(nil) }
|
||||
func (NilSample) Update(v int64) {}
|
||||
|
||||
// CalculatePercentiles returns an arbitrary percentile of the slice of int64.
|
||||
func SamplePercentile(values []int64, p float64) float64 {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
const epsilonPercentile = .00000000001
|
||||
|
||||
// Benchmark{Compute,Copy}{1000,1000000} demonstrate that, even for relatively
|
||||
// expensive computations like Variance, the cost of copying the Sample, as
|
||||
// approximated by a make and copy, is much greater than the cost of the
|
||||
|
|
|
|||
|
|
@ -6,19 +6,8 @@ import (
|
|||
)
|
||||
|
||||
type TimerSnapshot interface {
|
||||
Count() int64
|
||||
Max() int64
|
||||
Mean() float64
|
||||
Min() int64
|
||||
Percentile(float64) float64
|
||||
Percentiles([]float64) []float64
|
||||
Rate1() float64
|
||||
Rate5() float64
|
||||
Rate15() float64
|
||||
RateMean() float64
|
||||
Sum() int64
|
||||
Variance() float64
|
||||
StdDev() float64
|
||||
HistogramSnapshot
|
||||
MeterSnapshot
|
||||
}
|
||||
|
||||
// Timers capture the duration and rate of events.
|
||||
|
|
@ -81,61 +70,11 @@ func NewTimer() Timer {
|
|||
// NilTimer is a no-op Timer.
|
||||
type NilTimer struct{}
|
||||
|
||||
// Count is a no-op.
|
||||
func (NilTimer) Count() int64 { return 0 }
|
||||
|
||||
// Max is a no-op.
|
||||
func (NilTimer) Max() int64 { return 0 }
|
||||
|
||||
// Mean is a no-op.
|
||||
func (NilTimer) Mean() float64 { return 0.0 }
|
||||
|
||||
// Min is a no-op.
|
||||
func (NilTimer) Min() int64 { return 0 }
|
||||
|
||||
// Percentile is a no-op.
|
||||
func (NilTimer) Percentile(p float64) float64 { return 0.0 }
|
||||
|
||||
// Percentiles is a no-op.
|
||||
func (NilTimer) Percentiles(ps []float64) []float64 {
|
||||
return make([]float64, len(ps))
|
||||
}
|
||||
|
||||
// Rate1 is a no-op.
|
||||
func (NilTimer) Rate1() float64 { return 0.0 }
|
||||
|
||||
// Rate5 is a no-op.
|
||||
func (NilTimer) Rate5() float64 { return 0.0 }
|
||||
|
||||
// Rate15 is a no-op.
|
||||
func (NilTimer) Rate15() float64 { return 0.0 }
|
||||
|
||||
// RateMean is a no-op.
|
||||
func (NilTimer) RateMean() float64 { return 0.0 }
|
||||
|
||||
// Snapshot is a no-op.
|
||||
func (NilTimer) Snapshot() TimerSnapshot { return NilTimer{} }
|
||||
|
||||
// StdDev is a no-op.
|
||||
func (NilTimer) StdDev() float64 { return 0.0 }
|
||||
|
||||
// Stop is a no-op.
|
||||
func (NilTimer) Stop() {}
|
||||
|
||||
// Sum is a no-op.
|
||||
func (NilTimer) Sum() int64 { return 0 }
|
||||
|
||||
// Time is a no-op.
|
||||
func (NilTimer) Time(f func()) { f() }
|
||||
|
||||
// Update is a no-op.
|
||||
func (NilTimer) Update(time.Duration) {}
|
||||
|
||||
// UpdateSince is a no-op.
|
||||
func (NilTimer) UpdateSince(time.Time) {}
|
||||
|
||||
// Variance is a no-op.
|
||||
func (NilTimer) Variance() float64 { return 0.0 }
|
||||
func (NilTimer) Snapshot() TimerSnapshot { return (*emptySnapshot)(nil) }
|
||||
func (NilTimer) Stop() {}
|
||||
func (NilTimer) Time(f func()) { f() }
|
||||
func (NilTimer) Update(time.Duration) {}
|
||||
func (NilTimer) UpdateSince(time.Time) {}
|
||||
|
||||
// StandardTimer is the standard implementation of a Timer and uses a Histogram
|
||||
// and Meter.
|
||||
|
|
|
|||
Loading…
Reference in a new issue