]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
Benchmark different mmap sizes and journal modes
[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
13         _ "github.com/anacrolix/envpprof"
14         "github.com/anacrolix/torrent/storage"
15         test_storage "github.com/anacrolix/torrent/storage/test"
16         "github.com/dustin/go-humanize"
17         qt "github.com/frankban/quicktest"
18         "github.com/stretchr/testify/assert"
19         "github.com/stretchr/testify/require"
20 )
21
22 func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
23         opts.Path = filepath.Join(t.TempDir(), "sqlite3.db")
24         pool, err := NewPool(opts)
25         qt.Assert(t, err, qt.IsNil)
26         // sqlitex.Pool.Close doesn't like being called more than once. Let it slide for now.
27         //t.Cleanup(func() { pool.Close() })
28         qt.Assert(t, initPoolDatabase(pool, InitDbOpts{}), qt.IsNil)
29         prov, err := NewProvider(pool, ProviderOpts{BatchWrites: pool.NumConns() > 1})
30         require.NoError(t, err)
31         t.Cleanup(func() { prov.Close() })
32         return pool, prov
33 }
34
35 func TestTextBlobSize(t *testing.T) {
36         _, prov := newConnsAndProv(t, NewPoolOpts{})
37         a, _ := prov.NewInstance("a")
38         err := a.Put(bytes.NewBufferString("\x00hello"))
39         qt.Assert(t, err, qt.IsNil)
40         fi, err := a.Stat()
41         qt.Assert(t, err, qt.IsNil)
42         assert.EqualValues(t, 6, fi.Size())
43 }
44
45 func TestSimultaneousIncrementalBlob(t *testing.T) {
46         _, p := newConnsAndProv(t, NewPoolOpts{
47                 NumConns: 3,
48         })
49         a, err := p.NewInstance("a")
50         require.NoError(t, err)
51         const contents = "hello, world"
52         require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
53         rc1, err := a.Get()
54         require.NoError(t, err)
55         rc2, err := a.Get()
56         require.NoError(t, err)
57         var b1, b2 []byte
58         var e1, e2 error
59         var wg sync.WaitGroup
60         doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
61                 defer wg.Done()
62                 defer rc.Close()
63                 *b, *e = ioutil.ReadAll(rc)
64                 require.NoError(t, *e, n)
65                 assert.EqualValues(t, contents, *b)
66         }
67         wg.Add(2)
68         go doRead(&b2, &e2, rc2, 2)
69         go doRead(&b1, &e1, rc1, 1)
70         wg.Wait()
71 }
72
73 func BenchmarkMarkComplete(b *testing.B) {
74         const pieceSize = test_storage.DefaultPieceSize
75         const capacity = test_storage.DefaultNumPieces * pieceSize / 2
76         runBench := func(b *testing.B, ci storage.ClientImpl) {
77                 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
78         }
79         c := qt.New(b)
80         for _, memory := range []bool{false, true} {
81                 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
82                         b.Run("Direct", func(b *testing.B) {
83                                 var opts NewDirectStorageOpts
84                                 opts.Memory = memory
85                                 opts.Path = filepath.Join(b.TempDir(), "storage.db")
86                                 opts.Capacity = capacity
87                                 directBench := func(b *testing.B) {
88                                         ci, err := NewDirectStorage(opts)
89                                         if errors.Is(err, UnexpectedJournalMode) {
90                                                 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
91                                         }
92                                         c.Assert(err, qt.IsNil)
93                                         defer ci.Close()
94                                         runBench(b, ci)
95                                 }
96                                 for _, journalMode := range []string{"", "wal", "off", "delete", "memory"} {
97                                         opts.SetJournalMode = journalMode
98                                         b.Run("JournalMode="+journalMode, func(b *testing.B) {
99                                                 for _, mmapSize := range []int64{-1, 0, 1 << 24, 1 << 25, 1 << 26} {
100                                                         if memory && mmapSize >= 0 {
101                                                                 continue
102                                                         }
103                                                         b.Run(fmt.Sprintf("MmapSize=%s", func() string {
104                                                                 if mmapSize < 0 {
105                                                                         return "default"
106                                                                 } else {
107                                                                         return humanize.IBytes(uint64(mmapSize))
108                                                                 }
109                                                         }()), func(b *testing.B) {
110                                                                 opts.MmapSize = mmapSize
111                                                                 opts.MmapSizeOk = true
112                                                                 directBench(b)
113                                                         })
114                                                 }
115                                         })
116                                 }
117                         })
118                         b.Run("ResourcePieces", func(b *testing.B) {
119                                 for _, batchWrites := range []bool{false, true} {
120                                         b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
121                                                 var opts NewPiecesStorageOpts
122                                                 opts.Path = filepath.Join(b.TempDir(), "storage.db")
123                                                 //b.Logf("storage db path: %q", dbPath)
124                                                 opts.Capacity = capacity
125                                                 opts.Memory = memory
126                                                 opts.ProvOpts = func(opts *ProviderOpts) {
127                                                         opts.BatchWrites = batchWrites
128                                                 }
129                                                 ci, err := NewPiecesStorage(opts)
130                                                 c.Assert(err, qt.IsNil)
131                                                 defer ci.Close()
132                                                 runBench(b, ci)
133                                         })
134                                 }
135                         })
136                 })
137         }
138 }