-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_concurrent_test.go
123 lines (114 loc) · 2.53 KB
/
tree_concurrent_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package art
import (
"bytes"
"fmt"
"github.com/dshulyak/art"
"github.com/stretchr/testify/assert"
tbtree "github.com/tidwall/btree"
"math/rand"
"sync"
"testing"
"time"
)
func TestArtTest_Insert(t *testing.T) {
tree := Tree[int]{}
for i := 0; i < 1_000_000; i++ {
tree.Insert(Key(fmt.Sprintf("sharedNode::%d", i)), i)
}
}
func TestTree_ConcurrentInsert(t *testing.T) {
t.Parallel()
// set up
N := 1_000_000
tree := Tree[int]{}
wg := sync.WaitGroup{}
for i := 0; i < N; i++ {
wg.Add(1)
go func(i int) {
tree.Insert(Key(fmt.Sprintf("sharedNode::%d", i)), i)
wg.Done()
}(i)
}
wg.Wait()
for i := 0; i < N; i++ {
value, found := tree.Search(Key(fmt.Sprintf("sharedNode::%d", i)))
assert.True(t, found)
assert.Equal(t, i, value)
}
}
func TestTree_ConcurrentInsert2(t *testing.T) {
t.Parallel()
// set up
N := 1_000_000
tree := Tree[[]byte]{}
inserted := []Key{}
mu := sync.RWMutex{}
wg := sync.WaitGroup{}
for i := 0; i < N; i++ {
wg.Add(1)
go func(i int) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
k := randomKey(rng)
tree.Insert(k, k)
mu.Lock()
inserted = append(inserted, k)
mu.Unlock()
wg.Done()
}(i)
}
wg.Wait()
for _, key := range inserted {
value, found := tree.Search(key)
assert.True(t, found)
assert.Equal(t, []byte(key), value)
}
}
func BenchmarkArtConcurrentInsert(b *testing.B) {
value := newValue(123)
l := Tree[[]byte]{}
b.ResetTimer()
//var count int
b.RunParallel(func(pb *testing.PB) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for pb.Next() {
l.Insert(randomKey(rng), value)
}
})
}
func BenchmarkAnotherArtConcurrentInsert(b *testing.B) {
value := newValue(123)
l := art.Tree{}
b.ResetTimer()
//var count int
b.RunParallel(func(pb *testing.PB) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for pb.Next() {
l.Insert(randomKey(rng), value)
}
})
}
//func BenchmarkConcurrentInsert(b *testing.B) {
// value := newValue(123)
// l := NewArtTree()
// b.ResetTimer()
// //var count int
// b.RunParallel(func(pb *testing.PB) {
// rng := rand.New(rand.NewSource(time.Now().UnixNano()))
// for pb.Next() {
// l.Insert(randomKey(rng), value)
// }
// })
//}
func BenchmarkBtreeConcurrentInsert(b *testing.B) {
l := tbtree.NewGenericOptions[[]byte](func(a, b []byte) bool {
return bytes.Compare(a, b) < 0
}, tbtree.Options{NoLocks: false})
b.ResetTimer()
//var count int
b.RunParallel(func(pb *testing.PB) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for pb.Next() {
l.Set(randomKey(rng))
}
})
}