]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Add a benchmark for an observed slow case with Torrent.updatePiecePriorities
[btrtrc.git] / torrent_test.go
1 package torrent
2
3 import (
4         "testing"
5
6         "github.com/bradfitz/iter"
7         "github.com/stretchr/testify/assert"
8
9         "github.com/anacrolix/torrent/metainfo"
10         "github.com/anacrolix/torrent/peer_protocol"
11 )
12
13 func r(i, b, l peer_protocol.Integer) request {
14         return request{i, chunkSpec{b, l}}
15 }
16
17 // Check the given Request is correct for various torrent offsets.
18 func TestTorrentRequest(t *testing.T) {
19         const s = 472183431 // Length of torrent.
20         for _, _case := range []struct {
21                 off int64   // An offset into the torrent.
22                 req request // The expected Request. The zero value means !ok.
23         }{
24                 // Invalid offset.
25                 {-1, request{}},
26                 {0, r(0, 0, 16384)},
27                 // One before the end of a piece.
28                 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
29                 // Offset beyond torrent length.
30                 {472 * 1 << 20, request{}},
31                 // One before the end of the torrent. Complicates the chunk length.
32                 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
33                 {1, r(0, 0, 16384)},
34                 // One before end of chunk.
35                 {16383, r(0, 0, 16384)},
36                 // Second chunk.
37                 {16384, r(0, 16384, 16384)},
38         } {
39                 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
40                 if (_case.req == request{}) == ok {
41                         t.Fatalf("expected %v, got %v", _case.req, req)
42                 }
43                 if req != _case.req {
44                         t.Fatalf("expected %v, got %v", _case.req, req)
45                 }
46         }
47 }
48
49 func TestAppendToCopySlice(t *testing.T) {
50         orig := []int{1, 2, 3}
51         dupe := append([]int{}, orig...)
52         dupe[0] = 4
53         if orig[0] != 1 {
54                 t.FailNow()
55         }
56 }
57
58 func TestTorrentString(t *testing.T) {
59         tor := &Torrent{}
60         s := tor.InfoHash().HexString()
61         if s != "0000000000000000000000000000000000000000" {
62                 t.FailNow()
63         }
64 }
65
66 // This benchmark is from the observation that a lot of overlapping Readers on
67 // a large torrent with small pieces had a lot of overhead in recalculating
68 // piece priorities everytime a reader (possibly in another Torrent) changed.
69 func BenchmarkUpdatePiecePriorities(b *testing.B) {
70         cl := &Client{}
71         t := cl.newTorrent(metainfo.Hash{})
72         t.info = &metainfo.Info{
73                 Pieces:      make([]byte, 20*13410),
74                 PieceLength: 256 << 10,
75         }
76         t.makePieces()
77         assert.EqualValues(b, 13410, t.numPieces())
78         for range iter.N(7) {
79                 r := t.NewReader()
80                 r.SetReadahead(32 << 20)
81                 r.Seek(3500000, 0)
82         }
83         assert.Len(b, t.readers, 7)
84         t.pendPieceRange(0, t.numPieces())
85         for i := 0; i < t.numPieces(); i += 3 {
86                 t.completedPieces.Set(i, true)
87         }
88         for range iter.N(b.N) {
89                 t.updatePiecePriorities()
90         }
91 }