-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdigest_test.go
89 lines (82 loc) · 2.13 KB
/
tdigest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package tdigest
import (
"fmt"
"math/rand"
"strconv"
"sync"
"testing"
)
func TestTDigest(t *testing.T) {
td := NewConcurrent(Compression(123))
if td.ValueAt(1) != 0 {
t.Fatalf("failed to retreive value from empty TDigest, %v", td.ValueAt(1))
}
td.Add(1, 1)
td.Add(2, 1)
if val := td.ValueAt(0); val != 1 {
t.Fatalf("Unexpected quantile of large value: got %v, expected %v",
val, 1)
}
}
func BenchmarkAdd(b *testing.B) {
for _, size := range []int{10, 100, 200, 500, 1000, 5000, 10000, 50000} {
newSketch := func() Sketch { return New(Compression(float64(size))) }
b.Run(strconv.Itoa(size), benchmarkAddSize(newSketch, size))
}
}
func BenchmarkConcurrent(b *testing.B) {
for _, readPercent := range []float64{.0001, .001, .01, .1, 1, 10, 50} {
for _, concurrency := range []int{1, 2, 4, 8, 16, 32, 64} {
for _, size := range []int{10, 100, 200 /*500, 1000, 5000, 10000, 50000*/} {
newSketch := func() Sketch {
return NewConcurrent(Compression(float64(size)))
}
b.Run(fmt.Sprintf("buf_size=%d,concurrency=%d,reads=%f",
size, concurrency, readPercent),
benchmarkConcurrentAddSize(newSketch, size, concurrency, readPercent))
}
}
}
}
func benchmarkConcurrentAddSize(
newSketch func() Sketch, size, concurrency int, readPercent float64,
) func(*testing.B) {
return func(b *testing.B) {
h := newSketch()
data := make([]float64, 0, b.N)
for i := 0; i < b.N; i++ {
data = append(data, rand.Float64())
}
var wg sync.WaitGroup
wg.Add(concurrency)
readFrac := readPercent / 100
b.ResetTimer()
for goroutine := 0; goroutine < concurrency; goroutine++ {
go func(goroutine int) {
for i := goroutine; i < b.N; i += concurrency {
h.Add(data[i], 1)
if data[i] < readFrac {
h.ValueAt(.2)
}
}
wg.Done()
}(goroutine)
}
wg.Wait()
b.SetBytes(8)
}
}
func benchmarkAddSize(newSketch func() Sketch, size int) func(*testing.B) {
return func(b *testing.B) {
h := newSketch()
data := make([]float64, 0, b.N)
for i := 0; i < b.N; i++ {
data = append(data, rand.Float64())
}
b.ResetTimer()
for _, v := range data {
h.Add(v, 1)
}
b.SetBytes(8)
}
}