benchmark_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright (C) 2015 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 db_test
  7. import (
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "github.com/syncthing/syncthing/lib/db"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. )
  17. var files, oneFile, firstHalf, secondHalf []protocol.FileInfo
  18. var s *db.FileSet
  19. func init() {
  20. for i := 0; i < 1000; i++ {
  21. files = append(files, protocol.FileInfo{
  22. Name: fmt.Sprintf("file%d", i),
  23. Version: protocol.Vector{[]protocol.Counter{{ID: myID, Value: 1000}}},
  24. Blocks: genBlocks(i),
  25. })
  26. }
  27. middle := len(files) / 2
  28. firstHalf = files[:middle]
  29. secondHalf = files[middle:]
  30. oneFile = firstHalf[middle-1 : middle]
  31. ldb, _ := tempDB()
  32. s = db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  33. replace(s, remoteDevice0, files)
  34. replace(s, protocol.LocalDeviceID, firstHalf)
  35. }
  36. func tempDB() (*db.Instance, string) {
  37. dir, err := ioutil.TempDir("", "syncthing")
  38. if err != nil {
  39. panic(err)
  40. }
  41. dbi, err := db.Open(filepath.Join(dir, "db"))
  42. if err != nil {
  43. panic(err)
  44. }
  45. return dbi, dir
  46. }
  47. func BenchmarkReplaceAll(b *testing.B) {
  48. ldb, dir := tempDB()
  49. defer func() {
  50. ldb.Close()
  51. os.RemoveAll(dir)
  52. }()
  53. b.ResetTimer()
  54. for i := 0; i < b.N; i++ {
  55. m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  56. replace(m, protocol.LocalDeviceID, files)
  57. }
  58. b.ReportAllocs()
  59. }
  60. func BenchmarkUpdateOneChanged(b *testing.B) {
  61. changed := make([]protocol.FileInfo, 1)
  62. changed[0] = oneFile[0]
  63. changed[0].Version = changed[0].Version.Update(myID)
  64. changed[0].Blocks = genBlocks(len(changed[0].Blocks))
  65. for i := 0; i < b.N; i++ {
  66. if i%1 == 0 {
  67. s.Update(protocol.LocalDeviceID, changed)
  68. } else {
  69. s.Update(protocol.LocalDeviceID, oneFile)
  70. }
  71. }
  72. b.ReportAllocs()
  73. }
  74. func BenchmarkUpdateOneUnchanged(b *testing.B) {
  75. for i := 0; i < b.N; i++ {
  76. s.Update(protocol.LocalDeviceID, oneFile)
  77. }
  78. b.ReportAllocs()
  79. }
  80. func BenchmarkNeedHalf(b *testing.B) {
  81. for i := 0; i < b.N; i++ {
  82. count := 0
  83. s.WithNeed(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
  84. count++
  85. return true
  86. })
  87. if count != len(secondHalf) {
  88. b.Errorf("wrong length %d != %d", count, len(secondHalf))
  89. }
  90. }
  91. b.ReportAllocs()
  92. }
  93. func BenchmarkHave(b *testing.B) {
  94. for i := 0; i < b.N; i++ {
  95. count := 0
  96. s.WithHave(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
  97. count++
  98. return true
  99. })
  100. if count != len(firstHalf) {
  101. b.Errorf("wrong length %d != %d", count, len(firstHalf))
  102. }
  103. }
  104. b.ReportAllocs()
  105. }
  106. func BenchmarkGlobal(b *testing.B) {
  107. for i := 0; i < b.N; i++ {
  108. count := 0
  109. s.WithGlobal(func(fi db.FileIntf) bool {
  110. count++
  111. return true
  112. })
  113. if count != len(files) {
  114. b.Errorf("wrong length %d != %d", count, len(files))
  115. }
  116. }
  117. b.ReportAllocs()
  118. }
  119. func BenchmarkNeedHalfTruncated(b *testing.B) {
  120. for i := 0; i < b.N; i++ {
  121. count := 0
  122. s.WithNeedTruncated(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
  123. count++
  124. return true
  125. })
  126. if count != len(secondHalf) {
  127. b.Errorf("wrong length %d != %d", count, len(secondHalf))
  128. }
  129. }
  130. b.ReportAllocs()
  131. }
  132. func BenchmarkHaveTruncated(b *testing.B) {
  133. for i := 0; i < b.N; i++ {
  134. count := 0
  135. s.WithHaveTruncated(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
  136. count++
  137. return true
  138. })
  139. if count != len(firstHalf) {
  140. b.Errorf("wrong length %d != %d", count, len(firstHalf))
  141. }
  142. }
  143. b.ReportAllocs()
  144. }
  145. func BenchmarkGlobalTruncated(b *testing.B) {
  146. for i := 0; i < b.N; i++ {
  147. count := 0
  148. s.WithGlobalTruncated(func(fi db.FileIntf) bool {
  149. count++
  150. return true
  151. })
  152. if count != len(files) {
  153. b.Errorf("wrong length %d != %d", count, len(files))
  154. }
  155. }
  156. b.ReportAllocs()
  157. }