]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
Use separate squirrel module
[btrtrc.git] / storage / sqlite / sqlite-storage_test.go
1 //go:build cgo
2 // +build cgo
3
4 package sqliteStorage
5
6 import (
7         "errors"
8         "fmt"
9         "path/filepath"
10         "testing"
11         "time"
12
13         _ "github.com/anacrolix/envpprof"
14         "github.com/anacrolix/squirrel"
15         "github.com/anacrolix/torrent/storage"
16         test_storage "github.com/anacrolix/torrent/storage/test"
17         "github.com/dustin/go-humanize"
18         qt "github.com/frankban/quicktest"
19 )
20
21 func BenchmarkMarkComplete(b *testing.B) {
22         const pieceSize = test_storage.DefaultPieceSize
23         const noTriggers = false
24         var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
25         if noTriggers {
26                 // Since we won't push out old pieces, we have to mark them incomplete manually.
27                 capacity = 0
28         }
29         runBench := func(b *testing.B, ci storage.ClientImpl) {
30                 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
31         }
32         c := qt.New(b)
33         b.Run("CustomDirect", func(b *testing.B) {
34                 var opts squirrel.NewCacheOpts
35                 opts.Capacity = capacity
36                 opts.NoTriggers = noTriggers
37                 benchOpts := func(b *testing.B) {
38                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
39                         ci, err := NewDirectStorage(opts)
40                         c.Assert(err, qt.IsNil)
41                         defer ci.Close()
42                         runBench(b, ci)
43                 }
44                 b.Run("Default", benchOpts)
45         })
46         for _, memory := range []bool{false, true} {
47                 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
48                         b.Run("Direct", func(b *testing.B) {
49                                 var opts NewDirectStorageOpts
50                                 opts.Memory = memory
51                                 opts.Capacity = capacity
52                                 //opts.GcBlobs = true
53                                 opts.BlobFlushInterval = time.Second
54                                 opts.NoTriggers = noTriggers
55                                 directBench := func(b *testing.B) {
56                                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
57                                         ci, err := NewDirectStorage(opts)
58                                         var ujm squirrel.UnexpectedJournalMode
59                                         if errors.As(err, &ujm) {
60                                                 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
61                                         }
62                                         c.Assert(err, qt.IsNil)
63                                         defer ci.Close()
64                                         runBench(b, ci)
65                                 }
66                                 for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
67                                         opts.SetJournalMode = journalMode
68                                         b.Run("JournalMode="+journalMode, func(b *testing.B) {
69                                                 for _, mmapSize := range []int64{-1} {
70                                                         if memory && mmapSize >= 0 {
71                                                                 continue
72                                                         }
73                                                         b.Run(fmt.Sprintf("MmapSize=%s", func() string {
74                                                                 if mmapSize < 0 {
75                                                                         return "default"
76                                                                 } else {
77                                                                         return humanize.IBytes(uint64(mmapSize))
78                                                                 }
79                                                         }()), func(b *testing.B) {
80                                                                 opts.MmapSize = mmapSize
81                                                                 opts.MmapSizeOk = true
82                                                                 directBench(b)
83                                                         })
84                                                 }
85                                         })
86                                 }
87                         })
88                 })
89         }
90 }