diff options
| author | Jakob Unterwurzacher | 2016-09-24 22:45:25 +0200 | 
|---|---|---|
| committer | Jakob Unterwurzacher | 2016-09-25 16:43:17 +0200 | 
| commit | 6c52c1a6e6176ba7b6922ee27df7ddd09991b57e (patch) | |
| tree | ab97471d503d1e84f4ccacb99af9eed6a0f504b0 | |
| parent | 52a6f4f71ed464eb64dc05e0768d4af58bd2c195 (diff) | |
reverse: add longname benchmark
| -rw-r--r-- | tests/reverse/longname_perf_test.go | 73 | ||||
| -rw-r--r-- | tests/test_helpers/helpers.go | 15 | 
2 files changed, 83 insertions, 5 deletions
| diff --git a/tests/reverse/longname_perf_test.go b/tests/reverse/longname_perf_test.go new file mode 100644 index 0000000..43acd25 --- /dev/null +++ b/tests/reverse/longname_perf_test.go @@ -0,0 +1,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) +		} +	} +} diff --git a/tests/test_helpers/helpers.go b/tests/test_helpers/helpers.go index bad5675..7e1f4e8 100644 --- a/tests/test_helpers/helpers.go +++ b/tests/test_helpers/helpers.go @@ -15,13 +15,18 @@ import (  	"github.com/rfjakob/gocryptfs/internal/nametransform"  ) +// TmpDir will be created inside this directory  const testParentDir = "/tmp/gocryptfs-test-parent"  const GocryptfsBinary = "../../gocryptfs" -// "go test" runs package tests in parallel! We must create a unique TmpDir on -// startup or the tests will interfere horribly +// "go test" runs package tests in parallel! We create a unique TmpDir in +// init() so the tests do not interfere.  var TmpDir string + +// TmpDir + "/default-plain"  var DefaultPlainDir string + +// TmpDir + "/default-cipher"  var DefaultCipherDir string  func init() { @@ -77,7 +82,7 @@ func ResetTmpDir(plaintextNames bool) {  }  // InitFS calls "gocryptfs -init" on a new directory in TmpDir, passing -// "extraArgs" in addition to practical defaults. +// "extraArgs" in addition to useful defaults.  //  // The returned cipherdir has NO trailing slash.  func InitFS(t *testing.T, extraArgs ...string) string { @@ -128,7 +133,7 @@ func Mount(c string, p string, showOutput bool, extraArgs ...string) error {  	return cmd.Run()  } -// MountOrExit calls mount() and exits on failure. +// MountOrExit calls Mount() and exits on failure.  func MountOrExit(c string, p string, extraArgs ...string) {  	err := Mount(c, p, true, extraArgs...)  	if err != nil { @@ -137,7 +142,7 @@ func MountOrExit(c string, p string, extraArgs ...string) {  	}  } -// MountOrFatal calls mount() and calls t.Fatal() on failure. +// MountOrFatal calls Mount() and calls t.Fatal() on failure.  func MountOrFatal(t *testing.T, c string, p string, extraArgs ...string) {  	err := Mount(c, p, true, extraArgs...)  	if err != nil { | 
