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