]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
390c68f47e18ec80c346ff9655819a8559bd89a4
[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/anacrolix/torrent/test"
18         "github.com/dustin/go-humanize"
19         qt "github.com/frankban/quicktest"
20 )
21
22 func TestLeecherStorage(t *testing.T) {
23         test.TestLeecherStorage(t, test.LeecherStorageTestCase{
24                 "SqliteDirect",
25                 func(s string) storage.ClientImplCloser {
26                         path := filepath.Join(s, "sqlite3.db")
27                         var opts NewDirectStorageOpts
28                         opts.Path = path
29                         cl, err := NewDirectStorage(opts)
30                         if err != nil {
31                                 panic(err)
32                         }
33                         return cl
34                 },
35                 0,
36         })
37 }
38
39 func BenchmarkMarkComplete(b *testing.B) {
40         const pieceSize = test_storage.DefaultPieceSize
41         const noTriggers = false
42         const noCacheBlobs = false
43         var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
44         if noTriggers {
45                 // Since we won't push out old pieces, we have to mark them incomplete manually.
46                 capacity = 0
47         }
48         runBench := func(b *testing.B, ci storage.ClientImpl) {
49                 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
50         }
51         c := qt.New(b)
52         b.Run("CustomDirect", func(b *testing.B) {
53                 var opts squirrel.NewCacheOpts
54                 opts.Capacity = capacity
55                 opts.NoTriggers = noTriggers
56                 opts.NoCacheBlobs = noCacheBlobs
57                 benchOpts := func(b *testing.B) {
58                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
59                         ci, err := NewDirectStorage(opts)
60                         c.Assert(err, qt.IsNil)
61                         defer ci.Close()
62                         runBench(b, ci)
63                 }
64                 b.Run("Default", benchOpts)
65         })
66         for _, memory := range []bool{false, true} {
67                 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
68                         b.Run("Direct", func(b *testing.B) {
69                                 var opts NewDirectStorageOpts
70                                 opts.Memory = memory
71                                 opts.Capacity = capacity
72                                 // opts.GcBlobs = true
73                                 opts.BlobFlushInterval = time.Second
74                                 opts.NoTriggers = noTriggers
75                                 opts.NoCacheBlobs = noCacheBlobs
76                                 directBench := func(b *testing.B) {
77                                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
78                                         ci, err := NewDirectStorage(opts)
79                                         var ujm squirrel.ErrUnexpectedJournalMode
80                                         if errors.As(err, &ujm) {
81                                                 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
82                                         }
83                                         c.Assert(err, qt.IsNil)
84                                         defer ci.Close()
85                                         runBench(b, ci)
86                                 }
87                                 for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
88                                         opts.SetJournalMode = journalMode
89                                         b.Run("JournalMode="+journalMode, func(b *testing.B) {
90                                                 for _, mmapSize := range []int64{-1} {
91                                                         if memory && mmapSize >= 0 {
92                                                                 continue
93                                                         }
94                                                         b.Run(fmt.Sprintf("MmapSize=%s", func() string {
95                                                                 if mmapSize < 0 {
96                                                                         return "default"
97                                                                 } else {
98                                                                         return humanize.IBytes(uint64(mmapSize))
99                                                                 }
100                                                         }()), func(b *testing.B) {
101                                                                 opts.MmapSize = mmapSize
102                                                                 opts.MmapSizeOk = true
103                                                                 directBench(b)
104                                                         })
105                                                 }
106                                         })
107                                 }
108                         })
109                 })
110         }
111 }