diff --git a/serialization.go b/serialization.go index 6acb658..e05afc7 100644 --- a/serialization.go +++ b/serialization.go @@ -69,7 +69,7 @@ func FromBytes(buf *bytes.Reader, options ...tdigestOption) (*TDigest, error) { } if encoding != smallEncoding { - return nil, fmt.Errorf("Unsupported encoding version: %d", encoding) + return nil, fmt.Errorf("unsupported encoding version: %d", encoding) } t, err := newWithoutSummary(options...) diff --git a/summary.go b/summary.go index f7c9067..d6a29ce 100644 --- a/summary.go +++ b/summary.go @@ -25,7 +25,7 @@ func (s *summary) Len() int { func (s *summary) Add(key float64, value uint64) error { if math.IsNaN(key) { - return fmt.Errorf("Key must not be NaN") + return fmt.Errorf("key must not be NaN") } if value == 0 { return fmt.Errorf("Count must be >0") diff --git a/tdigest.go b/tdigest.go index a79f5d0..91a5192 100644 --- a/tdigest.go +++ b/tdigest.go @@ -8,16 +8,20 @@ // ElasticSearch), performance metrics for distributed systems, etc. // // After you create (and configure, if desired) the digest: -// digest, err := tdigest.New(tdigest.Compression(100)) +// +// digest, err := tdigest.New(tdigest.Compression(100)) // // You can then use it for registering measurements: -// digest.Add(number) +// +// digest.Add(number) // // Estimating quantiles: -// digest.Quantile(0.99) +// +// digest.Quantile(0.99) // // And merging with another digest: -// digest.Merge(otherDigest) +// +// digest.Merge(otherDigest) package tdigest import ( @@ -158,7 +162,7 @@ func boundedWeightedAverage(x1 float64, w1 float64, x2 float64, w2 float64) floa // This will emit an error if `value` is NaN or if `count` is zero. func (t *TDigest) AddWeighted(value float64, count uint64) (err error) { if count == 0 { - return fmt.Errorf("Illegal datapoint ", value, count) + return fmt.Errorf("illegal datapoint ", value, count) } if t.summary.Len() == 0 { @@ -174,7 +178,7 @@ func (t *TDigest) AddWeighted(value float64, count uint64) (err error) { begin, end := t.findNeighbors(begin, value) - closest := t.chooseMergeCandidate(begin, end, value, count) + closest := t.chooseMergeCandidate(begin, end, count) if closest == t.summary.Len() { err = t.summary.Add(value, count) @@ -366,7 +370,7 @@ func (t TDigest) findNeighbors(start int, value float64) (int, int) { return start, lastNeighbor } -func (t TDigest) chooseMergeCandidate(begin, end int, value float64, count uint64) int { +func (t TDigest) chooseMergeCandidate(begin, end int, count uint64) int { closest := t.summary.Len() sum := t.summary.HeadSum(begin) var n float32 diff --git a/tdigest_test.go b/tdigest_test.go index 1c4c51f..7ee5cdd 100644 --- a/tdigest_test.go +++ b/tdigest_test.go @@ -7,14 +7,10 @@ import ( "sort" "testing" - "github.com/leesper/go_rng" + rng "github.com/leesper/go_rng" "gonum.org/v1/gonum/stat" ) -func init() { - rand.Seed(0xDEADBEE) -} - func uncheckedNew(options ...tdigestOption) *TDigest { t, _ := New(options...) return t @@ -794,78 +790,6 @@ func randomTDigest(compression float64) *TDigest { return t } -var sumSizes = []int{10, 100, 1000, 10000} - -func BenchmarkSumLoopSimple(b *testing.B) { - for _, size := range sumSizes { - size := size - b.Run(fmt.Sprint(size), func(b *testing.B) { - benchmarkSumLoopSimple(b, size) - }) - } -} - -func benchmarkSumLoopSimple(b *testing.B, size int) { - counts := generateCounts(size) - indexes := generateIndexes(size) - - b.ReportAllocs() - b.ResetTimer() - for n := 0; n < b.N; n++ { - for _, idx := range indexes { - _ = sumUntilIndexSimple(counts, idx) - } - } -} - -func BenchmarkSumLoopUnrolled(b *testing.B) { - for _, size := range sumSizes { - size := size - b.Run(fmt.Sprint(size), func(b *testing.B) { - benchmarkSumLoopUnrolled(b, size) - }) - } -} - -func benchmarkSumLoopUnrolled(b *testing.B, size int) { - counts := generateCounts(size) - indexes := generateIndexes(size) - - b.ReportAllocs() - b.ResetTimer() - for n := 0; n < b.N; n++ { - for _, idx := range indexes { - _ = sumUntilIndex(counts, idx) - } - } -} - -func generateCounts(size int) []uint64 { - counts := make([]uint64, size) - for i := 0; i < size; i++ { - counts[i] = rand.Uint64() - } - return counts -} - -func generateIndexes(size int) []int { - const num = 100 - - indexes := make([]int, num) - for i := 0; i < num; i++ { - indexes[i] = rand.Intn(size) - } - return indexes -} - -func sumUntilIndexSimple(counts []uint64, idx int) uint64 { - var sum uint64 - for _, c := range counts { - sum += uint64(c) - } - return sum -} - // Pathological ordered-input case. func BenchmarkAddOrdered(b *testing.B) { t, _ := New(Compression(100))