9 "github.com/anacrolix/torrent/metainfo"
10 "github.com/anacrolix/torrent/storage"
11 qt "github.com/frankban/quicktest"
16 DefaultPieceSize = 2 << 20
20 func BenchmarkPieceMarkComplete(
21 b *testing.B, ci storage.ClientImpl,
22 pieceSize int64, numPieces int,
23 // This drives any special handling around capacity that may be configured into the storage
28 info := &metainfo.Info{
29 Pieces: make([]byte, numPieces*metainfo.HashSize),
30 PieceLength: pieceSize,
31 Length: pieceSize * int64(numPieces),
34 ti, err := ci.OpenTorrent(info, metainfo.Hash{})
35 c.Assert(err, qt.IsNil)
36 tw := storage.Torrent{ti}
38 rand.Read(info.Pieces)
39 data := make([]byte, pieceSize)
40 readData := make([]byte, pieceSize)
41 b.SetBytes(int64(numPieces) * pieceSize)
43 for pieceIndex := 0; pieceIndex < numPieces; pieceIndex += 1 {
44 pi := tw.Piece(info.Piece(pieceIndex))
48 for off := int64(0); off < int64(len(data)); off += ChunkSize {
52 n, err := pi.WriteAt(data[off:off+ChunkSize], off)
65 // This might not apply if users of this benchmark don't cache with the expected capacity.
66 c.Assert(pi.Completion(), qt.Equals, storage.Completion{Complete: false, Ok: true})
67 c.Assert(pi.MarkComplete(), qt.IsNil)
68 c.Assert(pi.Completion(), qt.Equals, storage.Completion{true, true})
69 n, err := pi.WriteTo(bytes.NewBuffer(readData[:0]))
71 c.Assert(err, qt.IsNil)
72 c.Assert(n, qt.Equals, int64(len(data)))
73 c.Assert(bytes.Equal(readData[:n], data), qt.IsTrue)
78 iterN := int((capacity + info.TotalLength() - 1) / info.TotalLength())
79 for i := 0; i < iterN; i += 1 {
84 for i := 0; i < b.N; i += 1 {