This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
common_test.go
180 lines (155 loc) · 4.26 KB
/
common_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
package db
import (
"encoding/binary"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
//----------------------------------------
// Helper functions.
func checkValue(t *testing.T, db DB, key []byte, valueWanted []byte) {
valueGot, err := db.Get(key)
assert.NoError(t, err)
assert.Equal(t, valueWanted, valueGot)
}
func checkValid(t *testing.T, itr Iterator, expected bool) {
valid := itr.Valid()
require.Equal(t, expected, valid)
}
func checkNext(t *testing.T, itr Iterator, expected bool) {
itr.Next()
// assert.NoError(t, err) TODO: look at fixing this
valid := itr.Valid()
require.Equal(t, expected, valid)
}
func checkNextPanics(t *testing.T, itr Iterator) {
assert.Panics(t, func() { itr.Next() }, "checkNextPanics expected an error but didn't")
}
func checkItem(t *testing.T, itr Iterator, key []byte, value []byte) {
v := itr.Value()
k := itr.Key()
assert.Exactly(t, key, k)
assert.Exactly(t, value, v)
}
func checkInvalid(t *testing.T, itr Iterator) {
checkValid(t, itr, false)
checkKeyPanics(t, itr)
checkValuePanics(t, itr)
checkNextPanics(t, itr)
}
func checkKeyPanics(t *testing.T, itr Iterator) {
assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't")
}
func checkValuePanics(t *testing.T, itr Iterator) {
assert.Panics(t, func() { itr.Value() })
}
func newTempDB(t *testing.T, backend BackendType) (db DB, dbDir string) {
dirname, err := ioutil.TempDir("", "db_common_test")
require.NoError(t, err)
db, err = NewDB("testdb", backend, dirname)
require.NoError(t, err)
return db, dirname
}
func newDB(t require.TestingT, backend BackendType) (DB, string, string) {
name := fmt.Sprintf("test_%x", randStr(12))
dir := os.TempDir()
db, err := NewDB(name, backend, dir)
require.NoError(t, err)
return db, dir, name
}
func benchmarkRangeScans(b *testing.B, db DB, dbSize int64) {
b.StopTimer()
rangeSize := int64(10000)
if dbSize < rangeSize {
b.Errorf("db size %v cannot be less than range size %v", dbSize, rangeSize)
}
for i := int64(0); i < dbSize; i++ {
bytes := int642Bytes(i)
err := db.Set(bytes, bytes)
if err != nil {
// require.NoError() is very expensive (according to profiler), so check manually
b.Fatal(b, err)
}
}
b.StartTimer()
for i := 0; i < b.N; i++ {
start := rand.Int63n(dbSize - rangeSize)
end := start + rangeSize
iter, err := db.Iterator(int642Bytes(start), int642Bytes(end))
require.NoError(b, err)
count := 0
for ; iter.Valid(); iter.Next() {
count++
}
iter.Close()
require.EqualValues(b, rangeSize, count)
}
}
func benchmarkRandomReadsWrites(b *testing.B, db DB) {
const numItems = int64(1000000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkRandomReadWrite(b, db, numItems)
}
}
func benchmarkParallelRandomReadsWrites(b *testing.B, db DB) {
const numItems = int64(1000000)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
benchmarkRandomReadWrite(b, db, numItems)
}
})
}
func benchmarkRandomReadWrite(b *testing.B, db DB, numItems int64) {
// Write something
{
idx := rand.Int63n(numItems)
val := idx + 2
idxBytes := int642Bytes(idx)
valBytes := int642Bytes(val)
// fmt.Printf("Set %X -> %X\n", idxBytes, valBytes)
err := db.Set(idxBytes, valBytes)
if err != nil {
// require.NoError() is very expensive (according to profiler), so check manually
b.Fatal(b, err)
}
}
// Read something
{
idx := rand.Int63n(numItems)
valExp := idx + 2
idxBytes := int642Bytes(idx)
valBytes, err := db.Get(idxBytes)
if err != nil {
// require.NoError() is very expensive (according to profiler), so check manually
b.Fatal(b, err)
}
// fmt.Printf("Get %X -> %X\n", idxBytes, valBytes)
if valBytes != nil {
if len(valBytes) != 8 {
b.Errorf("Expected length 8 for %v, got %X", idx, valBytes)
b.Fail()
}
valGot := bytes2Int64(valBytes)
if valGot > 1 { // cleveldb return 1 even if `idx` doesn't exist
if valExp != valGot {
b.Errorf("Expected %v for %v, got %v", valExp, idx, valGot)
b.Fail()
}
}
}
}
}
func int642Bytes(i int64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}
func bytes2Int64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf))
}