libgocryptfs/tests/reverse/longname_perf_test.go

51 lines
867 B
Go
Raw Normal View History

2016-09-24 22:45:25 +02:00
package reverse_test
import (
"fmt"
"os"
"testing"
)
func genName(i int, postfix string) string {
return fmt.Sprintf("%04d.%s", i, postfix)
2016-09-24 22:45:25 +02:00
}
// Create 10000 files with long names
func generateLongnameFiles(dir string) {
2016-09-24 22:45:25 +02:00
for i := 0; i < 100000; i++ {
n := genName(i, x240)
2016-09-24 22:45:25 +02:00
f, err := os.Create(dir + "/" + n)
if err != nil {
panic(err)
}
f.Close()
}
}
func BenchmarkLongnameStat(b *testing.B) {
// Setup
generateLongnameFiles(dirA)
2016-09-24 22:45:25 +02:00
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()
// Benchmark
2016-09-24 22:45:25 +02:00
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := os.Stat(dirB + "/" + encryptedNames[i%l])
if err != nil {
b.Fatal(err)
}
}
// Cleanup
b.StopTimer()
os.RemoveAll(dirA)
os.Mkdir(dirA, 0700)
2016-09-24 22:45:25 +02:00
}