blib/lib/Net/Shaper.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 68.7% 22 / 32 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 9 totaltotal 78.9% 116 / 147 CC 22 SCAR 18.8
CC
22
Cov
85%
run
14.3
new
2.1
LocalPort
2.1
CRAP 6.5
↑ Summary
1 package Net::Shaper;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.3';
12  
13 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
14 1 my($type, %args) = @_;
15 1 ▶ $type = ref $type || $type;
Condition33%1/3
ref $type || $type
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table33%1/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
16  
17 1 return bless \%args, $type;
18 }
19  
20 2 ▶sub LocalPort { @_ > 1 ? $_[0]->{LocalPort} = $_[1] : $_[0]->{LocalPort} }
Subroutine LocalPort: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 2 2
21 1 ▶sub LocalAddr { @_ > 1 ? $_[0]->{LocalAddr} = $_[1] : $_[0]->{LocalAddr} }
Subroutine LocalAddr: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
22 1 ▶sub LocalHost { @_ > 1 ? $_[0]->{LocalHost} = $_[1] : $_[0]->{LocalHost} }
Subroutine LocalHost: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
23 1 ▶sub PeerPort { @_ > 1 ? $_[0]->{PeerPort} = $_[1] : $_[0]->{PeerPort} }
Subroutine PeerPort: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
24 2 ▶sub PeerAddr { @_ > 1 ? $_[0]->{PeerAddr} = $_[1] : $_[0]->{PeerAddr} }
Subroutine PeerAddr: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 2 2
25 1 ▶sub PeerHost { @_ > 1 ? $_[0]->{PeerHost} = $_[1] : $_[0]->{PeerHost} }
Subroutine PeerHost: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
26 1 ▶sub Bps { @_ > 1 ? $_[0]->{Bps} = $_[1] : $_[0]->{Bps} }
Subroutine Bps: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
27  
28 1 ▶sub run {
Subroutine run: called CC=14 SCAR=26.6
Pod undocumented
29 1 my $this = shift;
30  
31 1 local $SIG{PIPE} = 'IGNORE';
32  
33 1 my @localArgs = map { $_ => $this->$_() } grep defined($this->$_()), qw(LocalPort LocalAddr LocalHost);
34 1 my @remoteArgs = map { $_ => $this->$_() } grep defined($this->$_()), qw(PeerPort PeerAddr PeerHost );
35  
36 1 my $src = IO::Socket::INET->new(@localArgs, Listen => SOMAXCONN, Reuse => 1, Proto => 'tcp');
37  
38 1 my $select = IO::Select->new($src);
39  
40 1 my $bps = $this->Bps();
41  
42 1 my(@dest, $done);
43  
44 1 ▶ $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { @dest = (); $done = 1;};
Subroutine __ANON__: called CC=1 SCAR=0.0
45  
46 1 ▶ while (!$done) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done) 6 1 7
47 6 ▶ if ($select->can_read(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->can_read(0)) 1 5 6
48 1 my $client = $src->accept();
49 1 push @dest, [$client => IO::Socket::INET->new(@remoteArgs, Proto => 'tcp')];
50 }
51  
52 6 my $start = Time::HiRes::time();
53 6 my @recvBuf = my @sendBuf = ();
54 6 my $bytes = 0;
55 6 ▶ my $bytesToRead = $bps && @dest ? $bps / @dest : 32768;
Branch: 1 branch
BranchTrueFalseTotal
$bps && @dest ? : 5 1 6
Condition66%2/3
$bps and @dest
!ll&&!rl&&r
015
MC/DC50%1/2
$bps and @dest
$bps @dest
Truth table66%2/3
A and B
A= $bpsB= @dest
ABresult
0X0
100
111
56 6 ▶ for (my $i = 0; $i < @dest; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @dest) 5 6 11
57 5 my($client, $dest) = @{ $dest[$i] };
58 5 $client->recv($recvBuf[$i], $bytesToRead, IO::Socket::MSG_DONTWAIT);
59 5 $dest->recv ($sendBuf[$i], $bytesToRead, IO::Socket::MSG_DONTWAIT);
60 5 $bytes += length($recvBuf[$i]) + length($sendBuf[$i]);
61 }
62 6 my $now = Time::HiRes::time();
63  
64 6 ▶ unless ($bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes) 2 4 6
65 # wait for something to be ready to read
66 2 my $sel = IO::Select->new();
67 2 $sel->add($_) for $src, map @$_, @dest;
68 2 my @ready = $sel->can_read();
69 2 for my $fh (@ready) {
70 1 $fh->recv(my $buf, 1, IO::Socket::MSG_PEEK);
71 1 ▶ unless (length($buf)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $buf) 1 0 1
72 0 ▶ @dest = grep { $_->[0] != $fh && $_->[1] != $fh } @dest;
Condition0%0/3
$_->[0] != $fh && $_->[1] != $fh
!ll&&!rl&&r
000
MC/DC0%0/2
$_->[0] != $fh && $_->[1] != $fh
$_->[0] != $fh $_->[1] != $fh
Truth table0%0/3
A && B
A= $_->[0] != $fhB= $_->[1] != $fh
ABresult
0X0
100
111
73 }
74 }
75 2 next;
76 }
77  
78 4 ▶ if ($bps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bps) 4 0 4
79 4 ▶ unless ($bytes / ($now - $start) < $bps) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes / ($now - $start) < $bps) 4 0 4
80 4 Time::HiRes::sleep(($bytes - $bps * ($now - $start)) / $bps);
81 }
82 }
83  
84 4 ▶ for (my $i = 0; $i < @dest; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @dest) 4 4 8
85 4 my($client, $dest) = @{ $dest[$i] };
86 4 $dest->send($recvBuf[$i]);
87 4 $client->send($sendBuf[$i]);
88 }
89 }
90 }
91  
92 2 1;
93 __END__
↑ Summary