17 _ "github.com/anacrolix/envpprof"
18 "github.com/dustin/go-humanize"
19 qt "github.com/frankban/quicktest"
20 "github.com/stretchr/testify/assert"
21 "github.com/stretchr/testify/require"
23 "github.com/anacrolix/torrent/storage"
24 test_storage "github.com/anacrolix/torrent/storage/test"
27 func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
28 opts.Path = filepath.Join(t.TempDir(), "sqlite3.db")
29 pool, err := NewPool(opts)
30 qt.Assert(t, err, qt.IsNil)
31 // sqlitex.Pool.Close doesn't like being called more than once. Let it slide for now.
32 //t.Cleanup(func() { pool.Close() })
33 qt.Assert(t, initPoolDatabase(pool, InitDbOpts{}), qt.IsNil)
34 if !opts.Memory && opts.SetJournalMode == "" {
35 opts.SetJournalMode = "wal"
37 qt.Assert(t, initPoolConns(nil, pool, opts.InitConnOpts), qt.IsNil)
38 prov, err := NewProvider(pool, ProviderOpts{BatchWrites: pool.NumConns() > 1})
39 require.NoError(t, err)
40 t.Cleanup(func() { prov.Close() })
44 func TestTextBlobSize(t *testing.T) {
45 _, prov := newConnsAndProv(t, NewPoolOpts{})
46 a, _ := prov.NewInstance("a")
47 err := a.Put(bytes.NewBufferString("\x00hello"))
48 qt.Assert(t, err, qt.IsNil)
50 qt.Assert(t, err, qt.IsNil)
51 assert.EqualValues(t, 6, fi.Size())
54 func TestSimultaneousIncrementalBlob(t *testing.T) {
55 _, p := newConnsAndProv(t, NewPoolOpts{
58 a, err := p.NewInstance("a")
59 require.NoError(t, err)
60 const contents = "hello, world"
61 require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
63 require.NoError(t, err)
65 require.NoError(t, err)
69 doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
72 *b, *e = ioutil.ReadAll(rc)
73 require.NoError(t, *e, n)
74 assert.EqualValues(t, contents, *b)
77 go doRead(&b2, &e2, rc2, 2)
78 go doRead(&b1, &e1, rc1, 1)
82 func BenchmarkMarkComplete(b *testing.B) {
83 const pieceSize = test_storage.DefaultPieceSize
84 const noTriggers = false
85 var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
87 // Since we won't push out old pieces, we have to mark them incomplete manually.
90 runBench := func(b *testing.B, ci storage.ClientImpl) {
91 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
94 b.Run("CustomDirect", func(b *testing.B) {
95 var opts NewDirectStorageOpts
96 opts.Capacity = capacity
97 opts.NoTriggers = noTriggers
98 benchOpts := func(b *testing.B) {
99 opts.Path = filepath.Join(b.TempDir(), "storage.db")
100 ci, err := NewDirectStorage(opts)
101 c.Assert(err, qt.IsNil)
105 b.Run("Default", benchOpts)
107 for _, memory := range []bool{false, true} {
108 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
109 b.Run("Direct", func(b *testing.B) {
110 var opts NewDirectStorageOpts
112 opts.Capacity = capacity
113 //opts.GcBlobs = true
114 opts.BlobFlushInterval = time.Second
115 opts.NoTriggers = noTriggers
116 directBench := func(b *testing.B) {
117 opts.Path = filepath.Join(b.TempDir(), "storage.db")
118 ci, err := NewDirectStorage(opts)
119 var ujm UnexpectedJournalMode
120 if errors.As(err, &ujm) {
121 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
123 c.Assert(err, qt.IsNil)
127 for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
128 opts.SetJournalMode = journalMode
129 b.Run("JournalMode="+journalMode, func(b *testing.B) {
130 for _, mmapSize := range []int64{-1} {
131 if memory && mmapSize >= 0 {
134 b.Run(fmt.Sprintf("MmapSize=%s", func() string {
138 return humanize.IBytes(uint64(mmapSize))
140 }()), func(b *testing.B) {
141 opts.MmapSize = mmapSize
142 opts.MmapSizeOk = true
149 b.Run("ResourcePieces", func(b *testing.B) {
150 for _, batchWrites := range []bool{false, true} {
151 b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
152 var opts NewPiecesStorageOpts
153 opts.Path = filepath.Join(b.TempDir(), "storage.db")
154 //b.Logf("storage db path: %q", dbPath)
155 opts.Capacity = capacity
157 opts.ProvOpts = func(opts *ProviderOpts) {
158 opts.BatchWrites = batchWrites
160 ci, err := NewPiecesStorage(opts)
161 c.Assert(err, qt.IsNil)