casefs_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (C) 2020 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "errors"
  9. "fmt"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "sort"
  14. "strings"
  15. "testing"
  16. "time"
  17. )
  18. func TestRealCase(t *testing.T) {
  19. // Verify realCase lookups on various underlying filesystems.
  20. t.Run("fake-sensitive", func(t *testing.T) {
  21. testRealCase(t, newFakeFilesystem(t.Name()))
  22. })
  23. t.Run("fake-insensitive", func(t *testing.T) {
  24. testRealCase(t, newFakeFilesystem(t.Name()+"?insens=true"))
  25. })
  26. t.Run("actual", func(t *testing.T) {
  27. fsys, tmpDir := setup(t)
  28. defer os.RemoveAll(tmpDir)
  29. testRealCase(t, fsys)
  30. })
  31. }
  32. func testRealCase(t *testing.T, fsys Filesystem) {
  33. testFs := NewCaseFilesystem(fsys).(*caseFilesystem)
  34. comps := []string{"Foo", "bar", "BAZ", "bAs"}
  35. path := filepath.Join(comps...)
  36. testFs.MkdirAll(filepath.Join(comps[:len(comps)-1]...), 0777)
  37. fd, err := testFs.Create(path)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. fd.Close()
  42. for i, tc := range []struct {
  43. in string
  44. len int
  45. }{
  46. {path, 4},
  47. {strings.ToLower(path), 4},
  48. {strings.ToUpper(path), 4},
  49. {"foo", 1},
  50. {"FOO", 1},
  51. {"foO", 1},
  52. {filepath.Join("Foo", "bar"), 2},
  53. {filepath.Join("Foo", "bAr"), 2},
  54. {filepath.Join("FoO", "bar"), 2},
  55. {filepath.Join("foo", "bar", "BAZ"), 3},
  56. {filepath.Join("Foo", "bar", "bAz"), 3},
  57. {filepath.Join("foo", "bar", "BAZ"), 3}, // Repeat on purpose
  58. } {
  59. out, err := testFs.realCase(tc.in)
  60. if err != nil {
  61. t.Error(err)
  62. } else if exp := filepath.Join(comps[:tc.len]...); out != exp {
  63. t.Errorf("tc %v: Expected %v, got %v", i, exp, out)
  64. }
  65. }
  66. }
  67. func TestRealCaseSensitive(t *testing.T) {
  68. // Verify that realCase returns the best on-disk case for case sensitive
  69. // systems. Test is skipped if the underlying fs is insensitive.
  70. t.Run("fake-sensitive", func(t *testing.T) {
  71. testRealCaseSensitive(t, newFakeFilesystem(t.Name()))
  72. })
  73. t.Run("actual", func(t *testing.T) {
  74. fsys, tmpDir := setup(t)
  75. defer os.RemoveAll(tmpDir)
  76. testRealCaseSensitive(t, fsys)
  77. })
  78. }
  79. func testRealCaseSensitive(t *testing.T, fsys Filesystem) {
  80. testFs := NewCaseFilesystem(fsys).(*caseFilesystem)
  81. names := make([]string, 2)
  82. names[0] = "foo"
  83. names[1] = strings.ToUpper(names[0])
  84. for _, n := range names {
  85. if err := testFs.MkdirAll(n, 0777); err != nil {
  86. if IsErrCaseConflict(err) {
  87. t.Skip("Filesystem is case-insensitive")
  88. }
  89. t.Fatal(err)
  90. }
  91. }
  92. for _, n := range names {
  93. if rn, err := testFs.realCase(n); err != nil {
  94. t.Error(err)
  95. } else if rn != n {
  96. t.Errorf("Got %v, expected %v", rn, n)
  97. }
  98. }
  99. }
  100. func TestCaseFSStat(t *testing.T) {
  101. // Verify that a Stat() lookup behaves in a case sensitive manner
  102. // regardless of the underlying fs.
  103. t.Run("fake-sensitive", func(t *testing.T) {
  104. testCaseFSStat(t, newFakeFilesystem(t.Name()))
  105. })
  106. t.Run("fake-insensitive", func(t *testing.T) {
  107. testCaseFSStat(t, newFakeFilesystem(t.Name()+"?insens=true"))
  108. })
  109. t.Run("actual", func(t *testing.T) {
  110. fsys, tmpDir := setup(t)
  111. defer os.RemoveAll(tmpDir)
  112. testCaseFSStat(t, fsys)
  113. })
  114. }
  115. func testCaseFSStat(t *testing.T, fsys Filesystem) {
  116. fd, err := fsys.Create("foo")
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. fd.Close()
  121. // Check if the underlying fs is sensitive or not
  122. sensitive := true
  123. if _, err = fsys.Stat("FOO"); err == nil {
  124. sensitive = false
  125. }
  126. testFs := NewCaseFilesystem(fsys)
  127. _, err = testFs.Stat("FOO")
  128. if sensitive {
  129. if IsNotExist(err) {
  130. t.Log("pass: case sensitive underlying fs")
  131. } else {
  132. t.Error("expected NotExist, not", err, "for sensitive fs")
  133. }
  134. } else if IsErrCaseConflict(err) {
  135. t.Log("pass: case insensitive underlying fs")
  136. } else {
  137. t.Error("expected ErrCaseConflict, not", err, "for insensitive fs")
  138. }
  139. }
  140. func BenchmarkWalkCaseFakeFS100k(b *testing.B) {
  141. const entries = 100_000
  142. fsys, paths, err := fakefsForBenchmark(entries, 0)
  143. if err != nil {
  144. b.Fatal(err)
  145. }
  146. b.Run("rawfs", func(b *testing.B) {
  147. fakefs := unwrapFilesystem(fsys).(*fakefs)
  148. fakefs.resetCounters()
  149. benchmarkWalkFakeFS(b, fsys, paths)
  150. fakefs.reportMetricsPerOp(b)
  151. fakefs.reportMetricsPer(b, entries, "entry")
  152. b.ReportAllocs()
  153. })
  154. b.Run("casefs", func(b *testing.B) {
  155. // Construct the casefs manually or it will get cached and the benchmark is invalid.
  156. casefs := &caseFilesystem{
  157. Filesystem: fsys,
  158. realCaser: newDefaultRealCaser(fsys),
  159. }
  160. fakefs := unwrapFilesystem(fsys).(*fakefs)
  161. fakefs.resetCounters()
  162. benchmarkWalkFakeFS(b, casefs, paths)
  163. fakefs.reportMetricsPerOp(b)
  164. fakefs.reportMetricsPer(b, entries, "entry")
  165. b.ReportAllocs()
  166. })
  167. }
  168. func benchmarkWalkFakeFS(b *testing.B, fsys Filesystem, paths []string) {
  169. // Simulate a scanner pass over the filesystem. First walk it to
  170. // discover all names, then stat each name individually to check if it's
  171. // been deleted or not (pretending that they all existed in the
  172. // database).
  173. var ms0 runtime.MemStats
  174. runtime.ReadMemStats(&ms0)
  175. t0 := time.Now()
  176. for i := 0; i < b.N; i++ {
  177. if err := doubleWalkFS(fsys, paths); err != nil {
  178. b.Fatal(err)
  179. }
  180. }
  181. t1 := time.Now()
  182. var ms1 runtime.MemStats
  183. runtime.ReadMemStats(&ms1)
  184. // We add metrics per path entry
  185. b.ReportMetric(float64(t1.Sub(t0))/float64(b.N)/float64(len(paths)), "ns/entry")
  186. b.ReportMetric(float64(ms1.Mallocs-ms0.Mallocs)/float64(b.N)/float64(len(paths)), "allocs/entry")
  187. b.ReportMetric(float64(ms1.TotalAlloc-ms0.TotalAlloc)/float64(b.N)/float64(len(paths)), "B/entry")
  188. }
  189. func TestStressCaseFS(t *testing.T) {
  190. // Exercise a bunch of paralell operations for stressing out race
  191. // conditions in the realnamer cache etc.
  192. const limit = 10 * time.Second
  193. if testing.Short() {
  194. t.Skip("long test")
  195. }
  196. fsys, paths, err := fakefsForBenchmark(10_000, 0)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. for i := 0; i < runtime.NumCPU()/2+1; i++ {
  201. t.Run(fmt.Sprintf("walker-%d", i), func(t *testing.T) {
  202. // Walk the filesystem and stat everything
  203. t.Parallel()
  204. t0 := time.Now()
  205. for time.Since(t0) < limit {
  206. if err := doubleWalkFS(fsys, paths); err != nil {
  207. t.Fatal(err)
  208. }
  209. }
  210. })
  211. t.Run(fmt.Sprintf("toucher-%d", i), func(t *testing.T) {
  212. // Touch all the things
  213. t.Parallel()
  214. t0 := time.Now()
  215. for time.Since(t0) < limit {
  216. for _, p := range paths {
  217. now := time.Now()
  218. if err := fsys.Chtimes(p, now, now); err != nil {
  219. t.Fatal(err)
  220. }
  221. }
  222. }
  223. })
  224. }
  225. }
  226. func doubleWalkFS(fsys Filesystem, paths []string) error {
  227. if err := fsys.Walk("/", func(path string, info FileInfo, err error) error {
  228. return err
  229. }); err != nil {
  230. return err
  231. }
  232. for _, p := range paths {
  233. if _, err := fsys.Lstat(p); err != nil {
  234. return err
  235. }
  236. }
  237. return nil
  238. }
  239. func fakefsForBenchmark(nfiles int, latency time.Duration) (Filesystem, []string, error) {
  240. fsys := NewFilesystem(FilesystemTypeFake, fmt.Sprintf("fakefsForBenchmark?files=%d&insens=true&latency=%s", nfiles, latency))
  241. var paths []string
  242. if err := fsys.Walk("/", func(path string, info FileInfo, err error) error {
  243. paths = append(paths, path)
  244. return err
  245. }); err != nil {
  246. return nil, nil, err
  247. }
  248. if len(paths) < nfiles {
  249. return nil, nil, errors.New("didn't find enough stuff")
  250. }
  251. sort.Strings(paths)
  252. return fsys, paths, nil
  253. }