12 _ "github.com/anacrolix/envpprof"
13 "github.com/anacrolix/torrent/storage"
14 test_storage "github.com/anacrolix/torrent/storage/test"
15 qt "github.com/frankban/quicktest"
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
20 func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
21 opts.Path = filepath.Join(t.TempDir(), "sqlite3.db")
22 conns, provOpts, err := NewPool(opts)
23 require.NoError(t, err)
24 // sqlitex.Pool.Close doesn't like being called more than once. Let it slide for now.
25 //t.Cleanup(func() { conns.Close() })
26 prov, err := NewProvider(conns, provOpts)
27 require.NoError(t, err)
28 t.Cleanup(func() { prov.Close() })
32 func TestTextBlobSize(t *testing.T) {
33 _, prov := newConnsAndProv(t, NewPoolOpts{})
34 a, _ := prov.NewInstance("a")
35 err := a.Put(bytes.NewBufferString("\x00hello"))
36 qt.Assert(t, err, qt.IsNil)
38 qt.Assert(t, err, qt.IsNil)
39 assert.EqualValues(t, 6, fi.Size())
42 func TestSimultaneousIncrementalBlob(t *testing.T) {
43 _, p := newConnsAndProv(t, NewPoolOpts{
46 a, err := p.NewInstance("a")
47 require.NoError(t, err)
48 const contents = "hello, world"
49 require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
51 require.NoError(t, err)
53 require.NoError(t, err)
57 doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
60 *b, *e = ioutil.ReadAll(rc)
61 require.NoError(t, *e, n)
62 assert.EqualValues(t, contents, *b)
65 go doRead(&b2, &e2, rc2, 2)
66 go doRead(&b1, &e1, rc1, 1)
70 func BenchmarkMarkComplete(b *testing.B) {
71 const pieceSize = 2 << 20
74 for _, memory := range []bool{false, true} {
75 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
76 dbPath := filepath.Join(b.TempDir(), "storage.db")
77 //b.Logf("storage db path: %q", dbPath)
78 ci, err := NewPiecesStorage(NewPiecesStorageOpts{
79 NewPoolOpts: NewPoolOpts{
81 Capacity: 4*pieceSize - 1,
82 NoConcurrentBlobReads: false,
86 ProvOpts: func(opts *ProviderOpts) {
87 opts.BatchWrites = true
89 ResourcePiecesOpts: storage.ResourcePiecesOpts{
90 NoSizedPuts: false || memory,
93 c.Assert(err, qt.IsNil)
95 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, 16, capacity)