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