db_bench_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright (C) 2025 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 sqlite
  7. import (
  8. "context"
  9. "fmt"
  10. "testing"
  11. "time"
  12. "github.com/syncthing/syncthing/internal/timeutil"
  13. "github.com/syncthing/syncthing/lib/config"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. "github.com/syncthing/syncthing/lib/rand"
  16. )
  17. var globalFi protocol.FileInfo
  18. func BenchmarkUpdate(b *testing.B) {
  19. db, err := OpenTemp()
  20. if err != nil {
  21. b.Fatal(err)
  22. }
  23. b.Cleanup(func() {
  24. if err := db.Close(); err != nil {
  25. b.Fatal(err)
  26. }
  27. })
  28. svc := db.Service(time.Hour).(*Service)
  29. fs := make([]protocol.FileInfo, 100)
  30. seed := 0
  31. size := 10000
  32. for size < 200_000 {
  33. t0 := time.Now()
  34. if err := svc.periodic(context.Background()); err != nil {
  35. b.Fatal(err)
  36. }
  37. b.Log("garbage collect in", time.Since(t0))
  38. for {
  39. local, err := db.CountLocal(folderID, protocol.LocalDeviceID)
  40. if err != nil {
  41. b.Fatal(err)
  42. }
  43. if local.Files >= size {
  44. break
  45. }
  46. fs := make([]protocol.FileInfo, 1000)
  47. for i := range fs {
  48. fs[i] = genFile(rand.String(24), 64, 0)
  49. }
  50. if err := db.Update(folderID, protocol.LocalDeviceID, fs); err != nil {
  51. b.Fatal(err)
  52. }
  53. }
  54. b.Run(fmt.Sprintf("Insert100Loc@%d", size), func(b *testing.B) {
  55. for range b.N {
  56. for i := range fs {
  57. fs[i] = genFile(rand.String(24), 64, 0)
  58. }
  59. if err := db.Update(folderID, protocol.LocalDeviceID, fs); err != nil {
  60. b.Fatal(err)
  61. }
  62. }
  63. b.ReportMetric(float64(b.N)*100.0/b.Elapsed().Seconds(), "files/s")
  64. })
  65. b.Run(fmt.Sprintf("RepBlocks100@%d", size), func(b *testing.B) {
  66. for range b.N {
  67. for i := range fs {
  68. fs[i].Blocks = genBlocks(fs[i].Name, seed, 64)
  69. fs[i].Version = fs[i].Version.Update(42)
  70. }
  71. seed++
  72. if err := db.Update(folderID, protocol.LocalDeviceID, fs); err != nil {
  73. b.Fatal(err)
  74. }
  75. }
  76. b.ReportMetric(float64(b.N)*100.0/b.Elapsed().Seconds(), "files/s")
  77. })
  78. b.Run(fmt.Sprintf("RepSame100@%d", size), func(b *testing.B) {
  79. for range b.N {
  80. for i := range fs {
  81. fs[i].Version = fs[i].Version.Update(42)
  82. }
  83. if err := db.Update(folderID, protocol.LocalDeviceID, fs); err != nil {
  84. b.Fatal(err)
  85. }
  86. }
  87. b.ReportMetric(float64(b.N)*100.0/b.Elapsed().Seconds(), "files/s")
  88. })
  89. b.Run(fmt.Sprintf("Insert100Rem@%d", size), func(b *testing.B) {
  90. for range b.N {
  91. for i := range fs {
  92. fs[i].Blocks = genBlocks(fs[i].Name, seed, 64)
  93. fs[i].Version = fs[i].Version.Update(42)
  94. fs[i].Sequence = timeutil.StrictlyMonotonicNanos()
  95. }
  96. if err := db.Update(folderID, protocol.DeviceID{42}, fs); err != nil {
  97. b.Fatal(err)
  98. }
  99. }
  100. b.ReportMetric(float64(b.N)*100.0/b.Elapsed().Seconds(), "files/s")
  101. })
  102. b.Run(fmt.Sprintf("GetGlobal100@%d", size), func(b *testing.B) {
  103. for range b.N {
  104. for i := range fs {
  105. _, ok, err := db.GetGlobalFile(folderID, fs[i].Name)
  106. if err != nil {
  107. b.Fatal(err)
  108. }
  109. if !ok {
  110. b.Fatal("should exist")
  111. }
  112. }
  113. }
  114. b.ReportMetric(float64(b.N)*100.0/b.Elapsed().Seconds(), "files/s")
  115. })
  116. b.Run(fmt.Sprintf("LocalSequenced@%d", size), func(b *testing.B) {
  117. count := 0
  118. for range b.N {
  119. cur, err := db.GetDeviceSequence(folderID, protocol.LocalDeviceID)
  120. if err != nil {
  121. b.Fatal(err)
  122. }
  123. it, errFn := db.AllLocalFilesBySequence(folderID, protocol.LocalDeviceID, cur-100, 0)
  124. for f := range it {
  125. count++
  126. globalFi = f
  127. }
  128. if err := errFn(); err != nil {
  129. b.Fatal(err)
  130. }
  131. }
  132. b.ReportMetric(float64(count)/b.Elapsed().Seconds(), "files/s")
  133. })
  134. b.Run(fmt.Sprintf("GetDeviceSequenceLoc@%d", size), func(b *testing.B) {
  135. for range b.N {
  136. _, err := db.GetDeviceSequence(folderID, protocol.LocalDeviceID)
  137. if err != nil {
  138. b.Fatal(err)
  139. }
  140. }
  141. })
  142. b.Run(fmt.Sprintf("GetDeviceSequenceRem@%d", size), func(b *testing.B) {
  143. for range b.N {
  144. _, err := db.GetDeviceSequence(folderID, protocol.DeviceID{42})
  145. if err != nil {
  146. b.Fatal(err)
  147. }
  148. }
  149. })
  150. b.Run(fmt.Sprintf("RemoteNeed@%d", size), func(b *testing.B) {
  151. count := 0
  152. for range b.N {
  153. it, errFn := db.AllNeededGlobalFiles(folderID, protocol.DeviceID{42}, config.PullOrderAlphabetic, 0, 0)
  154. for f := range it {
  155. count++
  156. globalFi = f
  157. }
  158. if err := errFn(); err != nil {
  159. b.Fatal(err)
  160. }
  161. }
  162. b.ReportMetric(float64(count)/b.Elapsed().Seconds(), "files/s")
  163. })
  164. b.Run(fmt.Sprintf("LocalNeed100Largest@%d", size), func(b *testing.B) {
  165. count := 0
  166. for range b.N {
  167. it, errFn := db.AllNeededGlobalFiles(folderID, protocol.LocalDeviceID, config.PullOrderLargestFirst, 100, 0)
  168. for f := range it {
  169. globalFi = f
  170. count++
  171. }
  172. if err := errFn(); err != nil {
  173. b.Fatal(err)
  174. }
  175. }
  176. b.ReportMetric(float64(count)/b.Elapsed().Seconds(), "files/s")
  177. })
  178. size <<= 1
  179. }
  180. }
  181. func TestBenchmarkDropAllRemote(t *testing.T) {
  182. if testing.Short() {
  183. t.Skip("slow test")
  184. }
  185. db, err := OpenTemp()
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. t.Cleanup(func() {
  190. if err := db.Close(); err != nil {
  191. t.Fatal(err)
  192. }
  193. })
  194. fs := make([]protocol.FileInfo, 1000)
  195. seq := 0
  196. for {
  197. local, err := db.CountLocal(folderID, protocol.LocalDeviceID)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. if local.Files >= 15_000 {
  202. break
  203. }
  204. for i := range fs {
  205. seq++
  206. fs[i] = genFile(rand.String(24), 64, seq)
  207. }
  208. if err := db.Update(folderID, protocol.DeviceID{42}, fs); err != nil {
  209. t.Fatal(err)
  210. }
  211. if err := db.Update(folderID, protocol.LocalDeviceID, fs); err != nil {
  212. t.Fatal(err)
  213. }
  214. }
  215. t0 := time.Now()
  216. if err := db.DropAllFiles(folderID, protocol.DeviceID{42}); err != nil {
  217. t.Fatal(err)
  218. }
  219. d := time.Since(t0)
  220. t.Log("drop all took", d)
  221. }