2016-09-24 22:45:25 +02:00
|
|
|
package reverse_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-09-25 15:05:09 +02:00
|
|
|
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
|
2016-09-25 15:05:09 +02:00
|
|
|
func generateLongnameFiles(dir string) {
|
2016-09-24 22:45:25 +02:00
|
|
|
for i := 0; i < 100000; i++ {
|
2016-09-25 15:05:09 +02:00
|
|
|
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) {
|
2016-09-25 15:05:09 +02:00
|
|
|
// 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()
|
2016-09-25 15:05:09 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2016-09-25 15:05:09 +02:00
|
|
|
// Cleanup
|
|
|
|
b.StopTimer()
|
|
|
|
os.RemoveAll(dirA)
|
|
|
|
os.Mkdir(dirA, 0700)
|
2016-09-24 22:45:25 +02:00
|
|
|
}
|