summaryrefslogtreecommitdiff
path: root/tests/reverse/longname_perf_test.go
blob: 43acd257306d750483174b6061b391a597f096d5 (plain)
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
package reverse_test

import (
	"bytes"
	"fmt"
	"os"
	"testing"

	"github.com/rfjakob/gocryptfs/tests/test_helpers"
)

var dirA, dirB, x240 string

func TestMain(m *testing.M) {
	x240 = string(bytes.Repeat([]byte("x"), 240))
	dirA = test_helpers.TmpDir + "/a"
	dirB = test_helpers.TmpDir + "/b"
	os.Mkdir(dirA, 0700)
	os.Mkdir(dirB, 0700)
	generateFiles(dirA)
	test_helpers.MountOrExit(dirA, dirB, "-zerokey", "-reverse")
	r := m.Run()
	test_helpers.UnmountPanic(dirB)
	os.RemoveAll(test_helpers.TmpDir)
	os.Exit(r)
}

func genName(i int) string {
	return fmt.Sprintf("%04d.%s", i, x240)
}

// Create 10000 files with long names
func generateFiles(dir string) {
	for i := 0; i < 100000; i++ {
		n := genName(i)
		f, err := os.Create(dir + "/" + n)
		if err != nil {
			panic(err)
		}
		f.Close()
	}
}

func TestLongnameStat(t *testing.T) {
	_, err := os.Stat(dirA + "/" + genName(0))
	if err != nil {
		t.Error(err)
	}
	_, err = os.Stat(dirA + "/" + genName(9999))
	if err != nil {
		t.Error(err)
	}
}

func BenchmarkLongnameStat(b *testing.B) {
	dirFd, err := os.Open(dirB)
	if err != nil {
		b.Fatal(err)
	}
	encryptedNames, err := dirFd.Readdirnames(-1)
	if err != nil {
		b.Fatal(err)
	}
	l := len(encryptedNames)
	dirFd.Close()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := os.Stat(dirB + "/" + encryptedNames[i%l])
		if err != nil {
			b.Fatal(err)
		}
	}
}