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