]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
Add batch writes cases to sqlite storage benchmarks
[btrtrc.git] / storage / sqlite / sqlite-storage_test.go
1 package sqliteStorage
2
3 import (
4         "bytes"
5         "fmt"
6         "io"
7         "io/ioutil"
8         "path/filepath"
9         "sync"
10         "testing"
11
12         _ "github.com/anacrolix/envpprof"
13         test_storage "github.com/anacrolix/torrent/storage/test"
14         qt "github.com/frankban/quicktest"
15         "github.com/stretchr/testify/assert"
16         "github.com/stretchr/testify/require"
17 )
18
19 func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
20         opts.Path = filepath.Join(t.TempDir(), "sqlite3.db")
21         conns, provOpts, err := NewPool(opts)
22         require.NoError(t, err)
23         // sqlitex.Pool.Close doesn't like being called more than once. Let it slide for now.
24         //t.Cleanup(func() { conns.Close() })
25         prov, err := NewProvider(conns, provOpts)
26         require.NoError(t, err)
27         t.Cleanup(func() { prov.Close() })
28         return conns, prov
29 }
30
31 func TestTextBlobSize(t *testing.T) {
32         _, prov := newConnsAndProv(t, NewPoolOpts{})
33         a, _ := prov.NewInstance("a")
34         err := a.Put(bytes.NewBufferString("\x00hello"))
35         qt.Assert(t, err, qt.IsNil)
36         fi, err := a.Stat()
37         qt.Assert(t, err, qt.IsNil)
38         assert.EqualValues(t, 6, fi.Size())
39 }
40
41 func TestSimultaneousIncrementalBlob(t *testing.T) {
42         _, p := newConnsAndProv(t, NewPoolOpts{
43                 NumConns: 3,
44         })
45         a, err := p.NewInstance("a")
46         require.NoError(t, err)
47         const contents = "hello, world"
48         require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
49         rc1, err := a.Get()
50         require.NoError(t, err)
51         rc2, err := a.Get()
52         require.NoError(t, err)
53         var b1, b2 []byte
54         var e1, e2 error
55         var wg sync.WaitGroup
56         doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
57                 defer wg.Done()
58                 defer rc.Close()
59                 *b, *e = ioutil.ReadAll(rc)
60                 require.NoError(t, *e, n)
61                 assert.EqualValues(t, contents, *b)
62         }
63         wg.Add(2)
64         go doRead(&b2, &e2, rc2, 2)
65         go doRead(&b1, &e1, rc1, 1)
66         wg.Wait()
67 }
68
69 func BenchmarkMarkComplete(b *testing.B) {
70         const pieceSize = 2 << 20
71         const capacity = 0
72         c := qt.New(b)
73         for _, memory := range []bool{false, true} {
74                 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
75                         for _, batchWrites := range []bool{false, true} {
76                                 b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
77                                         dbPath := filepath.Join(b.TempDir(), "storage.db")
78                                         //b.Logf("storage db path: %q", dbPath)
79                                         ci, err := NewPiecesStorage(NewPiecesStorageOpts{
80                                                 NewPoolOpts: NewPoolOpts{
81                                                         Path:                  dbPath,
82                                                         Capacity:              4*pieceSize - 1,
83                                                         NoConcurrentBlobReads: false,
84                                                         PageSize:              1 << 14,
85                                                         Memory:                memory,
86                                                 },
87                                                 ProvOpts: func(opts *ProviderOpts) {
88                                                         opts.BatchWrites = batchWrites
89                                                 },
90                                         })
91                                         c.Assert(err, qt.IsNil)
92                                         defer ci.Close()
93                                         test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, 16, capacity)
94                                 })
95                         }
96                 })
97         }
98 }