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"
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() })
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)
37 qt.Assert(t, err, qt.IsNil)
38 assert.EqualValues(t, 6, fi.Size())
41 func TestSimultaneousIncrementalBlob(t *testing.T) {
42 _, p := newConnsAndProv(t, NewPoolOpts{
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"))))
50 require.NoError(t, err)
52 require.NoError(t, err)
56 doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
59 *b, *e = ioutil.ReadAll(rc)
60 require.NoError(t, *e, n)
61 assert.EqualValues(t, contents, *b)
64 go doRead(&b2, &e2, rc2, 2)
65 go doRead(&b1, &e1, rc1, 1)
69 func BenchmarkMarkComplete(b *testing.B) {
70 const pieceSize = 2 << 20
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{
82 Capacity: 4*pieceSize - 1,
83 NoConcurrentBlobReads: false,
87 ProvOpts: func(opts *ProviderOpts) {
88 opts.BatchWrites = batchWrites
91 c.Assert(err, qt.IsNil)
93 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, 16, capacity)