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,
27 // This drives any special handling around capacity that may be configured into the storage
33 info := &metainfo.Info{
34 Pieces: make([]byte, numPieces*metainfo.HashSize),
35 PieceLength: pieceSize,
36 Length: pieceSize * int64(numPieces),
39 ti, err := ci.OpenTorrent(info, metainfo.Hash{})
40 c.Assert(err, qt.IsNil)
42 rand.Read(info.Pieces)
43 data := make([]byte, pieceSize)
44 b.SetBytes(int64(numPieces) * pieceSize)
46 for pieceIndex := range iter.N(numPieces) {
47 pi := ti.Piece(info.Piece(pieceIndex))
52 for off := int64(0); off < int64(len(data)); off += ChunkSize {
56 n, err := pi.WriteAt(data[off:off+ChunkSize], off)
71 // This might not apply if users of this benchmark don't cache with the expected capacity.
72 c.Assert(pi.Completion(), qt.Equals, storage.Completion{Complete: false, Ok: true})
73 c.Assert(pi.MarkComplete(), qt.IsNil)
74 c.Assert(pi.Completion(), qt.Equals, storage.Completion{true, true})
76 readData, err := ioutil.ReadAll(io.NewSectionReader(pi, 0, int64(len(data))))
77 c.Check(err, qt.IsNil)
78 c.Assert(len(readData), qt.Equals, len(data))
79 c.Assert(bytes.Equal(readData, data), qt.IsTrue)
85 for range iter.N(int((capacity + info.TotalLength() - 1) / info.TotalLength())) {
90 for range iter.N(b.N) {