mirror of
https://gitlab.com/pulsechaincom/go-pulse.git
synced 2024-12-22 03:30:35 +00:00
8b6cf128af
This change includes a lot of things, listed below. ### Split up interfaces, write vs read The interfaces have been split up into one write-interface and one read-interface, with `Snapshot` being the gateway from write to read. This simplifies the semantics _a lot_. Example of splitting up an interface into one readonly 'snapshot' part, and one updatable writeonly part: ```golang type MeterSnapshot interface { Count() int64 Rate1() float64 Rate5() float64 Rate15() float64 RateMean() float64 } // Meters count events to produce exponentially-weighted moving average rates // at one-, five-, and fifteen-minutes and a mean rate. type Meter interface { Mark(int64) Snapshot() MeterSnapshot Stop() } ``` ### A note about concurrency This PR makes the concurrency model clearer. We have actual meters and snapshot of meters. The `meter` is the thing which can be accessed from the registry, and updates can be made to it. - For all `meters`, (`Gauge`, `Timer` etc), it is assumed that they are accessed by different threads, making updates. Therefore, all `meters` update-methods (`Inc`, `Add`, `Update`, `Clear` etc) need to be concurrency-safe. - All `meters` have a `Snapshot()` method. This method is _usually_ called from one thread, a backend-exporter. But it's fully possible to have several exporters simultaneously: therefore this method should also be concurrency-safe. TLDR: `meter`s are accessible via registry, all their methods must be concurrency-safe. For all `Snapshot`s, it is assumed that an individual exporter-thread has obtained a `meter` from the registry, and called the `Snapshot` method to obtain a readonly snapshot. This snapshot is _not_ guaranteed to be concurrency-safe. There's no need for a snapshot to be concurrency-safe, since exporters should not share snapshots. Note, though: that by happenstance a lot of the snapshots _are_ concurrency-safe, being unmutable minimal representations of a value. Only the more complex ones are _not_ threadsafe, those that lazily calculate things like `Variance()`, `Mean()`. Example of how a background exporter typically works, obtaining the snapshot and sequentially accessing the non-threadsafe methods in it: ```golang ms := metric.Snapshot() ... fields := map[string]interface{}{ "count": ms.Count(), "max": ms.Max(), "mean": ms.Mean(), "min": ms.Min(), "stddev": ms.StdDev(), "variance": ms.Variance(), ``` TLDR: `snapshots` are not guaranteed to be concurrency-safe (but often are). ### Sample changes I also changed the `Sample` type: previously, it iterated the samples fully every time `Mean()`,`Sum()`, `Min()` or `Max()` was invoked. Since we now have readonly base data, we can just iterate it once, in the constructor, and set all four values at once. The same thing has been done for runtimehistogram. ### ResettingTimer API Back when ResettingTImer was implemented, as part of https://github.com/ethereum/go-ethereum/pull/15910, Anton implemented a `Percentiles` on the new type. However, the method did not conform to the other existing types which also had a `Percentiles`. 1. The existing ones, on input, took `0.5` to mean `50%`. Anton used `50` to mean `50%`. 2. The existing ones returned `float64` outputs, thus interpolating between values. A value-set of `0, 10`, at `50%` would return `5`, whereas Anton's would return either `0` or `10`. This PR removes the 'new' version, and uses only the 'legacy' percentiles, also for the ResettingTimer type. The resetting timer snapshot was also defined so that it would expose the internal values. This has been removed, and getters for `Max, Min, Mean` have been added instead. ### Unexport types A lot of types were exported, but do not need to be. This PR unexports quite a lot of them.
361 lines
8.8 KiB
Go
361 lines
8.8 KiB
Go
package metrics
|
|
|
|
import (
|
|
"math"
|
|
"math/rand"
|
|
"runtime"
|
|
"testing"
|
|
"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
|
|
// computation for small samples and only slightly less for large samples.
|
|
func BenchmarkCompute1000(b *testing.B) {
|
|
s := make([]int64, 1000)
|
|
var sum int64
|
|
for i := 0; i < len(s); i++ {
|
|
s[i] = int64(i)
|
|
sum += int64(i)
|
|
}
|
|
mean := float64(sum) / float64(len(s))
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
SampleVariance(mean, s)
|
|
}
|
|
}
|
|
func BenchmarkCompute1000000(b *testing.B) {
|
|
s := make([]int64, 1000000)
|
|
var sum int64
|
|
for i := 0; i < len(s); i++ {
|
|
s[i] = int64(i)
|
|
sum += int64(i)
|
|
}
|
|
mean := float64(sum) / float64(len(s))
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
SampleVariance(mean, s)
|
|
}
|
|
}
|
|
func BenchmarkCopy1000(b *testing.B) {
|
|
s := make([]int64, 1000)
|
|
for i := 0; i < len(s); i++ {
|
|
s[i] = int64(i)
|
|
}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
sCopy := make([]int64, len(s))
|
|
copy(sCopy, s)
|
|
}
|
|
}
|
|
func BenchmarkCopy1000000(b *testing.B) {
|
|
s := make([]int64, 1000000)
|
|
for i := 0; i < len(s); i++ {
|
|
s[i] = int64(i)
|
|
}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
sCopy := make([]int64, len(s))
|
|
copy(sCopy, s)
|
|
}
|
|
}
|
|
|
|
func BenchmarkExpDecaySample257(b *testing.B) {
|
|
benchmarkSample(b, NewExpDecaySample(257, 0.015))
|
|
}
|
|
|
|
func BenchmarkExpDecaySample514(b *testing.B) {
|
|
benchmarkSample(b, NewExpDecaySample(514, 0.015))
|
|
}
|
|
|
|
func BenchmarkExpDecaySample1028(b *testing.B) {
|
|
benchmarkSample(b, NewExpDecaySample(1028, 0.015))
|
|
}
|
|
|
|
func BenchmarkUniformSample257(b *testing.B) {
|
|
benchmarkSample(b, NewUniformSample(257))
|
|
}
|
|
|
|
func BenchmarkUniformSample514(b *testing.B) {
|
|
benchmarkSample(b, NewUniformSample(514))
|
|
}
|
|
|
|
func BenchmarkUniformSample1028(b *testing.B) {
|
|
benchmarkSample(b, NewUniformSample(1028))
|
|
}
|
|
|
|
func min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func TestExpDecaySample(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
reservoirSize int
|
|
alpha float64
|
|
updates int
|
|
}{
|
|
{100, 0.99, 10},
|
|
{1000, 0.01, 100},
|
|
{100, 0.99, 1000},
|
|
} {
|
|
sample := NewExpDecaySample(tc.reservoirSize, tc.alpha)
|
|
for i := 0; i < tc.updates; i++ {
|
|
sample.Update(int64(i))
|
|
}
|
|
snap := sample.Snapshot()
|
|
if have, want := int(snap.Count()), tc.updates; have != want {
|
|
t.Errorf("have %d want %d", have, want)
|
|
}
|
|
if have, want := snap.Size(), min(tc.updates, tc.reservoirSize); have != want {
|
|
t.Errorf("have %d want %d", have, want)
|
|
}
|
|
values := snap.(*sampleSnapshot).values
|
|
if have, want := len(values), min(tc.updates, tc.reservoirSize); have != want {
|
|
t.Errorf("have %d want %d", have, want)
|
|
}
|
|
for _, v := range values {
|
|
if v > int64(tc.updates) || v < 0 {
|
|
t.Errorf("out of range [0, %d): %v", tc.updates, v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// This test makes sure that the sample's priority is not amplified by using
|
|
// nanosecond duration since start rather than second duration since start.
|
|
// 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) {
|
|
sw := NewExpDecaySample(100, 0.99)
|
|
for i := 0; i < 100; i++ {
|
|
sw.Update(10)
|
|
}
|
|
time.Sleep(1 * time.Millisecond)
|
|
for i := 0; i < 100; i++ {
|
|
sw.Update(20)
|
|
}
|
|
s := sw.Snapshot()
|
|
v := s.(*sampleSnapshot).values
|
|
avg := float64(0)
|
|
for i := 0; i < len(v); i++ {
|
|
avg += float64(v[i])
|
|
}
|
|
avg /= float64(len(v))
|
|
if avg > 16 || avg < 14 {
|
|
t.Errorf("out of range [14, 16]: %v\n", avg)
|
|
}
|
|
}
|
|
|
|
func TestExpDecaySampleRescale(t *testing.T) {
|
|
s := NewExpDecaySample(2, 0.001).(*ExpDecaySample)
|
|
s.update(time.Now(), 1)
|
|
s.update(time.Now().Add(time.Hour+time.Microsecond), 1)
|
|
for _, v := range s.values.Values() {
|
|
if v.k == 0.0 {
|
|
t.Fatal("v.k == 0.0")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExpDecaySampleSnapshot(t *testing.T) {
|
|
now := time.Now()
|
|
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))
|
|
}
|
|
snapshot := s.Snapshot()
|
|
s.Update(1)
|
|
testExpDecaySampleStatistics(t, snapshot)
|
|
}
|
|
|
|
func TestExpDecaySampleStatistics(t *testing.T) {
|
|
now := time.Now()
|
|
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.Snapshot())
|
|
}
|
|
|
|
func TestUniformSample(t *testing.T) {
|
|
sw := NewUniformSample(100)
|
|
for i := 0; i < 1000; i++ {
|
|
sw.Update(int64(i))
|
|
}
|
|
s := sw.Snapshot()
|
|
if size := s.Count(); size != 1000 {
|
|
t.Errorf("s.Count(): 1000 != %v\n", size)
|
|
}
|
|
if size := s.Size(); size != 100 {
|
|
t.Errorf("s.Size(): 100 != %v\n", size)
|
|
}
|
|
values := s.(*sampleSnapshot).values
|
|
|
|
if l := len(values); l != 100 {
|
|
t.Errorf("len(s.Values()): 100 != %v\n", l)
|
|
}
|
|
for _, v := range values {
|
|
if v > 1000 || v < 0 {
|
|
t.Errorf("out of range [0, 100): %v\n", v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUniformSampleIncludesTail(t *testing.T) {
|
|
sw := NewUniformSample(100)
|
|
max := 100
|
|
for i := 0; i < max; i++ {
|
|
sw.Update(int64(i))
|
|
}
|
|
s := sw.Snapshot()
|
|
v := s.(*sampleSnapshot).values
|
|
sum := 0
|
|
exp := (max - 1) * max / 2
|
|
for i := 0; i < len(v); i++ {
|
|
sum += int(v[i])
|
|
}
|
|
if exp != sum {
|
|
t.Errorf("sum: %v != %v\n", exp, sum)
|
|
}
|
|
}
|
|
|
|
func TestUniformSampleSnapshot(t *testing.T) {
|
|
s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewSource(1)))
|
|
for i := 1; i <= 10000; i++ {
|
|
s.Update(int64(i))
|
|
}
|
|
snapshot := s.Snapshot()
|
|
s.Update(1)
|
|
testUniformSampleStatistics(t, snapshot)
|
|
}
|
|
|
|
func TestUniformSampleStatistics(t *testing.T) {
|
|
s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewSource(1)))
|
|
for i := 1; i <= 10000; i++ {
|
|
s.Update(int64(i))
|
|
}
|
|
testUniformSampleStatistics(t, s.Snapshot())
|
|
}
|
|
|
|
func benchmarkSample(b *testing.B, s Sample) {
|
|
var memStats runtime.MemStats
|
|
runtime.ReadMemStats(&memStats)
|
|
pauseTotalNs := memStats.PauseTotalNs
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
s.Update(1)
|
|
}
|
|
b.StopTimer()
|
|
runtime.GC()
|
|
runtime.ReadMemStats(&memStats)
|
|
b.Logf("GC cost: %d ns/op", int(memStats.PauseTotalNs-pauseTotalNs)/b.N)
|
|
}
|
|
|
|
func testExpDecaySampleStatistics(t *testing.T, s SampleSnapshot) {
|
|
if count := s.Count(); count != 10000 {
|
|
t.Errorf("s.Count(): 10000 != %v\n", count)
|
|
}
|
|
if min := s.Min(); min != 107 {
|
|
t.Errorf("s.Min(): 107 != %v\n", min)
|
|
}
|
|
if max := s.Max(); max != 10000 {
|
|
t.Errorf("s.Max(): 10000 != %v\n", max)
|
|
}
|
|
if mean := s.Mean(); mean != 4965.98 {
|
|
t.Errorf("s.Mean(): 4965.98 != %v\n", mean)
|
|
}
|
|
if stdDev := s.StdDev(); stdDev != 2959.825156930727 {
|
|
t.Errorf("s.StdDev(): 2959.825156930727 != %v\n", stdDev)
|
|
}
|
|
ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
|
|
if ps[0] != 4615 {
|
|
t.Errorf("median: 4615 != %v\n", ps[0])
|
|
}
|
|
if ps[1] != 7672 {
|
|
t.Errorf("75th percentile: 7672 != %v\n", ps[1])
|
|
}
|
|
if ps[2] != 9998.99 {
|
|
t.Errorf("99th percentile: 9998.99 != %v\n", ps[2])
|
|
}
|
|
}
|
|
|
|
func testUniformSampleStatistics(t *testing.T, s SampleSnapshot) {
|
|
if count := s.Count(); count != 10000 {
|
|
t.Errorf("s.Count(): 10000 != %v\n", count)
|
|
}
|
|
if min := s.Min(); min != 37 {
|
|
t.Errorf("s.Min(): 37 != %v\n", min)
|
|
}
|
|
if max := s.Max(); max != 9989 {
|
|
t.Errorf("s.Max(): 9989 != %v\n", max)
|
|
}
|
|
if mean := s.Mean(); mean != 4748.14 {
|
|
t.Errorf("s.Mean(): 4748.14 != %v\n", mean)
|
|
}
|
|
if stdDev := s.StdDev(); stdDev != 2826.684117548333 {
|
|
t.Errorf("s.StdDev(): 2826.684117548333 != %v\n", stdDev)
|
|
}
|
|
ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
|
|
if ps[0] != 4599 {
|
|
t.Errorf("median: 4599 != %v\n", ps[0])
|
|
}
|
|
if ps[1] != 7380.5 {
|
|
t.Errorf("75th percentile: 7380.5 != %v\n", ps[1])
|
|
}
|
|
if math.Abs(9986.429999999998-ps[2]) > epsilonPercentile {
|
|
t.Errorf("99th percentile: 9986.429999999998 != %v\n", ps[2])
|
|
}
|
|
}
|
|
|
|
// TestUniformSampleConcurrentUpdateCount would expose data race problems with
|
|
// concurrent Update and Count calls on Sample when test is called with -race
|
|
// argument
|
|
func TestUniformSampleConcurrentUpdateCount(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping in short mode")
|
|
}
|
|
s := NewUniformSample(100)
|
|
for i := 0; i < 100; i++ {
|
|
s.Update(int64(i))
|
|
}
|
|
quit := make(chan struct{})
|
|
go func() {
|
|
t := time.NewTicker(10 * time.Millisecond)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-t.C:
|
|
s.Update(rand.Int63())
|
|
case <-quit:
|
|
t.Stop()
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
for i := 0; i < 1000; i++ {
|
|
s.Snapshot().Count()
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
quit <- struct{}{}
|
|
}
|
|
|
|
func BenchmarkCalculatePercentiles(b *testing.B) {
|
|
pss := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999}
|
|
var vals []int64
|
|
for i := 0; i < 1000; i++ {
|
|
vals = append(vals, int64(rand.Int31()))
|
|
}
|
|
v := make([]int64, len(vals))
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
copy(v, vals)
|
|
_ = CalculatePercentiles(v, pss)
|
|
}
|
|
}
|