9 qt "github.com/frankban/quicktest"
11 "github.com/anacrolix/torrent/metainfo"
12 "github.com/anacrolix/torrent/storage"
17 DefaultPieceSize = 2 << 20
21 // This writes chunks to the storage concurrently, and waits for them all to complete. This matches
22 // the behaviour from the peer connection read loop.
23 func BenchmarkPieceMarkComplete(
24 b *testing.B, ci storage.ClientImpl,
25 pieceSize int64, numPieces int,
26 // This drives any special handling around capacity that may be configured into the storage
31 info := &metainfo.Info{
32 Pieces: make([]byte, numPieces*metainfo.HashSize),
33 PieceLength: pieceSize,
34 Length: pieceSize * int64(numPieces),
37 ti, err := ci.OpenTorrent(info, metainfo.Hash{})
38 c.Assert(err, qt.IsNil)
39 tw := storage.Torrent{ti}
41 rand.Read(info.Pieces)
42 data := make([]byte, pieceSize)
43 readData := make([]byte, pieceSize)
44 b.SetBytes(int64(numPieces) * pieceSize)
46 for pieceIndex := 0; pieceIndex < numPieces; pieceIndex += 1 {
47 pi := tw.Piece(info.Piece(pieceIndex))
51 for off := int64(0); off < int64(len(data)); off += ChunkSize {
55 n, err := pi.WriteAt(data[off:off+ChunkSize], off)
68 // This might not apply if users of this benchmark don't cache with the expected capacity.
69 c.Assert(pi.Completion(), qt.Equals, storage.Completion{Complete: false, Ok: true})
70 c.Assert(pi.MarkComplete(), qt.IsNil)
71 c.Assert(pi.Completion(), qt.Equals, storage.Completion{Complete: true, Ok: true})
72 n, err := pi.WriteTo(bytes.NewBuffer(readData[:0]))
74 c.Assert(err, qt.IsNil)
75 c.Assert(n, qt.Equals, int64(len(data)))
76 c.Assert(bytes.Equal(readData[:n], data), qt.IsTrue)
81 iterN := int((capacity + info.TotalLength() - 1) / info.TotalLength())
82 for i := 0; i < iterN; i += 1 {
88 for i := 0; i < b.N; i += 1 {