-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
xidmap_test.go
236 lines (205 loc) · 5.61 KB
/
xidmap_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//go:build integration
package xidmap
import (
"fmt"
"math/rand"
"runtime"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/dgraph/testutil"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/ristretto/z"
)
// Opens a badger db and runs a a test on it.
func withDB(t *testing.T, test func(db *badger.DB)) {
dir := t.TempDir()
opt := badger.LSMOnlyOptions(dir)
db, err := badger.Open(opt)
require.NoError(t, err)
defer db.Close()
test(db)
}
func getTestXidmapOpts(conn *grpc.ClientConn, db *badger.DB) XidMapOptions {
return XidMapOptions{
UidAssigner: conn,
DgClient: nil,
DB: db,
}
}
func TestXidmap(t *testing.T) {
conn, err := x.SetupConnection(testutil.SockAddrZero, nil, false)
require.NoError(t, err)
require.NotNil(t, conn)
withDB(t, func(db *badger.DB) {
xidmap := New(getTestXidmapOpts(conn, db))
uida, isNew := xidmap.AssignUid("a")
require.True(t, isNew)
uidaNew, isNew := xidmap.AssignUid("a")
require.Equal(t, uida, uidaNew)
require.False(t, isNew)
uidb, isNew := xidmap.AssignUid("b")
require.True(t, uida != uidb)
require.True(t, isNew)
uidbnew, isNew := xidmap.AssignUid("b")
require.Equal(t, uidb, uidbnew)
require.False(t, isNew)
to := xidmap.AllocateUid() + uint64(1e6+3)
xidmap.BumpTo(to)
uid := xidmap.AllocateUid() // Does not have to be above the bump.
t.Logf("bump up to: %d. allocated: %d", to, uid)
require.NoError(t, xidmap.Flush())
xidmap = nil
xidmap2 := New(getTestXidmapOpts(conn, db))
uida2, isNew := xidmap2.AssignUid("a")
require.Equal(t, uida, uida2)
require.False(t, isNew)
uidb2, isNew := xidmap2.AssignUid("b")
require.Equal(t, uidb, uidb2)
require.False(t, isNew)
require.NoError(t, xidmap2.Flush())
})
}
func TestXidmapMemory(t *testing.T) {
var loop uint32
bToMb := func(b uint64) uint64 {
return b / 1024 / 1024
}
printMemory := func() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf(" Heap = %v M", bToMb(m.HeapInuse))
fmt.Printf(" Alloc = %v M", bToMb(m.Alloc))
fmt.Printf(" Sys = %v M", bToMb(m.Sys))
fmt.Printf(" Loop = %.2fM", float64(atomic.LoadUint32(&loop))/1e6)
fmt.Printf(" NumGC = %v\n", m.NumGC)
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
go func() {
for range ticker.C {
printMemory()
}
}()
conn, err := x.SetupConnection(testutil.SockAddrZero, nil, false)
require.NoError(t, err)
require.NotNil(t, conn)
xidmap := New(getTestXidmapOpts(conn, nil))
defer xidmap.Flush()
start := time.Now()
var wg sync.WaitGroup
for numGo := 0; numGo < 32; numGo++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
i := atomic.AddUint32(&loop, 1)
if i > 10e6 {
return
}
xidmap.AssignUid(fmt.Sprintf("xid-%d", i))
}
}()
}
wg.Wait()
t.Logf("Time taken: %v", time.Since(start).Round(time.Millisecond))
}
// Benchmarks using Map
// BenchmarkXidmapWrites-32 4435590 278 ns/op
// BenchmarkXidmapReads-32 33248678 34.1 ns/op
//
// Benchmarks using Trie
// BenchmarkXidmapWrites-32 16202346 375 ns/op
// BenchmarkXidmapReads-32 139261450 44.8 ns/op
//
// go test -v -run=XXX -bench=BenchmarkXidmapWritesRandom -count=10
// go test -v -run=XXX -bench=BenchmarkXidmapReadsRandom -count=10
//
// Benchmarks using Skiplist
// BenchmarkXidmapWritesRandom-16 775ns ± 2%
// BenchmarkXidmapReadsRandom-16 416ns ± 1%
//
// Benchmarks using Trie
// BenchmarkXidmapWritesRandom-16 902ns ± 2%
// BenchmarkXidmapReadsRandom-16 428ns ± 2%
func BenchmarkXidmapWrites(b *testing.B) {
conn, err := x.SetupConnection(testutil.SockAddrZero, nil, false)
if err != nil {
b.Fatalf("Error setting up connection: %s", err.Error())
}
var counter int64
xidmap := New(getTestXidmapOpts(conn, nil))
defer xidmap.Flush()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
xid := atomic.AddInt64(&counter, 1)
xidmap.AssignUid("xid-" + strconv.Itoa(int(xid)))
}
})
}
func BenchmarkXidmapWritesRandom(b *testing.B) {
conn, err := x.SetupConnection(testutil.SockAddrZero, nil, false)
if err != nil {
b.Fatalf("Error setting up connection: %s", err.Error())
}
xidmap := New(getTestXidmapOpts(conn, nil))
defer xidmap.Flush()
b.ResetTimer()
buf := make([]byte, 32)
b.RunParallel(func(pb *testing.PB) {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
for pb.Next() {
r.Read(buf)
xidmap.AssignUid(string(buf))
}
})
}
func BenchmarkXidmapReads(b *testing.B) {
conn, err := x.SetupConnection(testutil.SockAddrZero, nil, false)
if err != nil {
b.Fatalf("Error setting up connection: %s", err.Error())
}
var N = 1000000
xidmap := New(getTestXidmapOpts(conn, nil))
defer xidmap.Flush()
for i := 0; i < N; i++ {
xidmap.AssignUid("xid-" + strconv.Itoa(i))
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
xid := int(z.FastRand()) % N
xidmap.AssignUid("xid-" + strconv.Itoa(xid))
}
})
}
func BenchmarkXidmapReadsRandom(b *testing.B) {
conn, err := x.SetupConnection(testutil.SockAddrZero, nil, false)
if err != nil {
b.Fatalf("Error setting up connection: %s", err.Error())
}
var N = 1000000
buf := make([]byte, 32)
var list [][]byte
xidmap := New(getTestXidmapOpts(conn, nil))
defer xidmap.Flush()
for i := 0; i < N; i++ {
rand.Read(buf)
list = append(list, buf)
xidmap.AssignUid(string(buf))
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
xidmap.AssignUid(string(list[rand.Intn(len(list))]))
}
})
}