-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
341 lines (296 loc) · 10.7 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"sync"
"time"
)
// ScannedFile holds the path and information about a file or directory.
type ScannedFile struct {
Path string // Path is the location of the file or directory.
Info os.FileInfo // Info is the file information.
}
// ScanResult holds a collection of scanned files and directories, providing thread-safe access.
type ScanResult struct {
Files []ScannedFile
sync.Mutex
}
// AddFile safely adds a ScannedFile to the ScanResult.
func (result *ScanResult) AddFile(file ScannedFile) {
result.Lock()
defer result.Unlock()
result.Files = append(result.Files, file)
}
// scanUsingWalkDir scans the directory tree rooted at rootDir using filepath.WalkDir.
// It returns a slice of ScannedFile and any error encountered.
func scanUsingWalkDir(rootDir string) ([]ScannedFile, error) {
var result ScanResult
// Walk the directory tree rooted at rootDir.
err := filepath.WalkDir(rootDir, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err // If there is an error accessing the path, return the error.
}
info, err := entry.Info()
if err != nil {
return err // If there is an error retrieving the file info, return the error.
}
// Add the file to the scan result.
result.AddFile(ScannedFile{Path: path, Info: info})
return nil
})
// Return the scanned files and any error encountered during the walk.
return result.Files, err
}
// scanUsingGoroutines scans the directory tree rooted at rootDir using concurrent goroutines.
// It returns a slice of ScannedFile and any error encountered.
func scanUsingGoroutines(rootDir string, numCPU int, concurrencyMultiplier int) ([]ScannedFile, error) {
var result ScanResult
var wg sync.WaitGroup
// Baseline
if numCPU == 0 {
concurrencyMultiplier = 1
numCPU = 1
}
semaphore := make(chan struct{}, numCPU*concurrencyMultiplier) // Buffered channel to limit concurrent goroutines.
errChan := make(chan error, 1) // Channel to capture errors.
doneChan := make(chan struct{}) // Channel to signal completion.
// scanDirectory is a recursive function to scan directories.
var scanDirectory func(string)
scanDirectory = func(dirPath string) {
defer wg.Done() // Decrement the WaitGroup counter when the function returns.
semaphore <- struct{}{} // Acquire a slot in the semaphore.
defer func() { <-semaphore }() // Release the slot in the semaphore when done.
entries, err := os.ReadDir(dirPath)
if err != nil {
select {
case errChan <- err: // Send error to errChan if possible.
default:
}
return
}
buffer := make([]ScannedFile, 0, len(entries)) // Buffer to store scanned files.
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
select {
case errChan <- err: // Send error to errChan if possible.
default:
}
continue
}
// Append the scanned file to the buffer.
buffer = append(buffer, ScannedFile{Path: filepath.Join(dirPath, entry.Name()), Info: info})
if entry.IsDir() {
wg.Add(1) // Increment the WaitGroup counter for the new goroutine.
go scanDirectory(filepath.Join(dirPath, entry.Name())) // Scan the subdirectory.
}
}
// Append the buffer to the result safely.
result.Lock()
result.Files = append(result.Files, buffer...)
result.Unlock()
}
// Get information about the root directory.
rootInfo, err := os.Stat(rootDir)
if err != nil {
return nil, err
}
result.AddFile(ScannedFile{Path: rootDir, Info: rootInfo}) // Add the root directory to the result.
wg.Add(1) // Increment the WaitGroup counter for the initial scan.
go scanDirectory(rootDir) // Start scanning from the root directory.
// Wait for all goroutines to finish and close doneChan.
go func() {
wg.Wait()
close(doneChan)
}()
// Wait for completion or an error.
select {
case <-doneChan:
return result.Files, nil // Return the result if done.
case err := <-errChan:
return nil, err // Return the error if encountered.
}
}
// ScanComparison holds the comparison results of two directory scans.
type ScanComparison struct {
TotalWalkDirFiles int // Total number of files found using WalkDir.
TotalWalkDirDirs int // Total number of directories found using WalkDir.
TotalWalkDirSize int64 // Total size of all files found using WalkDir.
TotalWalkDirObjects int // Total number of files and directories found using WalkDir.
TotalGoroutineFiles int // Total number of files found using Goroutines.
TotalGoroutineDirs int // Total number of directories found using Goroutines.
TotalGoroutineSize int64 // Total size of all files found using Goroutines.
TotalGoroutineObjects int // Total number of files and directories found using Goroutines.
OnlyInWalkDir []string // Paths only found in WalkDir scan.
OnlyInGoroutines []string // Paths only found in Goroutines scan.
}
// compareScanResults compares the results of two directory scans.
// It returns a ScanComparison struct with detailed comparison results.
func compareScanResults(walkDirFiles, goroutineFiles []ScannedFile) ScanComparison {
walkDirMap := make(map[string]os.FileInfo)
goroutineMap := make(map[string]os.FileInfo)
var comparison ScanComparison
for _, file := range walkDirFiles {
walkDirMap[file.Path] = file.Info
if file.Info.IsDir() {
comparison.TotalWalkDirDirs++
} else {
comparison.TotalWalkDirFiles++
comparison.TotalWalkDirSize += file.Info.Size()
}
}
comparison.TotalWalkDirObjects = comparison.TotalWalkDirFiles + comparison.TotalWalkDirDirs
for _, file := range goroutineFiles {
goroutineMap[file.Path] = file.Info
if file.Info.IsDir() {
comparison.TotalGoroutineDirs++
} else {
comparison.TotalGoroutineFiles++
comparison.TotalGoroutineSize += file.Info.Size()
}
}
comparison.TotalGoroutineObjects = comparison.TotalGoroutineFiles + comparison.TotalGoroutineDirs
for path := range walkDirMap {
if _, found := goroutineMap[path]; !found {
comparison.OnlyInWalkDir = append(comparison.OnlyInWalkDir, path)
}
}
for path := range goroutineMap {
if _, found := walkDirMap[path]; !found {
comparison.OnlyInGoroutines = append(comparison.OnlyInGoroutines, path)
}
}
return comparison
}
// main is the entry point of the program. It scans a directory using both WalkDir and goroutines,
// then compares the results and prints a summary.
func main() {
var inputPath string
var concurrencyMultiplier int
// Define the flags
flag.StringVar(&inputPath, "path", "", "The path to the projects directory")
flag.IntVar(&concurrencyMultiplier, "concurrencyMultiplier", 1, "increase default concurrency from runtime.NumCPU() * 2 to runtime.NumCPU() * concurrencyMultiplier")
// Parse the flags
flag.Parse()
// Check if the input path is provided
if inputPath == "" {
fmt.Println("Please provide a path using the -path flag.")
return
}
var rootDir string
if runtime.GOOS == "windows" {
rootDir = filepath.Clean(inputPath)
} else {
rootDir = filepath.Clean(inputPath)
}
// Capture memory stats before and after each scan
var memStatsBefore, memStatsAfter runtime.MemStats
numCPU := runtime.NumCPU()
for concurrency := 0; concurrency <= numCPU; concurrency++ {
// Measure WalkDir performance
runtime.GC() // Force garbage collection
runtime.ReadMemStats(&memStatsBefore)
start := time.Now()
walkDirFiles, err := scanUsingWalkDir(rootDir)
if err != nil {
fmt.Println("Error:", err)
return
}
durationWalkDir := time.Since(start)
runtime.ReadMemStats(&memStatsAfter)
memUsedWalkDir := memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc
// Measure Goroutines performance
runtime.GC() // Force garbage collection
runtime.ReadMemStats(&memStatsBefore)
start = time.Now()
goroutineFiles, err := scanUsingGoroutines(rootDir, concurrency, concurrencyMultiplier)
if err != nil {
fmt.Println("Error:", err)
return
}
durationGoroutines := time.Since(start)
runtime.ReadMemStats(&memStatsAfter)
memUsedGoroutines := memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc
comparison := compareScanResults(walkDirFiles, goroutineFiles)
if len(comparison.OnlyInWalkDir) > 0 || len(comparison.OnlyInGoroutines) > 0 {
fmt.Println("Mismatch in number of files found!")
}
var speedIncreaseWalkDirRounded float64
var speedIncreaseGoroutinesRounded float64
// Determine the slower and faster execution times
var speedIncreaseWalkDir, speedIncreaseGoroutines float64
if durationWalkDir > durationGoroutines {
speedIncreaseWalkDir = 1.0
speedIncreaseGoroutines = float64(durationWalkDir) / float64(durationGoroutines)
speedIncreaseWalkDirRounded = float64(int(speedIncreaseWalkDir*10+0.5)) / 10
speedIncreaseGoroutinesRounded = float64(int(speedIncreaseGoroutines*10+0.5)) / 10
} else {
speedIncreaseWalkDir = float64(durationGoroutines) / float64(durationWalkDir)
speedIncreaseGoroutines = 1.0
speedIncreaseWalkDirRounded = float64(int(speedIncreaseWalkDir*10+0.5)) / 10
speedIncreaseGoroutinesRounded = float64(int(speedIncreaseGoroutines*10+0.5)) / 10
}
chartTitle := "Test Number: " + strconv.Itoa(concurrency) + " | Directory Scan Comparison: " + rootDir
chartConcurrency := strconv.Itoa(concurrency * concurrencyMultiplier)
if concurrency*concurrencyMultiplier == 0 {
chartTitle = "Baseline: No Concurrency | Directory Scan Comparison: " + rootDir
chartConcurrency = "N/A"
}
resultsTable := Table("DarkSimple", chartTitle)
resultsTable.AppendHeader([]interface{}{
"Function",
"NumCPUs",
"Concurrency",
"Files",
"Directories",
"Objects",
"Size",
"Memory",
"Execution Time",
"Speed Increase",
})
resultsTable.AppendRow([]interface{}{
"WalkDir",
"N/A",
"N/A",
comparison.TotalWalkDirFiles,
comparison.TotalWalkDirDirs,
comparison.TotalWalkDirObjects,
comparison.TotalWalkDirSize,
memUsedWalkDir,
durationWalkDir,
fmt.Sprintf("%.1fx", speedIncreaseWalkDirRounded),
})
resultsTable.AppendRow([]interface{}{
"Concurrent Read",
numCPU,
chartConcurrency,
comparison.TotalGoroutineFiles,
comparison.TotalGoroutineDirs,
comparison.TotalGoroutineObjects,
comparison.TotalGoroutineSize,
memUsedGoroutines,
durationGoroutines,
fmt.Sprintf("%.1fx", speedIncreaseGoroutinesRounded),
})
fmt.Println()
resultsTable.Render()
fmt.Println()
if len(comparison.OnlyInWalkDir) > 0 {
fmt.Println("Files only in WalkDir:")
for _, path := range comparison.OnlyInWalkDir {
fmt.Println(path)
}
}
if len(comparison.OnlyInGoroutines) > 0 {
fmt.Println("Files only in Goroutines:")
for _, path := range comparison.OnlyInGoroutines {
fmt.Println(path)
}
}
}
}