From 792ab183e42cd42dbfe16a1c11aaa9c7c26244f0 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 30 Aug 2016 14:21:50 +1000 Subject: [PATCH] Add a benchmark for an observed slow case with Torrent.updatePiecePriorities --- torrent_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/torrent_test.go b/torrent_test.go index 6ffc8605..60951663 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -3,6 +3,10 @@ package torrent import ( "testing" + "github.com/bradfitz/iter" + "github.com/stretchr/testify/assert" + + "github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/peer_protocol" ) @@ -58,3 +62,30 @@ func TestTorrentString(t *testing.T) { t.FailNow() } } + +// This benchmark is from the observation that a lot of overlapping Readers on +// a large torrent with small pieces had a lot of overhead in recalculating +// piece priorities everytime a reader (possibly in another Torrent) changed. +func BenchmarkUpdatePiecePriorities(b *testing.B) { + cl := &Client{} + t := cl.newTorrent(metainfo.Hash{}) + t.info = &metainfo.Info{ + Pieces: make([]byte, 20*13410), + PieceLength: 256 << 10, + } + t.makePieces() + assert.EqualValues(b, 13410, t.numPieces()) + for range iter.N(7) { + r := t.NewReader() + r.SetReadahead(32 << 20) + r.Seek(3500000, 0) + } + assert.Len(b, t.readers, 7) + t.pendPieceRange(0, t.numPieces()) + for i := 0; i < t.numPieces(); i += 3 { + t.completedPieces.Set(i, true) + } + for range iter.N(b.N) { + t.updatePiecePriorities() + } +} -- 2.44.0