]> Sergey Matveev's repositories - btrtrc.git/blobdiff - storage/sqlite/sqlite-storage_test.go
gorond test files
[btrtrc.git] / storage / sqlite / sqlite-storage_test.go
index 6945069d06b8a9ef106c7f717dea1af47fe0aa3b..d2f16af7ee0449911b3cb0ea855243d64244f14c 100644 (file)
+//go:build cgo
+// +build cgo
+
 package sqliteStorage
 
 import (
-       "bytes"
+       "errors"
        "fmt"
-       "io"
-       "io/ioutil"
        "path/filepath"
-       "sync"
        "testing"
+       "time"
 
-       "crawshaw.io/sqlite/sqlitex"
        _ "github.com/anacrolix/envpprof"
-       "github.com/stretchr/testify/assert"
-       "github.com/stretchr/testify/require"
+       "github.com/anacrolix/squirrel"
+       "github.com/dustin/go-humanize"
+       qt "github.com/frankban/quicktest"
+
+       "github.com/anacrolix/torrent/storage"
+       test_storage "github.com/anacrolix/torrent/storage/test"
+       "github.com/anacrolix/torrent/test"
 )
 
-func TestSimultaneousIncrementalBlob(t *testing.T) {
-       pool, err := sqlitex.Open(
-               // We don't do this in memory, because it seems to have some locking issues with updating
-               // last_used.
-               fmt.Sprintf("file:%s", filepath.Join(t.TempDir(), "sqlite3.db")),
+func TestLeecherStorage(t *testing.T) {
+       test.TestLeecherStorage(t, test.LeecherStorageTestCase{
+               "SqliteDirect",
+               func(s string) storage.ClientImplCloser {
+                       path := filepath.Join(s, "sqlite3.db")
+                       var opts NewDirectStorageOpts
+                       opts.Path = path
+                       cl, err := NewDirectStorage(opts)
+                       if err != nil {
+                               panic(err)
+                       }
+                       return cl
+               },
                0,
-               10)
-       require.NoError(t, err)
-       defer pool.Close()
-       p, err := NewProviderPool(pool, 10)
-       require.NoError(t, err)
-       a, err := p.NewInstance("a")
-       require.NoError(t, err)
-       const contents = "hello, world"
-       require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
-       rc1, err := a.Get()
-       require.NoError(t, err)
-       rc2, err := a.Get()
-       require.NoError(t, err)
-       var b1, b2 []byte
-       var e1, e2 error
-       var wg sync.WaitGroup
-       doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
-               defer wg.Done()
-               defer rc.Close()
-               *b, *e = ioutil.ReadAll(rc)
-               require.NoError(t, *e, n)
-               assert.EqualValues(t, contents, *b)
+       })
+}
+
+func BenchmarkMarkComplete(b *testing.B) {
+       const pieceSize = test_storage.DefaultPieceSize
+       const noTriggers = false
+       const noCacheBlobs = false
+       var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
+       if noTriggers {
+               // Since we won't push out old pieces, we have to mark them incomplete manually.
+               capacity = 0
+       }
+       runBench := func(b *testing.B, ci storage.ClientImpl) {
+               test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
+       }
+       c := qt.New(b)
+       b.Run("CustomDirect", func(b *testing.B) {
+               var opts squirrel.NewCacheOpts
+               opts.Capacity = capacity
+               opts.NoTriggers = noTriggers
+               opts.NoCacheBlobs = noCacheBlobs
+               benchOpts := func(b *testing.B) {
+                       opts.Path = filepath.Join(b.TempDir(), "storage.db")
+                       ci, err := NewDirectStorage(opts)
+                       c.Assert(err, qt.IsNil)
+                       defer ci.Close()
+                       runBench(b, ci)
+               }
+               b.Run("Default", benchOpts)
+       })
+       for _, memory := range []bool{false, true} {
+               b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
+                       b.Run("Direct", func(b *testing.B) {
+                               var opts NewDirectStorageOpts
+                               opts.Memory = memory
+                               opts.Capacity = capacity
+                               // opts.GcBlobs = true
+                               opts.BlobFlushInterval = time.Second
+                               opts.NoTriggers = noTriggers
+                               opts.NoCacheBlobs = noCacheBlobs
+                               directBench := func(b *testing.B) {
+                                       opts.Path = filepath.Join(b.TempDir(), "storage.db")
+                                       ci, err := NewDirectStorage(opts)
+                                       var ujm squirrel.ErrUnexpectedJournalMode
+                                       if errors.As(err, &ujm) {
+                                               b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
+                                       }
+                                       c.Assert(err, qt.IsNil)
+                                       defer ci.Close()
+                                       runBench(b, ci)
+                               }
+                               for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
+                                       opts.SetJournalMode = journalMode
+                                       b.Run("JournalMode="+journalMode, func(b *testing.B) {
+                                               for _, mmapSize := range []int64{-1} {
+                                                       if memory && mmapSize >= 0 {
+                                                               continue
+                                                       }
+                                                       b.Run(fmt.Sprintf("MmapSize=%s", func() string {
+                                                               if mmapSize < 0 {
+                                                                       return "default"
+                                                               } else {
+                                                                       return humanize.IBytes(uint64(mmapSize))
+                                                               }
+                                                       }()), func(b *testing.B) {
+                                                               opts.MmapSize = mmapSize
+                                                               opts.MmapSizeOk = true
+                                                               directBench(b)
+                                                       })
+                                               }
+                                       })
+                               }
+                       })
+               })
        }
-       wg.Add(2)
-       go doRead(&b2, &e2, rc2, 2)
-       go doRead(&b1, &e1, rc1, 1)
-       wg.Wait()
 }