lib/Net/BitTorrent/Torrent/PiecePicker.pm

filter: Statementstmt 90.1% 73 / 81 Branchbran 68.7% 33 / 48 Conditioncond 37.9% 11 / 29 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 86.6% 13 / 15 Podpod 100% 8 / 8 totaltotal 71.2% 141 / 198 CC 36 SCAR 25.5
CC
36
Cov
74%
pick_block
22.4
pick_piece
13.7
set_priority
12.0
CRAP 12.8
« lib/Net/BitTorrent/Protocol/BEP52.pm ↑ Summary lib/Net/BitTorrent/Storage/File.pm »
1 26 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 26 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 26 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 #
5 26 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 class Net::BitTorrent::Torrent::PiecePicker v2.1.1 : isa(Net::BitTorrent::Emitter) {
7 26 ▶ use Acme::Selection::RarestFirst;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶ use Net::BitTorrent::Types qw[:pick];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 field $bitfield : param;
10 26 field $rarest_first = Acme::Selection::RarestFirst->new( size => $bitfield->size );
11 26 field $piece_priorities : param = undef;
12 26 field @piece_priorities;
13 26 field $strategy : param : reader : writer = PICK_RAREST_FIRST; # SEQUENTIAL, RAREST_FIRST, STREAMING
14 26 field $end_game : reader = 0;
15 #
16 68 ▶ ADJUST {
Subroutine __ANON__: called CC=2 SCAR=6.9
17 68 ▶ if ($piece_priorities) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece_priorities) { } 61 7 68
18 61 @piece_priorities = @$piece_priorities;
19 }
20 else {
21 7 @piece_priorities = (1) x $bitfield->size;
22 }
23 }
24 34 ▶ method update_availability ( $peer_bitfield, $delta ) { $rarest_first->update( $peer_bitfield, $delta ) }
Subroutine update_availability: called CC=1 SCAR=0.0
Pod documented
25  
26 0 ▶ method set_priority ( $index, $priority ) {
Subroutine set_priority: not called CC=3 SCAR=24.8
Pod documented
27 0 ▶ return if $index < 0 || $index >= $bitfield->size;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= $bitfield->size 0 0 0
Condition0%0/3
$index < 0 or $index >= $bitfield->size
l!l&&r!l&&!r
000
MC/DC0%0/2
$index < 0 or $index >= $bitfield->size
$index < 0 $index >= $bitfield->size
Truth table0%0/3
A or B
A= $index < 0B= $index >= $bitfield->size
ABresult
1X1
011
000
28 0 $piece_priorities[$index] = $priority;
29 }
30 0 ▶ method get_priority ($index) { $piece_priorities[$index] // 1 }
Subroutine get_priority: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$piece_priorities[$index] // 1
l!l
00
MC/DC0%0/1
$piece_priorities[$index] // 1
$piece_priorities[$index]
Truth table0%0/2
A
A= $piece_priorities[$index]
Aresult
11
00
31 3 ▶ method get_availability ($index) { $rarest_first->get_availability($index) }
Subroutine get_availability: called CC=1 SCAR=0.0
Pod documented
32  
33 16 ▶ method is_interesting ($peer) {
Subroutine is_interesting: called CC=6 SCAR=19.0
Pod documented
34 16 my $p_bf = $peer->torrent->peer_bitfields->{$peer};
35 16 ▶ unless ($p_bf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p_bf) 8 8 16
36 8 return 0;
37 }
38 8 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 8 8 16
39 8 ▶ if ( $p_bf->get($i) && !$bitfield->get($i) && $piece_priorities[$i] > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_bf->get($i) and not $bitfield->get($i) and $piece_priorities[$i] > 0) 0 8 8
Condition50%3/6
$p_bf->get($i) and not $bitfield->get($i) and $piece_priorities[$i] > 0
!ll&&!rl&&r
800
$p_bf->get($i) and not $bitfield->get($i)
!ll&&!rl&&r
620
MC/DC0%0/3
$p_bf->get($i) and not $bitfield->get($i) and $piece_priorities[$i] > 0
$p_bf->get($i) not $bitfield->get($i) $piece_priorities[$i] > 0
Truth table50%2/4
A and not(B) and C
A= $p_bf->get($i)B= not $bitfield->get($i)C= $piece_priorities[$i] > 0
ABCresult
0XX0
10X0
1100
1111
40 0 return 1;
41 }
42 }
43 8 return 0;
44 }
45  
46 2 ▶ method pick_piece ( $peer_bitfield, $blocks_pending ) {
Subroutine pick_piece: called CC=10 SCAR=26.2
Pod documented
47 2 ▶ return undef unless $peer_bitfield;
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfield 0 2 2
48  
49 # Get all candidates
50 2 my @candidates;
51 2 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 6 2 8
52 6 ▶ next if $bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
if $bitfield->get($i) 0 6 6
53 6 ▶ next if !$peer_bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfield->get($i) 2 4 6
54 4 ▶ next if $piece_priorities[$i] <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $piece_priorities[$i] <= 0 0 4 4
55 4 push @candidates, $i;
56 }
57 2 ▶ return undef unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 0 2 2
58  
59 # Apply Strategy to candidates
60 2 ▶ if ( $strategy == PICK_SEQUENTIAL ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($strategy == 0) { } 0 2 2
elsif ($strategy == 2) { } 0 2 2
61  
62 # Already sorted by index
63 }
64 elsif ( $strategy == PICK_STREAMING ) {
65 0 ▶ @candidates = sort { ( $piece_priorities[$b] <=> $piece_priorities[$a] ) || ( $a <=> $b ) } @candidates;
Condition0%0/3
$piece_priorities[$b] <=> $piece_priorities[$a] || $a <=> $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$piece_priorities[$b] <=> $piece_priorities[$a] || $a <=> $b
$piece_priorities[$b] <=> $piece_priorities[$a] $a <=> $b
Truth table0%0/3
A || B
A= $piece_priorities[$b] <=> $piece_priorities[$a]B= $a <=> $b
ABresult
1X1
011
000
66 }
67 else {
68 # RAREST_FIRST
69 2 @candidates = sort { $rarest_first->get_availability($a) <=> $rarest_first->get_availability($b) } @candidates;
70 }
71 2 return $candidates[0];
72 }
73  
74 34 ▶ method pick_block ( $peer, $blocks_pending ) {
Subroutine pick_block: called CC=18 SCAR=31.1
Pod documented
75 34 my $peer_bitfield = $peer->torrent->peer_bitfields->{$peer};
76 34 ▶ return undef unless $peer_bitfield;
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfield 0 34 34
77  
78 # Get all candidates
79 34 my @candidates;
80 34 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 42 34 76
81 42 ▶ next if $bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
if $bitfield->get($i) 5 37 42
82 37 ▶ next if !$peer_bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfield->get($i) 0 37 37
83 37 ▶ next if $piece_priorities[$i] <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $piece_priorities[$i] <= 0 0 37 37
84 37 push @candidates, $i;
85 }
86 34 ▶ return undef unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 2 32 34
87  
88 # Apply Strategy to candidates
89 32 ▶ if ( $strategy == PICK_SEQUENTIAL ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($strategy == 0) { } 0 32 32
elsif ($strategy == 2) { } 0 32 32
90  
91 # Already sorted by index
92 }
93 elsif ( $strategy == PICK_STREAMING ) {
94 0 ▶ @candidates = sort { ( $piece_priorities[$b] <=> $piece_priorities[$a] ) || ( $a <=> $b ) } @candidates;
Condition0%0/3
$piece_priorities[$b] <=> $piece_priorities[$a] || $a <=> $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$piece_priorities[$b] <=> $piece_priorities[$a] || $a <=> $b
$piece_priorities[$b] <=> $piece_priorities[$a] $a <=> $b
Truth table0%0/3
A || B
A= $piece_priorities[$b] <=> $piece_priorities[$a]B= $a <=> $b
ABresult
1X1
011
000
95 }
96 else {
97 # RAREST_FIRST
98 32 @candidates = sort { $rarest_first->get_availability($a) <=> $rarest_first->get_availability($b) } @candidates;
99 }
100 32 for my $piece_idx (@candidates) {
101 32 my $piece_len = $peer->torrent->piece_length($piece_idx);
102 32 my $offset = 0;
103 32 my $blocks_received = $peer->torrent->blocks_received;
104 32 ▶ while ( $offset < $piece_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $piece_len) 32 15 47
105 32 ▶ my $is_pending = $blocks_pending->{$piece_idx} && $blocks_pending->{$piece_idx}{$offset};
Condition100%3/3
$blocks_pending->{$piece_idx} && $blocks_pending->{$piece_idx}{$offset}
!ll&&!rl&&r
51512
MC/DC100%2/2
$blocks_pending->{$piece_idx} && $blocks_pending->{$piece_idx}{$offset}
$blocks_pending->{$piece_idx} $blocks_pending->{$piece_idx}{$offset}
Truth table100%3/3
A && B
A= $blocks_pending->{$piece_idx}B= $blocks_pending->{$piece_idx}{$offset}
ABresult
0X0
100
111
106 32 ▶ my $is_received = $blocks_received->{$piece_idx} && $blocks_received->{$piece_idx}{$offset};
Condition66%2/3
$blocks_received->{$piece_idx} && $blocks_received->{$piece_idx}{$offset}
!ll&&!rl&&r
17015
MC/DC50%1/2
$blocks_received->{$piece_idx} && $blocks_received->{$piece_idx}{$offset}
$blocks_received->{$piece_idx} $blocks_received->{$piece_idx}{$offset}
Truth table66%2/3
A && B
A= $blocks_received->{$piece_idx}B= $blocks_received->{$piece_idx}{$offset}
ABresult
0X0
100
111
107 32 ▶ if ( $end_game || ( !$is_pending && !$is_received ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_game or not $is_pending || $is_received) 32 0 32
Condition50%3/6
$end_game or not $is_pending || $is_received
l!l&&r!l&&!r
3110
$is_pending || $is_received
l!l&&r!l&&!r
001
MC/DC0%0/3
$end_game or not $is_pending || $is_received
$end_game $is_pending $is_received
Truth table50%2/4
A or not(B || C)
A= $end_gameB= $is_pendingC= $is_received
ABCresult
1XX1
0001
01X0
0010
108 32 ▶ if ( !$is_received ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_received) 17 15 32
109 17 return ( $piece_idx, $offset, 16384 );
110 }
111 }
112 15 $offset += 16384;
113 }
114 }
115 15 return undef;
116 }
117 6 ▶ method enter_end_game () { $end_game = 1 }
Subroutine enter_end_game: called CC=1 SCAR=0.0
Pod documented
118 }
119 #
120 26 1;
« lib/Net/BitTorrent/Protocol/BEP52.pm ↑ Summary lib/Net/BitTorrent/Storage/File.pm »