casefs_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. var fakefs *fakeFS
  148. if ffs, ok := unwrapFilesystem(fsys, filesystemWrapperTypeNone); ok {
  149. fakefs = ffs.(*fakeFS)
  150. }
  151. fakefs.resetCounters()
  152. benchmarkWalkFakeFS(b, fsys, paths, 0, "")
  153. fakefs.reportMetricsPerOp(b)
  154. fakefs.reportMetricsPer(b, entries, "entry")
  155. b.ReportAllocs()
  156. })
  157. b.Run("casefs", func(b *testing.B) {
  158. // Construct the casefs manually or it will get cached and the benchmark is invalid.
  159. casefs := &caseFilesystem{
  160. Filesystem: fsys,
  161. realCaser: newDefaultRealCaser(fsys),
  162. }
  163. var fakefs *fakeFS
  164. if ffs, ok := unwrapFilesystem(fsys, filesystemWrapperTypeNone); ok {
  165. fakefs = ffs.(*fakeFS)
  166. }
  167. fakefs.resetCounters()
  168. benchmarkWalkFakeFS(b, casefs, paths, 0, "")
  169. fakefs.reportMetricsPerOp(b)
  170. fakefs.reportMetricsPer(b, entries, "entry")
  171. b.ReportAllocs()
  172. })
  173. var otherOpPath string
  174. sep := string(PathSeparator)
  175. longest := 0
  176. for _, p := range paths {
  177. if length := len(strings.Split(p, sep)); length > longest {
  178. otherOpPath = p
  179. longest = length
  180. }
  181. }
  182. otherOpEvery := 1000
  183. b.Run(fmt.Sprintf("casefs-otherOpEvery%v", otherOpEvery), func(b *testing.B) {
  184. // Construct the casefs manually or it will get cached and the benchmark is invalid.
  185. casefs := &caseFilesystem{
  186. Filesystem: fsys,
  187. realCaser: newDefaultRealCaser(fsys),
  188. }
  189. var fakefs *fakeFS
  190. if ffs, ok := unwrapFilesystem(fsys, filesystemWrapperTypeNone); ok {
  191. fakefs = ffs.(*fakeFS)
  192. }
  193. fakefs.resetCounters()
  194. benchmarkWalkFakeFS(b, casefs, paths, otherOpEvery, otherOpPath)
  195. fakefs.reportMetricsPerOp(b)
  196. fakefs.reportMetricsPer(b, entries, "entry")
  197. b.ReportAllocs()
  198. })
  199. }
  200. func benchmarkWalkFakeFS(b *testing.B, fsys Filesystem, paths []string, otherOpEvery int, otherOpPath string) {
  201. // Simulate a scanner pass over the filesystem. First walk it to
  202. // discover all names, then stat each name individually to check if it's
  203. // been deleted or not (pretending that they all existed in the
  204. // database).
  205. var ms0 runtime.MemStats
  206. runtime.ReadMemStats(&ms0)
  207. t0 := time.Now()
  208. for i := 0; i < b.N; i++ {
  209. if err := doubleWalkFSWithOtherOps(fsys, paths, otherOpEvery, otherOpPath); err != nil {
  210. b.Fatal(err)
  211. }
  212. }
  213. t1 := time.Now()
  214. var ms1 runtime.MemStats
  215. runtime.ReadMemStats(&ms1)
  216. // We add metrics per path entry
  217. b.ReportMetric(float64(t1.Sub(t0))/float64(b.N)/float64(len(paths)), "ns/entry")
  218. b.ReportMetric(float64(ms1.Mallocs-ms0.Mallocs)/float64(b.N)/float64(len(paths)), "allocs/entry")
  219. b.ReportMetric(float64(ms1.TotalAlloc-ms0.TotalAlloc)/float64(b.N)/float64(len(paths)), "B/entry")
  220. }
  221. func TestStressCaseFS(t *testing.T) {
  222. // Exercise a bunch of paralell operations for stressing out race
  223. // conditions in the realnamer cache etc.
  224. const limit = 10 * time.Second
  225. if testing.Short() {
  226. t.Skip("long test")
  227. }
  228. fsys, paths, err := fakefsForBenchmark(10_000, 0)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. for i := 0; i < runtime.NumCPU()/2+1; i++ {
  233. t.Run(fmt.Sprintf("walker-%d", i), func(t *testing.T) {
  234. // Walk the filesystem and stat everything
  235. t.Parallel()
  236. t0 := time.Now()
  237. for time.Since(t0) < limit {
  238. if err := doubleWalkFS(fsys, paths); err != nil {
  239. t.Fatal(err)
  240. }
  241. }
  242. })
  243. t.Run(fmt.Sprintf("toucher-%d", i), func(t *testing.T) {
  244. // Touch all the things
  245. t.Parallel()
  246. t0 := time.Now()
  247. for time.Since(t0) < limit {
  248. for _, p := range paths {
  249. now := time.Now()
  250. if err := fsys.Chtimes(p, now, now); err != nil {
  251. t.Fatal(err)
  252. }
  253. }
  254. }
  255. })
  256. }
  257. }
  258. func doubleWalkFS(fsys Filesystem, paths []string) error {
  259. return doubleWalkFSWithOtherOps(fsys, paths, 0, "")
  260. }
  261. func doubleWalkFSWithOtherOps(fsys Filesystem, paths []string, otherOpEvery int, otherOpPath string) error {
  262. i := 0
  263. if err := fsys.Walk("/", func(path string, info FileInfo, err error) error {
  264. i++
  265. if otherOpEvery != 0 && i%otherOpEvery == 0 {
  266. // l.Infoln("AAA", otherOpPath)
  267. if _, err := fsys.Lstat(otherOpPath); err != nil {
  268. return err
  269. }
  270. }
  271. // l.Infoln("CCC", path)
  272. return err
  273. }); err != nil {
  274. return err
  275. }
  276. for _, p := range paths {
  277. for p != "." {
  278. i++
  279. if otherOpEvery != 0 && i%otherOpEvery == 0 {
  280. if _, err := fsys.Lstat(otherOpPath); err != nil {
  281. // l.Infoln("AAA", otherOpPath)
  282. return err
  283. }
  284. }
  285. // l.Infoln("CCC", p)
  286. if _, err := fsys.Lstat(p); err != nil {
  287. return err
  288. }
  289. p = filepath.Dir(p)
  290. }
  291. }
  292. return nil
  293. }
  294. func fakefsForBenchmark(nfiles int, latency time.Duration) (Filesystem, []string, error) {
  295. fsys := NewFilesystem(FilesystemTypeFake, fmt.Sprintf("fakefsForBenchmark?files=%d&insens=true&latency=%s", nfiles, latency))
  296. var paths []string
  297. if err := fsys.Walk("/", func(path string, info FileInfo, err error) error {
  298. paths = append(paths, path)
  299. return err
  300. }); err != nil {
  301. return nil, nil, err
  302. }
  303. if len(paths) < nfiles {
  304. return nil, nil, errors.New("didn't find enough stuff")
  305. }
  306. sort.Strings(paths)
  307. return fsys, paths, nil
  308. }