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
/
cleveldb_test.go
125 lines (105 loc) · 2.59 KB
/
cleveldb_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
//go:build cleveldb
// +build cleveldb
package db
import (
"bytes"
"fmt"
"math/rand"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func BenchmarkRandomReadsWrites2(b *testing.B) {
b.StopTimer()
numItems := int64(1000000)
internal := map[int64]int64{}
for i := 0; i < int(numItems); i++ {
internal[int64(i)] = int64(0)
}
dir := os.TempDir()
db, err := NewCLevelDB(fmt.Sprintf("test_%x", randStr(12)), dir)
if err != nil {
b.Fatal(err.Error())
return
}
fmt.Println("ok, starting")
b.StartTimer()
for i := 0; i < b.N; i++ {
// Write something
{
idx := (int64(rand.Int()) % numItems)
internal[idx]++
val := internal[idx]
idxBytes := int642Bytes(idx)
valBytes := int642Bytes(val)
// fmt.Printf("Set %X -> %X\n", idxBytes, valBytes)
// nolint: errcheck
db.Set(
idxBytes,
valBytes,
)
}
// Read something
{
idx := (int64(rand.Int()) % numItems)
val := internal[idx]
idxBytes := int642Bytes(idx)
valBytes, err := db.Get(idxBytes)
if err != nil {
b.Error(err)
}
// fmt.Printf("Get %X -> %X\n", idxBytes, valBytes)
if val == 0 {
if !bytes.Equal(valBytes, nil) {
b.Errorf("Expected %v for %v, got %X",
nil, idx, valBytes)
break
}
} else {
if len(valBytes) != 8 {
b.Errorf("Expected length 8 for %v, got %X",
idx, valBytes)
break
}
valGot := bytes2Int64(valBytes)
if val != valGot {
b.Errorf("Expected %v for %v, got %v",
val, idx, valGot)
break
}
}
}
}
db.Close()
}
func TestCLevelDBNewDB(t *testing.T) {
db, dir, name := newDB(t, CLevelDBBackend)
defer closeDBWithCleanupDBDir(db, dir, name)
_, ok := db.(*CLevelDB)
assert.True(t, ok)
}
func TestCLevelDBStats(t *testing.T) {
db, dir, name := newDB(t, CLevelDBBackend)
defer closeDBWithCleanupDBDir(db, dir, name)
assert.NotEmpty(t, db.Stats())
}
func BenchmarkCLevelDBRangeScans1M(b *testing.B) {
db, dir, name := newDB(b, CLevelDBBackend)
defer closeDBWithCleanupDBDir(db, dir, name)
benchmarkRangeScans(b, db, int64(1e6))
}
func BenchmarkCLevelDBRangeScans10M(b *testing.B) {
db, dir, name := newDB(b, CLevelDBBackend)
defer closeDBWithCleanupDBDir(db, dir, name)
benchmarkRangeScans(b, db, int64(10e6))
}
func BenchmarkCLevelDBRandomReadsWrites(b *testing.B) {
db, dir, name := newDB(b, CLevelDBBackend)
defer closeDBWithCleanupDBDir(db, dir, name)
benchmarkRandomReadsWrites(b, db)
}
func BenchmarkCLevelDBParallelRandomReadsWrites(b *testing.B) {
db, dir, name := newDB(b, CLevelDBBackend)
defer closeDBWithCleanupDBDir(db, dir, name)
benchmarkParallelRandomReadsWrites(b, db)
}