9 "github.com/anacrolix/torrent/metainfo"
10 "github.com/anacrolix/torrent/storage"
11 "github.com/bradfitz/iter"
12 qt "github.com/frankban/quicktest"
17 DefaultPieceSize = 2 << 20
22 func BenchmarkPieceMarkComplete(
23 b *testing.B, ci storage.ClientImpl,
24 pieceSize int64, numPieces int,
25 // This drives any special handling around capacity that may be configured into the storage
30 info := &metainfo.Info{
31 Pieces: make([]byte, numPieces*metainfo.HashSize),
32 PieceLength: pieceSize,
33 Length: pieceSize * int64(numPieces),
36 ti, err := ci.OpenTorrent(info, metainfo.Hash{})
37 c.Assert(err, qt.IsNil)
38 tw := storage.Torrent{ti}
40 rand.Read(info.Pieces)
41 data := make([]byte, pieceSize)
42 readData := make([]byte, pieceSize)
43 b.SetBytes(int64(numPieces) * pieceSize)
45 for pieceIndex := range iter.N(numPieces) {
46 pi := tw.Piece(info.Piece(pieceIndex))
50 for off := int64(0); off < int64(len(data)); off += ChunkSize {
54 n, err := pi.WriteAt(data[off:off+ChunkSize], off)
67 // This might not apply if users of this benchmark don't cache with the expected capacity.
68 c.Assert(pi.Completion(), qt.Equals, storage.Completion{Complete: false, Ok: true})
69 c.Assert(pi.MarkComplete(), qt.IsNil)
70 c.Assert(pi.Completion(), qt.Equals, storage.Completion{true, true})
71 n, err := pi.WriteTo(bytes.NewBuffer(readData[:0]))
73 c.Assert(err, qt.IsNil)
74 c.Assert(n, qt.Equals, int64(len(data)))
75 c.Assert(bytes.Equal(readData[:n], data), qt.IsTrue)
80 for range iter.N(int((capacity + info.TotalLength() - 1) / info.TotalLength())) {
85 for range iter.N(b.N) {