-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
skywalker_test.go
141 lines (126 loc) · 3.15 KB
/
skywalker_test.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//Copyright (c) 2017, Will Dixon. All rights reserved.
//Use of this source code is governed by a BSD-style
//license that can be found in the LICENSE file.
//nolint
package skywalker_test
import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
var (
root = "testingFolder"
benchRoot = "benchmarking"
subFolders = []string{"the", "subfolder", "sub", "sub/folder/subfolder"}
subFiles = []string{"just.txt", "a.log", "few.pdf", "files"}
)
type TestWorker struct {
*sync.Mutex
found map[string]struct{}
}
func NewTW() *TestWorker {
found := make(map[string]struct{})
mutext := new(sync.Mutex)
return &TestWorker{
Mutex: mutext,
found: found,
}
}
func (tw *TestWorker) Work(path string) {
tw.Lock()
defer tw.Unlock()
tw.found[path] = struct{}{}
}
func TestMain(m *testing.M) {
if err := standupData(); err != nil {
log.Fatal(err)
}
retCode := m.Run()
teardownData()
os.Exit(retCode)
}
func BenchmarkFilepathWalk(b *testing.B) {
defer teardownBenchmark()
standupBenchmark(b.N)
tw := NewTW()
b.ResetTimer()
filepath.Walk(filepath.Join(root, benchRoot), func(path string, _ os.FileInfo, _ error) error {
tw.Work(path)
return nil
})
b.ReportAllocs()
}
func BenchmarkSkyWalker(b *testing.B) {
for _, bc := range benchCases {
b.Run(bc.name, func(b *testing.B) {
defer teardownBenchmark()
standupBenchmark(b.N)
sw, _ := bc.Test()
sw.Root = filepath.Join(root, benchRoot)
b.ResetTimer()
sw.Walk()
b.ReportAllocs()
})
}
}
func TestWalk(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)
sw, tw := tc.Test()
err := sw.Walk()
assert.Equal(tc.expectedErr, (err != nil), "Was/Wasn't expecting an error")
assert.Equal(len(tc.expected), len(tw.found), "Not the expected number of results")
for _, e := range tc.expected {
path, _ := filepath.Abs(e)
_, ok := tw.found[path]
assert.True(ok, "Could not find %s", e)
}
})
}
}
func standupBenchmark(num int) error {
for i := 0; i < num; i++ {
name := strconv.Itoa(i)
folders := strings.Split(name, "")
if len(name) > 1 {
path := filepath.Join(folders[:len(folders)-1]...)
fileName := folders[len(folders)-1]
fileName = fileName + "." + fileName
os.MkdirAll(filepath.Join(root, benchRoot, path), 0777)
file, _ := os.OpenFile(filepath.Join(root, benchRoot, path, fileName), os.O_CREATE|os.O_RDONLY, 0666)
defer file.Close()
} else {
os.MkdirAll(filepath.Join(root, benchRoot), 0777)
file, _ := os.OpenFile(filepath.Join(root, benchRoot, name+"."+name), os.O_CREATE|os.O_RDONLY, 0666)
defer file.Close()
}
}
return nil
}
func teardownBenchmark() error {
return os.RemoveAll(filepath.Join(root, benchRoot))
}
func standupData() error {
for _, sf := range subFolders {
if err := os.MkdirAll(filepath.Join(root, sf), 0777); err != nil {
return err
}
for _, f := range subFiles {
file, err := os.OpenFile(filepath.Join(root, sf, f), os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
defer file.Close()
}
}
return nil
}
func teardownData() error {
return os.RemoveAll(root)
}