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