]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
Set page size before initializing connections
[btrtrc.git] / storage / sqlite / sqlite-storage_test.go
1 package sqliteStorage
2
3 import (
4         "bytes"
5         "errors"
6         "fmt"
7         "io"
8         "io/ioutil"
9         "path/filepath"
10         "sync"
11         "testing"
12         "time"
13
14         _ "github.com/anacrolix/envpprof"
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         "github.com/stretchr/testify/assert"
20         "github.com/stretchr/testify/require"
21 )
22
23 func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
24         opts.Path = filepath.Join(t.TempDir(), "sqlite3.db")
25         pool, err := NewPool(opts)
26         qt.Assert(t, err, qt.IsNil)
27         // sqlitex.Pool.Close doesn't like being called more than once. Let it slide for now.
28         //t.Cleanup(func() { pool.Close() })
29         qt.Assert(t, initPoolDatabase(pool, InitDbOpts{}), qt.IsNil)
30         prov, err := NewProvider(pool, ProviderOpts{BatchWrites: pool.NumConns() > 1})
31         require.NoError(t, err)
32         t.Cleanup(func() { prov.Close() })
33         return pool, prov
34 }
35
36 func TestTextBlobSize(t *testing.T) {
37         _, prov := newConnsAndProv(t, NewPoolOpts{})
38         a, _ := prov.NewInstance("a")
39         err := a.Put(bytes.NewBufferString("\x00hello"))
40         qt.Assert(t, err, qt.IsNil)
41         fi, err := a.Stat()
42         qt.Assert(t, err, qt.IsNil)
43         assert.EqualValues(t, 6, fi.Size())
44 }
45
46 func TestSimultaneousIncrementalBlob(t *testing.T) {
47         _, p := newConnsAndProv(t, NewPoolOpts{
48                 NumConns: 3,
49         })
50         a, err := p.NewInstance("a")
51         require.NoError(t, err)
52         const contents = "hello, world"
53         require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
54         rc1, err := a.Get()
55         require.NoError(t, err)
56         rc2, err := a.Get()
57         require.NoError(t, err)
58         var b1, b2 []byte
59         var e1, e2 error
60         var wg sync.WaitGroup
61         doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
62                 defer wg.Done()
63                 defer rc.Close()
64                 *b, *e = ioutil.ReadAll(rc)
65                 require.NoError(t, *e, n)
66                 assert.EqualValues(t, contents, *b)
67         }
68         wg.Add(2)
69         go doRead(&b2, &e2, rc2, 2)
70         go doRead(&b1, &e1, rc1, 1)
71         wg.Wait()
72 }
73
74 func BenchmarkMarkComplete(b *testing.B) {
75         const pieceSize = test_storage.DefaultPieceSize
76         const noTriggers = false
77         var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
78         if noTriggers {
79                 // Since we won't push out old pieces, we have to mark them incomplete manually.
80                 capacity = 0
81         }
82         runBench := func(b *testing.B, ci storage.ClientImpl) {
83                 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
84         }
85         c := qt.New(b)
86         b.Run("CustomDirect", func(b *testing.B) {
87                 var opts NewDirectStorageOpts
88                 opts.Capacity = capacity
89                 opts.BlobFlushInterval = time.Second
90                 opts.CacheBlobs = true
91                 opts.SetJournalMode = "off"
92                 benchOpts := func(b *testing.B) {
93                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
94                         ci, err := NewDirectStorage(opts)
95                         c.Assert(err, qt.IsNil)
96                         defer ci.Close()
97                         runBench(b, ci)
98                 }
99                 b.Run("Control", benchOpts)
100                 opts.PageSize = 1 << 16
101                 b.Run("64KiB_PageSize", benchOpts)
102         })
103         for _, memory := range []bool{false, true} {
104                 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
105                         b.Run("Direct", func(b *testing.B) {
106                                 var opts NewDirectStorageOpts
107                                 opts.Memory = memory
108                                 opts.Capacity = capacity
109                                 opts.CacheBlobs = true
110                                 //opts.GcBlobs = true
111                                 opts.BlobFlushInterval = time.Second
112                                 opts.NoTriggers = noTriggers
113                                 directBench := func(b *testing.B) {
114                                         opts.Path = filepath.Join(b.TempDir(), "storage.db")
115                                         ci, err := NewDirectStorage(opts)
116                                         var ujm UnexpectedJournalMode
117                                         if errors.As(err, &ujm) {
118                                                 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
119                                         }
120                                         c.Assert(err, qt.IsNil)
121                                         defer ci.Close()
122                                         runBench(b, ci)
123                                 }
124                                 for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
125                                         opts.SetJournalMode = journalMode
126                                         b.Run("JournalMode="+journalMode, func(b *testing.B) {
127                                                 for _, mmapSize := range []int64{-1, 0, 1 << 23, 1 << 24, 1 << 25} {
128                                                         if memory && mmapSize >= 0 {
129                                                                 continue
130                                                         }
131                                                         b.Run(fmt.Sprintf("MmapSize=%s", func() string {
132                                                                 if mmapSize < 0 {
133                                                                         return "default"
134                                                                 } else {
135                                                                         return humanize.IBytes(uint64(mmapSize))
136                                                                 }
137                                                         }()), func(b *testing.B) {
138                                                                 opts.MmapSize = mmapSize
139                                                                 opts.MmapSizeOk = true
140                                                                 directBench(b)
141                                                         })
142                                                 }
143                                         })
144                                 }
145                         })
146                         b.Run("ResourcePieces", func(b *testing.B) {
147                                 for _, batchWrites := range []bool{false, true} {
148                                         b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
149                                                 var opts NewPiecesStorageOpts
150                                                 opts.Path = filepath.Join(b.TempDir(), "storage.db")
151                                                 //b.Logf("storage db path: %q", dbPath)
152                                                 opts.Capacity = capacity
153                                                 opts.Memory = memory
154                                                 opts.ProvOpts = func(opts *ProviderOpts) {
155                                                         opts.BatchWrites = batchWrites
156                                                 }
157                                                 ci, err := NewPiecesStorage(opts)
158                                                 c.Assert(err, qt.IsNil)
159                                                 defer ci.Close()
160                                                 runBench(b, ci)
161                                         })
162                                 }
163                         })
164                 })
165         }
166 }