github.com/anacrolix/torrent@v1.61.0/storage/sqlite/sqlite-storage_test.go (about)

     1  //go:build cgo
     2  // +build cgo
     3  
     4  package sqliteStorage
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	_ "github.com/anacrolix/envpprof"
    13  	"github.com/anacrolix/squirrel"
    14  	"github.com/dustin/go-humanize"
    15  	qt "github.com/go-quicktest/qt"
    16  
    17  	"github.com/anacrolix/torrent/storage"
    18  	test_storage "github.com/anacrolix/torrent/storage/test"
    19  	"github.com/anacrolix/torrent/test"
    20  )
    21  
    22  func TestLeecherStorage(t *testing.T) {
    23  	test.TestLeecherStorage(t, test.LeecherStorageTestCase{
    24  		"SqliteDirect",
    25  		func(s string) storage.ClientImplCloser {
    26  			path := filepath.Join(s, "sqlite3.db")
    27  			var opts NewDirectStorageOpts
    28  			opts.Path = path
    29  			cl, err := NewDirectStorage(opts)
    30  			if err != nil {
    31  				panic(err)
    32  			}
    33  			return cl
    34  		},
    35  		0,
    36  	})
    37  }
    38  
    39  func BenchmarkMarkComplete(b *testing.B) {
    40  	const pieceSize = test_storage.DefaultPieceSize
    41  	const noTriggers = false
    42  	var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
    43  	if noTriggers {
    44  		// Since we won't push out old pieces, we have to mark them incomplete manually.
    45  		capacity = 0
    46  	}
    47  	runBench := func(b *testing.B, ci storage.ClientImpl) {
    48  		test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
    49  	}
    50  	b.Run("CustomDirect", func(b *testing.B) {
    51  		var opts squirrel.NewCacheOpts
    52  		opts.Capacity = capacity
    53  		opts.NoTriggers = noTriggers
    54  		benchOpts := func(b *testing.B) {
    55  			opts.Path = filepath.Join(b.TempDir(), "storage.db")
    56  			ci, err := NewDirectStorage(opts)
    57  			qt.Assert(b, qt.IsNil(err))
    58  			defer ci.Close()
    59  			runBench(b, ci)
    60  		}
    61  		b.Run("Default", benchOpts)
    62  	})
    63  	for _, memory := range []bool{false, true} {
    64  		b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
    65  			b.Run("Direct", func(b *testing.B) {
    66  				var opts NewDirectStorageOpts
    67  				opts.Memory = memory
    68  				opts.Capacity = capacity
    69  				opts.NoTriggers = noTriggers
    70  				directBench := func(b *testing.B) {
    71  					opts.Path = filepath.Join(b.TempDir(), "storage.db")
    72  					ci, err := NewDirectStorage(opts)
    73  					var ujm squirrel.ErrUnexpectedJournalMode
    74  					if errors.As(err, &ujm) {
    75  						b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
    76  					}
    77  					qt.Assert(b, qt.IsNil(err))
    78  					defer ci.Close()
    79  					runBench(b, ci)
    80  				}
    81  				for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
    82  					opts.SetJournalMode = journalMode
    83  					b.Run("JournalMode="+journalMode, func(b *testing.B) {
    84  						for _, mmapSize := range []int64{-1} {
    85  							if memory && mmapSize >= 0 {
    86  								continue
    87  							}
    88  							b.Run(fmt.Sprintf("MmapSize=%s", func() string {
    89  								if mmapSize < 0 {
    90  									return "default"
    91  								} else {
    92  									return humanize.IBytes(uint64(mmapSize))
    93  								}
    94  							}()), func(b *testing.B) {
    95  								opts.MmapSize = mmapSize
    96  								opts.MmapSizeOk = true
    97  								directBench(b)
    98  							})
    99  						}
   100  					})
   101  				}
   102  			})
   103  		})
   104  	}
   105  }