-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathvalue_gc_rewrite_bench_test.go
More file actions
150 lines (125 loc) · 3.57 KB
/
value_gc_rewrite_bench_test.go
File metadata and controls
150 lines (125 loc) · 3.57 KB
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
/*
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package badger
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
const (
benchmarkRewriteExpiredKeyCount = 8192
benchmarkRewriteLiveKeyCount = 4096
benchmarkRewriteValueSize = 2 << 10
benchmarkRewriteValueLogFileSize = 8 << 20
benchmarkRewriteValueThreshold = 128
benchmarkRewriteEntryExpiry uint64 = 1
)
func benchmarkValueGCRewriteOptions(dir string) Options {
opt := getTestOptions(dir)
opt.ValueLogFileSize = benchmarkRewriteValueLogFileSize
opt.BaseTableSize = 1 << 20
opt.BaseLevelSize = 4 << 20
opt.ValueThreshold = benchmarkRewriteValueThreshold
opt.NumCompactors = 0
opt.MetricsEnabled = false
return opt
}
func benchmarkWriteEntries(b *testing.B, db *DB, prefix string, count int, value []byte, expiresAt uint64) {
b.Helper()
for i := 0; i < count; {
txn := db.NewTransaction(true)
for ; i < count; i++ {
entry := NewEntry([]byte(fmt.Sprintf("%s-%08d", prefix, i)), value)
entry.ExpiresAt = expiresAt
err := txn.SetEntry(entry)
if err == ErrTxnTooBig {
require.NoError(b, txn.Commit())
txn = nil
break
}
require.NoError(b, err)
}
if txn != nil {
require.NoError(b, txn.Commit())
}
}
}
func benchmarkPrepareRewriteFixture(b *testing.B, dir string) {
b.Helper()
db, err := Open(benchmarkValueGCRewriteOptions(dir))
require.NoError(b, err)
expiredValue := bytes.Repeat([]byte("e"), benchmarkRewriteValueSize)
liveValue := bytes.Repeat([]byte("l"), benchmarkRewriteValueSize)
// Fill the earliest vlog files with expired entries before appending live entries.
benchmarkWriteEntries(b, db, "expired", benchmarkRewriteExpiredKeyCount, expiredValue, benchmarkRewriteEntryExpiry)
benchmarkWriteEntries(b, db, "live", benchmarkRewriteLiveKeyCount, liveValue, 0)
require.NoError(b, db.Close())
}
func copyDir(src, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
targetPath := filepath.Join(dst, relPath)
if info.IsDir() {
return os.MkdirAll(targetPath, info.Mode())
}
if !info.Mode().IsRegular() {
return fmt.Errorf("unsupported file mode for %s", path)
}
srcFile, err := os.Open(path)
if err != nil {
return err
}
dstFile, err := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode())
if err != nil {
_ = srcFile.Close()
return err
}
if _, err := io.Copy(dstFile, srcFile); err != nil {
_ = srcFile.Close()
_ = dstFile.Close()
return err
}
if err := srcFile.Close(); err != nil {
_ = dstFile.Close()
return err
}
return dstFile.Close()
})
}
func BenchmarkValueGCRewriteExpiredOnlyFile(b *testing.B) {
rootDir := b.TempDir()
fixtureDir := filepath.Join(rootDir, "fixture")
benchmarkPrepareRewriteFixture(b, fixtureDir)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
runDir := filepath.Join(rootDir, fmt.Sprintf("run-%03d", i))
require.NoError(b, copyDir(fixtureDir, runDir))
db, err := Open(benchmarkValueGCRewriteOptions(runDir))
require.NoError(b, err)
fids := db.vlog.sortedFids()
require.Greater(b, len(fids), 1)
db.vlog.filesLock.RLock()
lf := db.vlog.filesMap[fids[0]]
db.vlog.filesLock.RUnlock()
b.StartTimer()
err = db.vlog.rewrite(lf)
b.StopTimer()
require.NoError(b, err)
require.NoError(b, db.Close())
removeDir(runDir)
}
}