-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbench_test.go
98 lines (86 loc) · 2.04 KB
/
bench_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
90
91
92
93
94
95
96
97
98
package stree_test
import (
"fmt"
"math"
"math/rand/v2"
"sort"
"testing"
"github.com/creachadair/mds/stree"
)
const benchSeed = 1471808909908695897
// Trial values of β for load-testing tree operations.
var balances = []int{0, 50, 100, 150, 200, 250, 300, 500, 800, 1000}
func intCompare(a, b int) int { return a - b }
func randomTree(b *testing.B, β int) (*stree.Tree[int], []int) {
rng := rand.New(rand.NewPCG(benchSeed, benchSeed))
values := make([]int, b.N)
for i := range values {
values[i] = rng.IntN(math.MaxInt32)
}
return stree.New(β, intCompare, values...), values
}
func BenchmarkNew(b *testing.B) {
for _, β := range balances {
b.Run(fmt.Sprintf("β=%d", β), func(b *testing.B) {
randomTree(b, β)
})
}
}
func BenchmarkAddRandom(b *testing.B) {
for _, β := range balances {
b.Run(fmt.Sprintf("β=%d", β), func(b *testing.B) {
_, values := randomTree(b, β)
b.ResetTimer()
tree := stree.New[int](β, intCompare)
for _, v := range values {
tree.Add(v)
}
})
}
}
func BenchmarkAddOrdered(b *testing.B) {
for _, β := range balances {
b.Run(fmt.Sprintf("β=%d", β), func(b *testing.B) {
tree := stree.New[int](β, intCompare)
for i := range b.N {
tree.Add(i + 1)
}
})
}
}
func BenchmarkRemoveRandom(b *testing.B) {
for _, β := range balances {
b.Run(fmt.Sprintf("β=%d", β), func(b *testing.B) {
tree, values := randomTree(b, β)
b.ResetTimer()
for _, v := range values {
tree.Remove(v)
}
})
}
}
func BenchmarkRemoveOrdered(b *testing.B) {
for _, β := range balances {
b.Run(fmt.Sprintf("β=%d", β), func(b *testing.B) {
tree, values := randomTree(b, β)
sort.Slice(values, func(i, j int) bool {
return values[i] < values[j]
})
b.ResetTimer()
for _, v := range values {
tree.Remove(v)
}
})
}
}
func BenchmarkLookup(b *testing.B) {
for _, β := range balances {
b.Run(fmt.Sprintf("β=%d", β), func(b *testing.B) {
tree, values := randomTree(b, β)
b.ResetTimer()
for _, v := range values {
tree.Get(v)
}
})
}
}