]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
Don't build storage/sqlite tests without cgo
[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         const noCacheBlobs = false
25         var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
26         if noTriggers {
27                 // Since we won't push out old pieces, we have to mark them incomplete manually.
28                 capacity = 0
29         }
30         runBench := func(b *testing.B, ci storage.ClientImpl) {
31                 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
32         }
33         c := qt.New(b)
34         b.Run("CustomDirect", func(b *testing.B) {
35                 var opts squirrel.NewCacheOpts
36                 opts.Capacity = capacity
37                 opts.NoTriggers = noTriggers
38                 opts.NoCacheBlobs = noCacheBlobs
39                 benchOpts := func(b *testing.B) {
40                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
41                         ci, err := NewDirectStorage(opts)
42                         c.Assert(err, qt.IsNil)
43                         defer ci.Close()
44                         runBench(b, ci)
45                 }
46                 b.Run("Default", benchOpts)
47         })
48         for _, memory := range []bool{false, true} {
49                 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
50                         b.Run("Direct", func(b *testing.B) {
51                                 var opts NewDirectStorageOpts
52                                 opts.Memory = memory
53                                 opts.Capacity = capacity
54                                 // opts.GcBlobs = true
55                                 opts.BlobFlushInterval = time.Second
56                                 opts.NoTriggers = noTriggers
57                                 opts.NoCacheBlobs = noCacheBlobs
58                                 directBench := func(b *testing.B) {
59                                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
60                                         ci, err := NewDirectStorage(opts)
61                                         var ujm squirrel.ErrUnexpectedJournalMode
62                                         if errors.As(err, &ujm) {
63                                                 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
64                                         }
65                                         c.Assert(err, qt.IsNil)
66                                         defer ci.Close()
67                                         runBench(b, ci)
68                                 }
69                                 for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
70                                         opts.SetJournalMode = journalMode
71                                         b.Run("JournalMode="+journalMode, func(b *testing.B) {
72                                                 for _, mmapSize := range []int64{-1} {
73                                                         if memory && mmapSize >= 0 {
74                                                                 continue
75                                                         }
76                                                         b.Run(fmt.Sprintf("MmapSize=%s", func() string {
77                                                                 if mmapSize < 0 {
78                                                                         return "default"
79                                                                 } else {
80                                                                         return humanize.IBytes(uint64(mmapSize))
81                                                                 }
82                                                         }()), func(b *testing.B) {
83                                                                 opts.MmapSize = mmapSize
84                                                                 opts.MmapSizeOk = true
85                                                                 directBench(b)
86                                                         })
87                                                 }
88                                         })
89                                 }
90                         })
91                 })
92         }
93 }