-
Notifications
You must be signed in to change notification settings - Fork 1
/
bltfs_test.go
261 lines (213 loc) · 5.09 KB
/
bltfs_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package bltfs_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/boltdb/bolt"
pb "github.com/golang/protobuf/proto"
"github.com/kr/pretty"
"github.com/pkg/errors"
"hpt.space/bltfs"
"hpt.space/bltfs/ltfs"
"hpt.space/bltfs/proto"
)
const (
testDirectory = "/tmp/ltfs/tape"
fixtures = "./fixtures"
)
func setupCleanTape() string {
dir, err := ioutil.TempDir("", "bltfstest")
if err != nil {
panic(err)
}
if err := os.MkdirAll(dir, 0755); err != nil {
panic(err)
}
return dir
}
func cleanup(path string) {
if err := os.RemoveAll(path); err != nil {
panic(err)
}
}
func TestLargeBinaryIndex(t *testing.T) {
db, err := bolt.Open("idx.db", 0600, nil)
if err != nil {
t.Fatal(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket([]byte("index"))
if err != nil {
return errors.Wrap(err, "failed to create bucket")
}
return nil
})
if err != nil {
t.Fatal(err)
}
begin := time.Now()
idx, err := ltfs.LoadIndexFromFile("./fixtures/large.schema")
if err != nil {
t.Fatal(err)
}
fmt.Printf("loading and parsing XML index from disk took: %v\n", time.Since(begin))
binIdx, err := bltfs.NewIndex(idx, db)
if err != nil {
t.Fatal(err)
}
begin = time.Now()
//dir := "/+C=DK+ST=NA+L=NA+O=NBI+OU=NA+CN=Jonas_Bardino+emailAddress=bardino@nbi.ku.dk/archive-fGAY1G/"
dir := "/"
begin = time.Now()
lst, err := binIdx.Scan(dir)
fmt.Printf("full scan took: %v\n", time.Since(begin))
if err != nil {
t.Fatal(err)
}
var nDirs, nFiles int
for _, e := range lst {
switch e.Elem.(type) {
case *proto.Entry_Dir:
nDirs++
case *proto.Entry_File:
nFiles++
}
}
fmt.Printf("number of directories: %d\n", nDirs)
fmt.Printf("number of files: %d\n", nFiles)
var root proto.Entry
begin = time.Now()
if err := proto.MarshalDirectoryRecursive(idx.Root, &root); err != nil {
t.Fatal(err)
}
fmt.Printf("full protobuf tree building from ltfs.Index took: %v\n", time.Since(begin))
//pretty.Println(root)
//printTree(&root, "/")
//printDir(&root)
begin = time.Now()
buf, err := pb.Marshal(&root)
if err != nil {
t.Fatal(err)
}
fmt.Printf("full protobuf tree marshalling took: %v\n", time.Since(begin))
if err := ioutil.WriteFile("testpb", buf, os.ModePerm); err != nil {
t.Fatal(err)
}
begin = time.Now()
pbdir, err := binIdx.Marshal()
if err != nil {
t.Fatal(err)
}
fmt.Printf("full protobuf tree building from boltdb took: %v\n", time.Since(begin))
//pretty.Println(pbdir)
//printTree(pbdir, "/")
//printDir(pbdir)
begin = time.Now()
buf, err = pb.Marshal(pbdir)
if err != nil {
t.Fatal(err)
}
fmt.Printf("full protobuf tree marshalling took: %v\n", time.Since(begin))
if err := ioutil.WriteFile("testpb2", buf, os.ModePerm); err != nil {
t.Fatal(err)
}
begin = time.Now()
buf, err = ioutil.ReadFile("testpb2")
if err != nil {
t.Fatal(err)
}
var root2 proto.Entry
if err := pb.Unmarshal(buf, &root2); err != nil {
t.Fatal(err)
}
fmt.Printf("loading and unmarshalling full protobuf tree from disk took: %v\n", time.Since(begin))
if !reflect.DeepEqual(pbdir, &root2) {
t.Fatal("written and read protobuf not equal!")
}
}
func TestSpeed(t *testing.T) {
begin := time.Now()
buf, err := ioutil.ReadFile("testpb2")
if err != nil {
t.Fatal(err)
}
var root proto.Entry
if err := pb.Unmarshal(buf, &root); err != nil {
t.Fatal(err)
}
fmt.Printf("loading and unmarshalling full protobuf tree from disk took: %v\n", time.Since(begin))
}
func printTree(root *proto.Entry, subdir string) {
fmt.Println(filepath.Join(subdir, root.Name))
pretty.Println(root)
switch x := root.Elem.(type) {
case *proto.Entry_Dir:
for _, e := range x.Dir.Entries {
printTree(e, filepath.Join(subdir, root.Name))
}
}
}
func printDir(d *proto.Entry) {
for _, e := range d.Elem.(*proto.Entry_Dir).Dir.Entries {
fmt.Println(e.Name)
}
}
/*
func TestBinaryIndex(t *testing.T) {
db, err := bolt.Open("idx.db", 0600, nil)
if err != nil {
t.Fatal(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket([]byte("index"))
if err != nil {
return errors.Wrap(err, "failed to create bucket")
}
return nil
})
if err != nil {
t.Fatal(err)
}
backend, err := filedebug.Open(filepath.Join(fixtures, "large-filled-tape"))
if err != nil {
t.Fatal(err)
}
store, err := bltfs.Open(backend,
bltfs.WithFileDebug(),
)
var root pb.Entry
begin := time.Now()
if err := pb.MarshalDirectoryRecursive(store.idx.Root, &root); err != nil {
t.Fatal(err)
}
fmt.Printf("full protobuf tree building took: %v\n", time.Since(begin))
pretty.Println(root)
begin = time.Now()
buf, err := root.Marshal()
if err != nil {
t.Fatal(err)
}
fmt.Printf("full protobuf tree marshalling took: %v\n", time.Since(begin))
if err := ioutil.WriteFile("testpb", buf, os.ModePerm); err != nil {
t.Fatal(err)
}
pbdir, err := binIdx.Build()
if err != nil {
t.Fatal(err)
}
pretty.Println(pbdir)
buf, err = pbdir.Marshal()
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile("testpb2", buf, os.ModePerm); err != nil {
t.Fatal(err)
}
}
*/