-
Notifications
You must be signed in to change notification settings - Fork 1
/
filestorage_test.go
191 lines (148 loc) · 3.71 KB
/
filestorage_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
package raft
import (
"fmt"
"io/ioutil"
"math"
"math/rand"
"os"
"reflect"
"testing"
"github.com/relab/raft/commonpb"
)
func newFileStorage(t testing.TB, overwrite bool, filepath ...string) (fs *FileStorage, path string, cleanup func()) {
var dbfile string
if len(filepath) < 1 {
file, err := ioutil.TempFile("", "bolt")
if err != nil {
t.Fatal(err)
}
dbfile = file.Name()
} else {
dbfile = filepath[0]
}
storage, err := NewFileStorage(dbfile, overwrite)
if err != nil {
t.Fatal(err)
}
return storage, dbfile, func() {
if err := os.Remove(dbfile); err != nil {
t.Fatal(err)
}
}
}
func TestNewFileStorage(t *testing.T) {
overwrite := false
// Create storage on path.
storage, path, _ := newFileStorage(t, !overwrite)
// Close storage so that the lock on the file is released in time for
// the recover below.
storage.Close()
// Recover from path, where file exists.
_, _, cleanup2 := newFileStorage(t, overwrite, path)
cleanup2()
// Recover from path, where file doesn't exist.
newFileStorage(t, overwrite, path)
// Overwrite path, where file exists.
_, _, cleanup3 := newFileStorage(t, !overwrite, path)
cleanup3()
// Overwrite path, where file doesn't exist.
_, _, cleanup4 := newFileStorage(t, !overwrite, path)
cleanup4()
if _, err := os.Stat(path); err == nil {
t.Errorf("got %s exists, want %s removed", path, path)
}
}
func TestFileStorageStoreValue(t *testing.T) {
var storage Storage
storage, _, cleanup := newFileStorage(t, true)
defer cleanup()
var expected uint64 = 5
err := storage.Set(KeyTerm, expected)
if err != nil {
t.Fatal(err)
}
got, err := storage.Get(KeyTerm)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("got %+v, want %+v", got, expected)
}
}
func TestFileStorageStoreEntry(t *testing.T) {
var storage Storage
storage, _, cleanup := newFileStorage(t, true)
defer cleanup()
expected := &commonpb.Entry{Term: 5, Index: 1}
err := storage.StoreEntries([]*commonpb.Entry{expected})
if err != nil {
t.Fatal(err)
}
got, err := storage.GetEntry(1)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("got %+v, want %+v", got, expected)
}
}
func BenchmarkSnapshot(b *testing.B) {
storage, _, cleanup := newFileStorage(b, true, "benchsnap.bolt")
defer cleanup()
// 200kb.
data := make([]byte, 200000)
rand.Read(data)
snapshot := &commonpb.Snapshot{Data: data}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := storage.SetSnapshot(snapshot); err != nil {
b.Error(err)
}
}
}
func BenchmarkThroughput(b *testing.B) {
rand.Seed(500)
type benchmark struct {
name string
numEntries int
payloadInBytes int
}
var benchmarks []benchmark
for i := 0; i < 5; i++ {
for _, payload := range []int{10, 50, 100, 200, 500, 1000} {
numEntries := int(math.Pow(10, float64(i)))
name := fmt.Sprintf("%d entries with %d bytes", numEntries, payload)
benchmarks = append(benchmarks, benchmark{
name,
numEntries,
payload,
})
}
}
for _, bm := range benchmarks {
storage, _, cleanup := newFileStorage(b, true, "benchthroughput.bolt")
entries := make([]*commonpb.Entry, bm.numEntries)
for i := 0; i < bm.numEntries; i++ {
b := make([]byte, bm.payloadInBytes)
rand.Read(b)
entries[i] = &commonpb.Entry{Data: b}
}
b.Run("store "+bm.name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := storage.StoreEntries(entries); err != nil {
b.Error(err)
}
}
})
b.Run("delete "+bm.name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := storage.RemoveEntries(0, uint64(len(entries))-1); err != nil {
b.Error(err)
}
}
})
cleanup()
}
}