mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 07:37:20 +00:00
metrics: reduce allocations for metrics (#33699)
This commit is contained in:
parent
e64c8d8e26
commit
ad459f4fac
12 changed files with 70 additions and 20 deletions
|
|
@ -7,7 +7,10 @@ import (
|
|||
// GetOrRegisterCounter returns an existing Counter or constructs and registers
|
||||
// a new Counter.
|
||||
func GetOrRegisterCounter(name string, r Registry) *Counter {
|
||||
return getOrRegister(name, NewCounter, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewCounter() }).(*Counter)
|
||||
}
|
||||
|
||||
// NewCounter constructs a new Counter.
|
||||
|
|
|
|||
|
|
@ -8,7 +8,10 @@ import (
|
|||
// GetOrRegisterCounterFloat64 returns an existing *CounterFloat64 or constructs and registers
|
||||
// a new CounterFloat64.
|
||||
func GetOrRegisterCounterFloat64(name string, r Registry) *CounterFloat64 {
|
||||
return getOrRegister(name, NewCounterFloat64, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewCounterFloat64() }).(*CounterFloat64)
|
||||
}
|
||||
|
||||
// NewCounterFloat64 constructs a new CounterFloat64.
|
||||
|
|
|
|||
|
|
@ -11,7 +11,10 @@ func (g GaugeSnapshot) Value() int64 { return int64(g) }
|
|||
// GetOrRegisterGauge returns an existing Gauge or constructs and registers a
|
||||
// new Gauge.
|
||||
func GetOrRegisterGauge(name string, r Registry) *Gauge {
|
||||
return getOrRegister(name, NewGauge, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewGauge() }).(*Gauge)
|
||||
}
|
||||
|
||||
// NewGauge constructs a new Gauge.
|
||||
|
|
|
|||
|
|
@ -8,7 +8,10 @@ import (
|
|||
// GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a
|
||||
// new GaugeFloat64.
|
||||
func GetOrRegisterGaugeFloat64(name string, r Registry) *GaugeFloat64 {
|
||||
return getOrRegister(name, NewGaugeFloat64, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewGaugeFloat64() }).(*GaugeFloat64)
|
||||
}
|
||||
|
||||
// GaugeFloat64Snapshot is a read-only copy of a GaugeFloat64.
|
||||
|
|
|
|||
|
|
@ -16,7 +16,10 @@ func (val GaugeInfoValue) String() string {
|
|||
// GetOrRegisterGaugeInfo returns an existing GaugeInfo or constructs and registers a
|
||||
// new GaugeInfo.
|
||||
func GetOrRegisterGaugeInfo(name string, r Registry) *GaugeInfo {
|
||||
return getOrRegister(name, NewGaugeInfo, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewGaugeInfo() }).(*GaugeInfo)
|
||||
}
|
||||
|
||||
// NewGaugeInfo constructs a new GaugeInfo.
|
||||
|
|
|
|||
|
|
@ -23,13 +23,19 @@ type Histogram interface {
|
|||
// GetOrRegisterHistogram returns an existing Histogram or constructs and
|
||||
// registers a new StandardHistogram.
|
||||
func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
|
||||
return getOrRegister(name, func() Histogram { return NewHistogram(s) }, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewHistogram(s) }).(Histogram)
|
||||
}
|
||||
|
||||
// GetOrRegisterHistogramLazy returns an existing Histogram or constructs and
|
||||
// registers a new StandardHistogram.
|
||||
func GetOrRegisterHistogramLazy(name string, r Registry, s func() Sample) Histogram {
|
||||
return getOrRegister(name, func() Histogram { return NewHistogram(s()) }, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewHistogram(s()) }).(Histogram)
|
||||
}
|
||||
|
||||
// NewHistogram constructs a new StandardHistogram from a Sample.
|
||||
|
|
|
|||
|
|
@ -12,7 +12,10 @@ import (
|
|||
// Be sure to unregister the meter from the registry once it is of no use to
|
||||
// allow for garbage collection.
|
||||
func GetOrRegisterMeter(name string, r Registry) *Meter {
|
||||
return getOrRegister(name, NewMeter, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewMeter() }).(*Meter)
|
||||
}
|
||||
|
||||
// NewMeter constructs a new Meter and launches a goroutine.
|
||||
|
|
|
|||
|
|
@ -345,13 +345,6 @@ func GetOrRegister(name string, i func() interface{}) interface{} {
|
|||
return DefaultRegistry.GetOrRegister(name, i)
|
||||
}
|
||||
|
||||
func getOrRegister[T any](name string, ctor func() T, r Registry) T {
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return ctor() }).(T)
|
||||
}
|
||||
|
||||
// Register the given metric under the given name. Returns a ErrDuplicateMetric
|
||||
// if a metric by the given name is already registered.
|
||||
func Register(name string, i interface{}) error {
|
||||
|
|
|
|||
|
|
@ -14,6 +14,31 @@ func BenchmarkRegistry(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkRegistryGetOrRegister(b *testing.B) {
|
||||
sample := func() Sample { return nil }
|
||||
tests := []struct {
|
||||
name string
|
||||
ctor func() any
|
||||
}{
|
||||
{name: "counter", ctor: func() any { return GetOrRegisterCounter("counter", DefaultRegistry) }},
|
||||
{name: "gauge", ctor: func() any { return GetOrRegisterGauge("gauge", DefaultRegistry) }},
|
||||
{name: "gaugefloat64", ctor: func() any { return GetOrRegisterGaugeFloat64("gaugefloat64", DefaultRegistry) }},
|
||||
{name: "histogram", ctor: func() any { return GetOrRegisterHistogram("histogram", DefaultRegistry, sample()) }},
|
||||
{name: "meter", ctor: func() any { return GetOrRegisterMeter("meter", DefaultRegistry) }},
|
||||
{name: "timer", ctor: func() any { return GetOrRegisterTimer("timer", DefaultRegistry) }},
|
||||
{name: "gaugeinfo", ctor: func() any { return GetOrRegisterGaugeInfo("gaugeinfo", DefaultRegistry) }},
|
||||
{name: "resettingtimer", ctor: func() any { return GetOrRegisterResettingTimer("resettingtimer", DefaultRegistry) }},
|
||||
{name: "runtimehistogramlazy", ctor: func() any { return GetOrRegisterHistogramLazy("runtimehistogramlazy", DefaultRegistry, sample) }},
|
||||
}
|
||||
for _, test := range tests {
|
||||
b.Run(test.name, func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
test.ctor()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRegistryGetOrRegisterParallel_8(b *testing.B) {
|
||||
benchmarkRegistryGetOrRegisterParallel(b, 8)
|
||||
}
|
||||
|
|
@ -268,7 +293,7 @@ func TestPrefixedChildRegistryGet(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestChildPrefixedRegistryRegister(t *testing.T) {
|
||||
r := NewPrefixedChildRegistry(DefaultRegistry, "prefix.")
|
||||
r := NewPrefixedChildRegistry(NewRegistry(), "prefix.")
|
||||
err := r.Register("foo", NewCounter())
|
||||
c := NewCounter()
|
||||
Register("bar", c)
|
||||
|
|
|
|||
|
|
@ -8,7 +8,10 @@ import (
|
|||
// GetOrRegisterResettingTimer returns an existing ResettingTimer or constructs and registers a
|
||||
// new ResettingTimer.
|
||||
func GetOrRegisterResettingTimer(name string, r Registry) *ResettingTimer {
|
||||
return getOrRegister(name, NewResettingTimer, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewResettingTimer() }).(*ResettingTimer)
|
||||
}
|
||||
|
||||
// NewRegisteredResettingTimer constructs and registers a new ResettingTimer.
|
||||
|
|
|
|||
|
|
@ -8,8 +8,10 @@ import (
|
|||
)
|
||||
|
||||
func getOrRegisterRuntimeHistogram(name string, scale float64, r Registry) *runtimeHistogram {
|
||||
constructor := func() Histogram { return newRuntimeHistogram(scale) }
|
||||
return getOrRegister(name, constructor, r).(*runtimeHistogram)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return newRuntimeHistogram(scale) }).(*runtimeHistogram)
|
||||
}
|
||||
|
||||
// runtimeHistogram wraps a runtime/metrics histogram.
|
||||
|
|
|
|||
|
|
@ -10,7 +10,10 @@ import (
|
|||
// Be sure to unregister the meter from the registry once it is of no use to
|
||||
// allow for garbage collection.
|
||||
func GetOrRegisterTimer(name string, r Registry) *Timer {
|
||||
return getOrRegister(name, NewTimer, r)
|
||||
if r == nil {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
return r.GetOrRegister(name, func() any { return NewTimer() }).(*Timer)
|
||||
}
|
||||
|
||||
// NewCustomTimer constructs a new Timer from a Histogram and a Meter.
|
||||
|
|
|
|||
Loading…
Reference in a new issue