]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
Merge branch 'sqlite-direct'
[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         for _, memory := range []bool{false, true} {
87                 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
88                         b.Run("Direct", func(b *testing.B) {
89                                 var opts NewDirectStorageOpts
90                                 opts.Memory = memory
91                                 opts.Path = filepath.Join(b.TempDir(), "storage.db")
92                                 opts.Capacity = capacity
93                                 opts.CacheBlobs = true
94                                 //opts.GcBlobs = true
95                                 opts.BlobFlushInterval = time.Second
96                                 opts.NoTriggers = noTriggers
97                                 directBench := func(b *testing.B) {
98                                         ci, err := NewDirectStorage(opts)
99                                         if errors.Is(err, UnexpectedJournalMode) {
100                                                 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
101                                         }
102                                         c.Assert(err, qt.IsNil)
103                                         defer ci.Close()
104                                         runBench(b, ci)
105                                 }
106                                 for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
107                                         opts.SetJournalMode = journalMode
108                                         b.Run("JournalMode="+journalMode, func(b *testing.B) {
109                                                 for _, mmapSize := range []int64{-1, 0, 1 << 23, 1 << 24, 1 << 25} {
110                                                         if memory && mmapSize >= 0 {
111                                                                 continue
112                                                         }
113                                                         b.Run(fmt.Sprintf("MmapSize=%s", func() string {
114                                                                 if mmapSize < 0 {
115                                                                         return "default"
116                                                                 } else {
117                                                                         return humanize.IBytes(uint64(mmapSize))
118                                                                 }
119                                                         }()), func(b *testing.B) {
120                                                                 opts.MmapSize = mmapSize
121                                                                 opts.MmapSizeOk = true
122                                                                 directBench(b)
123                                                         })
124                                                 }
125                                         })
126                                 }
127                         })
128                         b.Run("ResourcePieces", func(b *testing.B) {
129                                 for _, batchWrites := range []bool{false, true} {
130                                         b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
131                                                 var opts NewPiecesStorageOpts
132                                                 opts.Path = filepath.Join(b.TempDir(), "storage.db")
133                                                 //b.Logf("storage db path: %q", dbPath)
134                                                 opts.Capacity = capacity
135                                                 opts.Memory = memory
136                                                 opts.ProvOpts = func(opts *ProviderOpts) {
137                                                         opts.BatchWrites = batchWrites
138                                                 }
139                                                 ci, err := NewPiecesStorage(opts)
140                                                 c.Assert(err, qt.IsNil)
141                                                 defer ci.Close()
142                                                 runBench(b, ci)
143                                         })
144                                 }
145                         })
146                 })
147         }
148 }