aboutsummaryrefslogtreecommitdiff
path: root/tests/reverse/longname_perf_test.go
blob: f170ad74b668a88b3a97fd8e1fb058a844b018f7 (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
package reverse_test

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

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

// Create 10000 files with long names
func generateLongnameFiles(dir string) {
	x240 := string(bytes.Repeat([]byte("x"), 240))
	for i := 0; i < 100000; i++ {
		n := genName(i, x240)
		f, err := os.Create(dir + "/" + n)
		if err != nil {
			panic(err)
		}
		f.Close()
	}
}

func BenchmarkLongnameStat(b *testing.B) {
	// Setup
	generateLongnameFiles(dirA)
	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
	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)
}