11 "github.com/anacrolix/torrent/metainfo"
12 "github.com/anacrolix/torrent/storage"
13 "github.com/bradfitz/iter"
14 qt "github.com/frankban/quicktest"
19 DefaultPieceSize = 2 << 20
24 func BenchmarkPieceMarkComplete(
25 b *testing.B, ci storage.ClientImpl,
26 pieceSize int64, numPieces int, capacity int64,
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)
39 rand.Read(info.Pieces)
40 data := make([]byte, pieceSize)
41 b.SetBytes(int64(numPieces) * pieceSize)
43 for pieceIndex := range iter.N(numPieces) {
44 pi := ti.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{true, true})
71 readData, err := ioutil.ReadAll(io.NewSectionReader(pi, 0, int64(len(data))))
72 c.Assert(err, qt.IsNil)
73 c.Assert(len(readData), qt.Equals, len(data))
74 c.Assert(bytes.Equal(readData, data), qt.IsTrue)
80 for range iter.N(int((capacity + info.TotalLength() - 1) / info.TotalLength())) {
85 for range iter.N(b.N) {