github.com/charlievieth/fastwalk@v1.0.3/fastwalk_darwin_test.go (about) 1 //go:build darwin && go1.13 && !appengine 2 // +build darwin,go1.13,!appengine 3 4 package fastwalk 5 6 import ( 7 "flag" 8 "io/fs" 9 "os" 10 "path/filepath" 11 "runtime" 12 "sort" 13 "strconv" 14 "testing" 15 ) 16 17 func TestDarwinReaddir(t *testing.T) { 18 wd, err := os.Getwd() 19 if err != nil { 20 t.Fatal(err) 21 } 22 want, err := os.ReadDir(wd) 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 rdEnts, err := os.ReadDir(wd) 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 var gdEnts []fs.DirEntry 33 err = readDir(wd, func(_, _ string, de fs.DirEntry) error { 34 gdEnts = append(gdEnts, de) 35 return nil 36 }) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 sort.Slice(rdEnts, func(i, j int) bool { 42 return rdEnts[i].Name() < rdEnts[j].Name() 43 }) 44 sort.Slice(gdEnts, func(i, j int) bool { 45 return gdEnts[i].Name() < gdEnts[j].Name() 46 }) 47 48 sameDirEntry := func(d1, d2 fs.DirEntry) bool { 49 if d1.Name() != d2.Name() || d1.IsDir() != d2.IsDir() || d1.Type() != d2.Type() { 50 return false 51 } 52 fi1, e1 := d1.Info() 53 fi2, e2 := d2.Info() 54 if e1 != e2 { 55 return false 56 } 57 return os.SameFile(fi1, fi2) 58 } 59 60 for i := range want { 61 de := want[i] 62 re := rdEnts[i] 63 ge := gdEnts[i] 64 if !sameDirEntry(de, re) { 65 t.Errorf("Readir: %q: want: %#v get: %#v", de.Name(), de, re) 66 } 67 if !sameDirEntry(de, ge) { 68 t.Errorf("Getdirentries: %q: want: %#v get: %#v", de.Name(), de, ge) 69 } 70 } 71 if len(rdEnts) != len(want) { 72 t.Errorf("Readir returned %d entries want: %d", len(rdEnts), len(want)) 73 } 74 if len(gdEnts) != len(want) { 75 t.Errorf("Getdirentries returned %d entries want: %d", len(gdEnts), len(want)) 76 } 77 } 78 79 var benchDir = flag.String("benchdir", runtime.GOROOT(), "The directory to scan for BenchmarkFastWalk") 80 81 func noopReadDirFunc(_, _ string, _ fs.DirEntry) error { 82 return nil 83 } 84 85 func benchmarkReadDir(b *testing.B, parallel bool, fn func(dirName string, fn func(dirName, entName string, de fs.DirEntry) error) error) { 86 mktemp := func(sz int) string { 87 dir := filepath.Join(b.TempDir(), strconv.Itoa(sz)) 88 if err := os.MkdirAll(dir, 0755); err != nil { 89 b.Fatal(err) 90 } 91 for i := 0; i < sz; i++ { 92 name := strconv.Itoa(i) 93 if err := os.WriteFile(filepath.Join(dir, name), []byte(name), 0644); err != nil { 94 b.Fatal(err) 95 } 96 } 97 return dir 98 } 99 sizes := []int{4, 8, 16, 32, 64, 128, 256} 100 for _, sz := range sizes { 101 dir := mktemp(sz) 102 b.Run(strconv.Itoa(sz), func(b *testing.B) { 103 if parallel { 104 b.RunParallel(func(pb *testing.PB) { 105 for pb.Next() { 106 fn(dir, noopReadDirFunc) 107 } 108 }) 109 } else { 110 for i := 0; i < b.N; i++ { 111 fn(dir, noopReadDirFunc) 112 } 113 } 114 }) 115 } 116 } 117 118 func BenchmarkReadDir(b *testing.B) { 119 benchmarkReadDir(b, false, readDir) 120 } 121 122 func BenchmarkReadDirParallel(b *testing.B) { 123 dirname := *benchDir 124 b.RunParallel(func(pb *testing.PB) { 125 for pb.Next() { 126 if err := readDir(dirname, noopReadDirFunc); err != nil { 127 b.Fatal(err) 128 } 129 } 130 }) 131 }