-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (119 loc) · 2.73 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/dgraph-io/badger/v3"
"github.com/google/uuid"
"github.com/studyzy/kvinit/wal"
)
func main() {
readWal(100, 200)
return
runWal(3 * 1024)
return
runBadgerTest(nil, func(db *badger.DB) {
for i := int64(0); i < 1000000; i++ {
key := GetRandTxId()
txnSet(db, []byte(key), []byte(fmt.Sprintf("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111%d", i)), 0x00)
if i%10000 == 0 {
fmt.Printf("insert count:%d, current key:%s\n", i, key)
}
}
})
fmt.Println("all done!")
}
func runWal(sizeG int) {
walOpt := &wal.Options{
NoSync: true,
}
walOpt.SegmentSize = 100 * 1024 * 1024 //100M
log, err := wal.Open("./waldata", nil)
if err != nil {
fmt.Println(err.Error())
return
}
for i := 0; i < sizeG; i++ {
for j := 0; j < 100; j++ {
data := make([]byte, 9*1024*1024) //9M
data[0] = byte(i*100 + j)
err = log.Write(uint64(i*100+j+1), data)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("write file index:", i*100+j)
}
}
fmt.Println("write done!")
}
func readWal(h1, h2 uint64) {
walOpt := &wal.Options{
NoSync: true,
}
walOpt.SegmentSize = 100 * 1024 * 1024 //100M
log, err := wal.Open("./waldata", nil)
if err != nil {
fmt.Println(err.Error())
return
}
start := time.Now()
for i := h1; i <= h2; i++ {
data, err := log.Read(i)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("Read wal height:", i, " get data length:", len(data), "cost:", time.Since(start))
}
}
// Opens a badger db and runs a a test on it.
func runBadgerTest(opts *badger.Options, test func(db *badger.DB)) {
dir := "./badger_data"
if opts == nil {
opts = new(badger.Options)
*opts = getTestOptions(dir)
} else {
opts.Dir = dir
opts.ValueDir = dir
}
if opts.InMemory {
opts.Dir = ""
opts.ValueDir = ""
}
db, err := badger.Open(*opts)
if err != nil {
fmt.Println(err)
return
}
defer func() {
db.Close()
}()
test(db)
}
func txnSet(kv *badger.DB, key []byte, val []byte, meta byte) {
txn := kv.NewTransaction(true)
txn.SetEntry(badger.NewEntry(key, val).WithMeta(meta))
txn.Commit()
}
func getTestOptions(dir string) badger.Options {
opt := badger.DefaultOptions(dir).
WithSyncWrites(false).
WithLoggingLevel(badger.WARNING)
return opt
}
func GetUUIDWithSeed(seed int64) string {
r := rand.New(rand.NewSource(seed))
uuid, _ := uuid.NewRandomFromReader(r)
return strings.Replace(uuid.String(), "-", "", -1)
}
func getStandardUUID() string {
return uuid.New().String()
}
func GetUUID() string {
return strings.Replace(getStandardUUID(), "-", "", -1)
}
func GetRandTxId() string {
return GetUUID() + GetUUID()
}