13 _ "github.com/anacrolix/envpprof"
14 "github.com/anacrolix/torrent/storage"
15 test_storage "github.com/anacrolix/torrent/storage/test"
16 "github.com/dustin/go-humanize"
17 qt "github.com/frankban/quicktest"
18 "github.com/stretchr/testify/assert"
19 "github.com/stretchr/testify/require"
22 func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
23 opts.Path = filepath.Join(t.TempDir(), "sqlite3.db")
24 pool, err := NewPool(opts)
25 qt.Assert(t, err, qt.IsNil)
26 // sqlitex.Pool.Close doesn't like being called more than once. Let it slide for now.
27 //t.Cleanup(func() { pool.Close() })
28 qt.Assert(t, initPoolDatabase(pool, InitDbOpts{}), qt.IsNil)
29 prov, err := NewProvider(pool, ProviderOpts{BatchWrites: pool.NumConns() > 1})
30 require.NoError(t, err)
31 t.Cleanup(func() { prov.Close() })
35 func TestTextBlobSize(t *testing.T) {
36 _, prov := newConnsAndProv(t, NewPoolOpts{})
37 a, _ := prov.NewInstance("a")
38 err := a.Put(bytes.NewBufferString("\x00hello"))
39 qt.Assert(t, err, qt.IsNil)
41 qt.Assert(t, err, qt.IsNil)
42 assert.EqualValues(t, 6, fi.Size())
45 func TestSimultaneousIncrementalBlob(t *testing.T) {
46 _, p := newConnsAndProv(t, NewPoolOpts{
49 a, err := p.NewInstance("a")
50 require.NoError(t, err)
51 const contents = "hello, world"
52 require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
54 require.NoError(t, err)
56 require.NoError(t, err)
60 doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
63 *b, *e = ioutil.ReadAll(rc)
64 require.NoError(t, *e, n)
65 assert.EqualValues(t, contents, *b)
68 go doRead(&b2, &e2, rc2, 2)
69 go doRead(&b1, &e1, rc1, 1)
73 func BenchmarkMarkComplete(b *testing.B) {
74 const pieceSize = test_storage.DefaultPieceSize
75 const capacity = test_storage.DefaultNumPieces * pieceSize / 2
76 runBench := func(b *testing.B, ci storage.ClientImpl) {
77 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
80 for _, memory := range []bool{false, true} {
81 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
82 b.Run("Direct", func(b *testing.B) {
83 var opts NewDirectStorageOpts
85 opts.Path = filepath.Join(b.TempDir(), "storage.db")
86 opts.Capacity = capacity
87 directBench := func(b *testing.B) {
88 ci, err := NewDirectStorage(opts)
89 if errors.Is(err, UnexpectedJournalMode) {
90 b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
92 c.Assert(err, qt.IsNil)
96 for _, journalMode := range []string{"", "wal", "off", "delete", "memory"} {
97 opts.SetJournalMode = journalMode
98 b.Run("JournalMode="+journalMode, func(b *testing.B) {
99 for _, mmapSize := range []int64{-1, 0, 1 << 24, 1 << 25, 1 << 26} {
100 if memory && mmapSize >= 0 {
103 b.Run(fmt.Sprintf("MmapSize=%s", func() string {
107 return humanize.IBytes(uint64(mmapSize))
109 }()), func(b *testing.B) {
110 opts.MmapSize = mmapSize
111 opts.MmapSizeOk = true
118 b.Run("ResourcePieces", func(b *testing.B) {
119 for _, batchWrites := range []bool{false, true} {
120 b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
121 var opts NewPiecesStorageOpts
122 opts.Path = filepath.Join(b.TempDir(), "storage.db")
123 //b.Logf("storage db path: %q", dbPath)
124 opts.Capacity = capacity
126 opts.ProvOpts = func(opts *ProviderOpts) {
127 opts.BatchWrites = batchWrites
129 ci, err := NewPiecesStorage(opts)
130 c.Assert(err, qt.IsNil)