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