]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
a566322d708b5d78075e983684f58d4f3d698ae1
[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
12         _ "github.com/anacrolix/envpprof"
13         "github.com/anacrolix/squirrel"
14         "github.com/dustin/go-humanize"
15         qt "github.com/frankban/quicktest"
16
17         "github.com/anacrolix/torrent/storage"
18         test_storage "github.com/anacrolix/torrent/storage/test"
19         "github.com/anacrolix/torrent/test"
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         var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
43         if noTriggers {
44                 // Since we won't push out old pieces, we have to mark them incomplete manually.
45                 capacity = 0
46         }
47         runBench := func(b *testing.B, ci storage.ClientImpl) {
48                 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
49         }
50         c := qt.New(b)
51         b.Run("CustomDirect", func(b *testing.B) {
52                 var opts squirrel.NewCacheOpts
53                 opts.Capacity = capacity
54                 opts.NoTriggers = noTriggers
55                 benchOpts := func(b *testing.B) {
56                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
57                         ci, err := NewDirectStorage(opts)
58                         c.Assert(err, qt.IsNil)
59                         defer ci.Close()
60                         runBench(b, ci)
61                 }
62                 b.Run("Default", benchOpts)
63         })
64         for _, memory := range []bool{false, true} {
65                 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
66                         b.Run("Direct", func(b *testing.B) {
67                                 var opts NewDirectStorageOpts
68                                 opts.Memory = memory
69                                 opts.Capacity = capacity
70                                 opts.NoTriggers = noTriggers
71                                 directBench := func(b *testing.B) {
72                                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
73                                         ci, err := NewDirectStorage(opts)
74                                         var ujm squirrel.ErrUnexpectedJournalMode
75                                         if errors.As(err, &ujm) {
76                                                 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
77                                         }
78                                         c.Assert(err, qt.IsNil)
79                                         defer ci.Close()
80                                         runBench(b, ci)
81                                 }
82                                 for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
83                                         opts.SetJournalMode = journalMode
84                                         b.Run("JournalMode="+journalMode, func(b *testing.B) {
85                                                 for _, mmapSize := range []int64{-1} {
86                                                         if memory && mmapSize >= 0 {
87                                                                 continue
88                                                         }
89                                                         b.Run(fmt.Sprintf("MmapSize=%s", func() string {
90                                                                 if mmapSize < 0 {
91                                                                         return "default"
92                                                                 } else {
93                                                                         return humanize.IBytes(uint64(mmapSize))
94                                                                 }
95                                                         }()), func(b *testing.B) {
96                                                                 opts.MmapSize = mmapSize
97                                                                 opts.MmapSizeOk = true
98                                                                 directBench(b)
99                                                         })
100                                                 }
101                                         })
102                                 }
103                         })
104                 })
105         }
106 }