benchmark_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. "testing"
  10. "github.com/syncthing/syncthing/lib/db"
  11. "github.com/syncthing/syncthing/lib/fs"
  12. "github.com/syncthing/syncthing/lib/protocol"
  13. )
  14. var files, oneFile, firstHalf, secondHalf, changed100, unchanged100 []protocol.FileInfo
  15. func lazyInitBenchFiles() {
  16. if files != nil {
  17. return
  18. }
  19. files = make([]protocol.FileInfo, 0, 1000)
  20. for i := 0; i < 1000; i++ {
  21. files = append(files, protocol.FileInfo{
  22. Name: fmt.Sprintf("file%d", i),
  23. Version: protocol.Vector{Counters: []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. unchanged100 := files[100:200]
  32. changed100 := append([]protocol.FileInfo{}, unchanged100...)
  33. for i := range changed100 {
  34. changed100[i].Version = changed100[i].Version.Copy().Update(myID)
  35. }
  36. }
  37. func getBenchFileSet(b testing.TB) (*db.Lowlevel, *db.FileSet) {
  38. lazyInitBenchFiles()
  39. ldb := newLowlevelMemory(b)
  40. benchS := newFileSet(b, "test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  41. replace(benchS, remoteDevice0, files)
  42. replace(benchS, protocol.LocalDeviceID, firstHalf)
  43. return ldb, benchS
  44. }
  45. func BenchmarkReplaceAll(b *testing.B) {
  46. ldb := newLowlevelMemory(b)
  47. defer ldb.Close()
  48. b.ResetTimer()
  49. for i := 0; i < b.N; i++ {
  50. m := newFileSet(b, "test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  51. replace(m, protocol.LocalDeviceID, files)
  52. }
  53. b.ReportAllocs()
  54. }
  55. func BenchmarkUpdateOneChanged(b *testing.B) {
  56. ldb, benchS := getBenchFileSet(b)
  57. defer ldb.Close()
  58. changed := make([]protocol.FileInfo, 1)
  59. changed[0] = oneFile[0]
  60. changed[0].Version = changed[0].Version.Copy().Update(myID)
  61. b.ResetTimer()
  62. for i := 0; i < b.N; i++ {
  63. if i%2 == 0 {
  64. benchS.Update(protocol.LocalDeviceID, changed)
  65. } else {
  66. benchS.Update(protocol.LocalDeviceID, oneFile)
  67. }
  68. }
  69. b.ReportAllocs()
  70. }
  71. func BenchmarkUpdate100Changed(b *testing.B) {
  72. ldb, benchS := getBenchFileSet(b)
  73. defer ldb.Close()
  74. b.ResetTimer()
  75. for i := 0; i < b.N; i++ {
  76. if i%2 == 0 {
  77. benchS.Update(protocol.LocalDeviceID, changed100)
  78. } else {
  79. benchS.Update(protocol.LocalDeviceID, unchanged100)
  80. }
  81. }
  82. b.ReportAllocs()
  83. }
  84. func setup10Remotes(benchS *db.FileSet) {
  85. idBase := remoteDevice1.String()[1:]
  86. first := 'J'
  87. for i := 0; i < 10; i++ {
  88. id, _ := protocol.DeviceIDFromString(fmt.Sprintf("%v%s", first+rune(i), idBase))
  89. if i%2 == 0 {
  90. benchS.Update(id, changed100)
  91. } else {
  92. benchS.Update(id, unchanged100)
  93. }
  94. }
  95. }
  96. func BenchmarkUpdate100Changed10Remotes(b *testing.B) {
  97. ldb, benchS := getBenchFileSet(b)
  98. defer ldb.Close()
  99. setup10Remotes(benchS)
  100. b.ResetTimer()
  101. for i := 0; i < b.N; i++ {
  102. if i%2 == 0 {
  103. benchS.Update(protocol.LocalDeviceID, changed100)
  104. } else {
  105. benchS.Update(protocol.LocalDeviceID, unchanged100)
  106. }
  107. }
  108. b.ReportAllocs()
  109. }
  110. func BenchmarkUpdate100ChangedRemote(b *testing.B) {
  111. ldb, benchS := getBenchFileSet(b)
  112. defer ldb.Close()
  113. b.ResetTimer()
  114. for i := 0; i < b.N; i++ {
  115. if i%2 == 0 {
  116. benchS.Update(remoteDevice0, changed100)
  117. } else {
  118. benchS.Update(remoteDevice0, unchanged100)
  119. }
  120. }
  121. b.ReportAllocs()
  122. }
  123. func BenchmarkUpdate100ChangedRemote10Remotes(b *testing.B) {
  124. ldb, benchS := getBenchFileSet(b)
  125. defer ldb.Close()
  126. b.ResetTimer()
  127. for i := 0; i < b.N; i++ {
  128. if i%2 == 0 {
  129. benchS.Update(remoteDevice0, changed100)
  130. } else {
  131. benchS.Update(remoteDevice0, unchanged100)
  132. }
  133. }
  134. b.ReportAllocs()
  135. }
  136. func BenchmarkUpdateOneUnchanged(b *testing.B) {
  137. ldb, benchS := getBenchFileSet(b)
  138. defer ldb.Close()
  139. b.ResetTimer()
  140. for i := 0; i < b.N; i++ {
  141. benchS.Update(protocol.LocalDeviceID, oneFile)
  142. }
  143. b.ReportAllocs()
  144. }
  145. func BenchmarkNeedHalf(b *testing.B) {
  146. ldb, benchS := getBenchFileSet(b)
  147. defer ldb.Close()
  148. b.ResetTimer()
  149. for i := 0; i < b.N; i++ {
  150. count := 0
  151. snap := snapshot(b, benchS)
  152. snap.WithNeed(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
  153. count++
  154. return true
  155. })
  156. snap.Release()
  157. if count != len(secondHalf) {
  158. b.Errorf("wrong length %d != %d", count, len(secondHalf))
  159. }
  160. }
  161. b.ReportAllocs()
  162. }
  163. func BenchmarkNeedHalfRemote(b *testing.B) {
  164. ldb := newLowlevelMemory(b)
  165. defer ldb.Close()
  166. fset := newFileSet(b, "test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  167. replace(fset, remoteDevice0, firstHalf)
  168. replace(fset, protocol.LocalDeviceID, files)
  169. b.ResetTimer()
  170. for i := 0; i < b.N; i++ {
  171. count := 0
  172. snap := snapshot(b, fset)
  173. snap.WithNeed(remoteDevice0, func(fi protocol.FileIntf) bool {
  174. count++
  175. return true
  176. })
  177. snap.Release()
  178. if count != len(secondHalf) {
  179. b.Errorf("wrong length %d != %d", count, len(secondHalf))
  180. }
  181. }
  182. b.ReportAllocs()
  183. }
  184. func BenchmarkHave(b *testing.B) {
  185. ldb, benchS := getBenchFileSet(b)
  186. defer ldb.Close()
  187. b.ResetTimer()
  188. for i := 0; i < b.N; i++ {
  189. count := 0
  190. snap := snapshot(b, benchS)
  191. snap.WithHave(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
  192. count++
  193. return true
  194. })
  195. snap.Release()
  196. if count != len(firstHalf) {
  197. b.Errorf("wrong length %d != %d", count, len(firstHalf))
  198. }
  199. }
  200. b.ReportAllocs()
  201. }
  202. func BenchmarkGlobal(b *testing.B) {
  203. ldb, benchS := getBenchFileSet(b)
  204. defer ldb.Close()
  205. b.ResetTimer()
  206. for i := 0; i < b.N; i++ {
  207. count := 0
  208. snap := snapshot(b, benchS)
  209. snap.WithGlobal(func(fi protocol.FileIntf) bool {
  210. count++
  211. return true
  212. })
  213. snap.Release()
  214. if count != len(files) {
  215. b.Errorf("wrong length %d != %d", count, len(files))
  216. }
  217. }
  218. b.ReportAllocs()
  219. }
  220. func BenchmarkNeedHalfTruncated(b *testing.B) {
  221. ldb, benchS := getBenchFileSet(b)
  222. defer ldb.Close()
  223. b.ResetTimer()
  224. for i := 0; i < b.N; i++ {
  225. count := 0
  226. snap := snapshot(b, benchS)
  227. snap.WithNeedTruncated(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
  228. count++
  229. return true
  230. })
  231. snap.Release()
  232. if count != len(secondHalf) {
  233. b.Errorf("wrong length %d != %d", count, len(secondHalf))
  234. }
  235. }
  236. b.ReportAllocs()
  237. }
  238. func BenchmarkHaveTruncated(b *testing.B) {
  239. ldb, benchS := getBenchFileSet(b)
  240. defer ldb.Close()
  241. b.ResetTimer()
  242. for i := 0; i < b.N; i++ {
  243. count := 0
  244. snap := snapshot(b, benchS)
  245. snap.WithHaveTruncated(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
  246. count++
  247. return true
  248. })
  249. snap.Release()
  250. if count != len(firstHalf) {
  251. b.Errorf("wrong length %d != %d", count, len(firstHalf))
  252. }
  253. }
  254. b.ReportAllocs()
  255. }
  256. func BenchmarkGlobalTruncated(b *testing.B) {
  257. ldb, benchS := getBenchFileSet(b)
  258. defer ldb.Close()
  259. b.ResetTimer()
  260. for i := 0; i < b.N; i++ {
  261. count := 0
  262. snap := snapshot(b, benchS)
  263. snap.WithGlobalTruncated(func(fi protocol.FileIntf) bool {
  264. count++
  265. return true
  266. })
  267. snap.Release()
  268. if count != len(files) {
  269. b.Errorf("wrong length %d != %d", count, len(files))
  270. }
  271. }
  272. b.ReportAllocs()
  273. }
  274. func BenchmarkNeedCount(b *testing.B) {
  275. ldb, benchS := getBenchFileSet(b)
  276. defer ldb.Close()
  277. benchS.Update(protocol.LocalDeviceID, changed100)
  278. b.ResetTimer()
  279. for i := 0; i < b.N; i++ {
  280. snap := snapshot(b, benchS)
  281. _ = snap.NeedSize(protocol.LocalDeviceID)
  282. snap.Release()
  283. }
  284. b.ReportAllocs()
  285. }