pax_global_header00006660000000000000000000000064141432266430014517gustar00rootroot0000000000000052 comment=131360b71334e83f9ee9ba7a18d9d4a79684c061 golang-github-valyala-histogram-1.2.0+ds/000077500000000000000000000000001414322664300202725ustar00rootroot00000000000000golang-github-valyala-histogram-1.2.0+ds/LICENSE000066400000000000000000000020771414322664300213050ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2019 Aliaksandr Valialkin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-valyala-histogram-1.2.0+ds/README.md000066400000000000000000000005431414322664300215530ustar00rootroot00000000000000[![GoDoc](https://godoc.org/github.com/valyala/histogram?status.svg)](http://godoc.org/github.com/valyala/histogram) [![Go Report](https://goreportcard.com/badge/github.com/valyala/histogram)](https://goreportcard.com/report/github.com/valyala/histogram) # histogram Fast histograms for Go. See [docs](https://godoc.org/github.com/valyala/histogram). golang-github-valyala-histogram-1.2.0+ds/go.mod000066400000000000000000000001311414322664300213730ustar00rootroot00000000000000module github.com/valyala/histogram go 1.12 require github.com/valyala/fastrand v1.1.0 golang-github-valyala-histogram-1.2.0+ds/go.sum000066400000000000000000000002551414322664300214270ustar00rootroot00000000000000github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8= github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= golang-github-valyala-histogram-1.2.0+ds/histogram.go000066400000000000000000000045211414322664300226200ustar00rootroot00000000000000// Package histogram provides building blocks for fast histograms. package histogram import ( "math" "sort" "sync" "github.com/valyala/fastrand" ) var ( infNeg = math.Inf(-1) infPos = math.Inf(1) nan = math.NaN() ) // Fast is a fast histogram. // // It cannot be used from concurrently running goroutines without // external synchronization. type Fast struct { max float64 min float64 count uint64 a []float64 tmp []float64 rng fastrand.RNG } // NewFast returns new fast histogram. func NewFast() *Fast { f := &Fast{} f.Reset() return f } // Reset resets the histogram. func (f *Fast) Reset() { f.max = infNeg f.min = infPos f.count = 0 if len(f.a) > 0 { f.a = f.a[:0] f.tmp = f.tmp[:0] } else { // Free up memory occupied by unused histogram. f.a = nil f.tmp = nil } // Reset rng state in order to get repeatable results // for the same sequence of values passed to Fast.Update. // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1612 f.rng.Seed(1) } // Update updates the f with v. func (f *Fast) Update(v float64) { if v > f.max { f.max = v } if v < f.min { f.min = v } f.count++ if len(f.a) < maxSamples { f.a = append(f.a, v) return } if n := int(f.rng.Uint32n(uint32(f.count))); n < len(f.a) { f.a[n] = v } } const maxSamples = 1000 // Quantile returns the quantile value for the given phi. func (f *Fast) Quantile(phi float64) float64 { f.tmp = append(f.tmp[:0], f.a...) sort.Float64s(f.tmp) return f.quantile(phi) } // Quantiles appends quantile values to dst for the given phis. func (f *Fast) Quantiles(dst, phis []float64) []float64 { f.tmp = append(f.tmp[:0], f.a...) sort.Float64s(f.tmp) for _, phi := range phis { q := f.quantile(phi) dst = append(dst, q) } return dst } func (f *Fast) quantile(phi float64) float64 { if len(f.tmp) == 0 || math.IsNaN(phi) { return nan } if phi <= 0 { return f.min } if phi >= 1 { return f.max } idx := uint(phi*float64(len(f.tmp)-1) + 0.5) if idx >= uint(len(f.tmp)) { idx = uint(len(f.tmp) - 1) } return f.tmp[idx] } // GetFast returns a histogram from a pool. func GetFast() *Fast { v := fastPool.Get() if v == nil { return NewFast() } return v.(*Fast) } // PutFast puts hf to the pool. // // hf cannot be used after this call. func PutFast(f *Fast) { f.Reset() fastPool.Put(f) } var fastPool sync.Pool golang-github-valyala-histogram-1.2.0+ds/histogram_test.go000066400000000000000000000036111414322664300236560ustar00rootroot00000000000000package histogram import ( "math" "testing" ) func TestFastUnderflow(t *testing.T) { f := GetFast() defer PutFast(f) q := f.Quantile(0.5) if !math.IsNaN(q) { t.Fatalf("unexpected quantile for empty histogram; got %v; want %v", q, nan) } for i := 0; i < maxSamples; i++ { f.Update(float64(i)) } qs := f.Quantiles(nil, []float64{0, 0.5, 1}) if qs[0] != 0 { t.Fatalf("unexpected quantile value for phi=0; got %v; want %v", qs[0], 0) } if qs[1] != maxSamples/2 { t.Fatalf("unexpected quantile value for phi=0.5; got %v; want %v", qs[1], maxSamples/2) } if qs[2] != maxSamples-1 { t.Fatalf("unexpected quantile value for phi=1; got %v; want %v", qs[2], maxSamples-1) } } func TestFastOverflow(t *testing.T) { f := GetFast() defer PutFast(f) for i := 0; i < maxSamples*10; i++ { f.Update(float64(i)) } qs := f.Quantiles(nil, []float64{0, 0.5, 0.9999, 1}) if qs[0] != 0 { t.Fatalf("unexpected quantile value for phi=0; got %v; want %v", qs[0], 0) } median := float64(maxSamples*10-1) / 2 if qs[1] < median*0.9 || qs[1] > median*1.1 { t.Fatalf("unexpected quantile value for phi=0.5; got %v; want %v", qs[1], median) } if qs[2] < maxSamples*10*0.9 { t.Fatalf("unexpected quantile value for phi=0.9999; got %v; want %v", qs[2], maxSamples*10*0.9) } if qs[3] != maxSamples*10-1 { t.Fatalf("unexpected quantile value for phi=1; got %v; want %v", qs[3], maxSamples*10-1) } q := f.Quantile(nan) if !math.IsNaN(q) { t.Fatalf("unexpected value for phi=NaN; got %v; want %v", q, nan) } } func TestFastRepeatableResults(t *testing.T) { f := GetFast() defer PutFast(f) for i := 0; i < maxSamples*10; i++ { f.Update(float64(i)) } q1 := f.Quantile(0.95) for j := 0; j < 10; j++ { f.Reset() for i := 0; i < maxSamples*10; i++ { f.Update(float64(i)) } q2 := f.Quantile(0.95) if q2 != q1 { t.Fatalf("unexpected quantile value; got %g; want %g", q2, q1) } } } golang-github-valyala-histogram-1.2.0+ds/histogram_timing_test.go000066400000000000000000000005441414322664300252270ustar00rootroot00000000000000package histogram import ( "sync" "testing" ) func BenchmarkFastUpdate(b *testing.B) { b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { f := NewFast() var v float64 for pb.Next() { f.Update(v) v += 1.5 } SinkLock.Lock() Sink += f.Quantile(0.5) SinkLock.Unlock() }) } var Sink float64 var SinkLock sync.Mutex