11 "github.com/anacrolix/torrent/metainfo"
12 "github.com/anacrolix/torrent/storage"
13 "github.com/bradfitz/iter"
14 qt "github.com/frankban/quicktest"
17 const chunkSize = 1 << 14
19 func BenchmarkPieceMarkComplete(b *testing.B, ci storage.ClientImpl, pieceSize int64, numPieces int, capacity int64) {
21 ti, err := ci.OpenTorrent(&metainfo.Info{
22 Pieces: make([]byte, metainfo.HashSize*numPieces),
23 PieceLength: pieceSize,
25 c.Assert(err, qt.IsNil)
27 info := &metainfo.Info{
28 Pieces: make([]byte, numPieces*metainfo.HashSize),
29 PieceLength: pieceSize,
30 Length: pieceSize * int64(numPieces),
32 rand.Read(info.Pieces)
33 data := make([]byte, pieceSize)
35 for pieceIndex := range iter.N(numPieces) {
36 pi := ti.Piece(info.Piece(pieceIndex))
39 for off := int64(0); off < int64(len(data)); off += chunkSize {
43 n, err := pi.WriteAt(data[off:off+chunkSize], off)
56 // This might not apply if users of this benchmark don't cache with the expected capacity.
57 c.Assert(pi.Completion(), qt.Equals, storage.Completion{Complete: false, Ok: true})
58 c.Assert(pi.MarkComplete(), qt.IsNil)
59 c.Assert(pi.Completion(), qt.Equals, storage.Completion{true, true})
60 readData, err := ioutil.ReadAll(io.NewSectionReader(pi, 0, int64(len(data))))
61 c.Assert(err, qt.IsNil)
62 c.Assert(len(readData), qt.Equals, len(data))
63 c.Assert(bytes.Equal(readData, data), qt.IsTrue)
68 for range iter.N(int((capacity + info.TotalLength() - 1) / info.TotalLength())) {
73 for range iter.N(b.N) {