lib/Net/Iperf/Parser.pm

filter: Statementstmt 80.3% 41 / 51 Branchbran 83.3% 5 / 6 Conditioncond 93.7% 15 / 16 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 66.6% 6 / 9 Podpod 85.7% 6 / 7 totaltotal 82.2% 79 / 96 SCAR 25.4
CC
12
Cov
84%
dump
6.0
parse
5.0
is_global_avg
4.0
CRAP 12.6
« blib/lib/Net/Iperf/Parser.pm ↑ Summary
1 package Net::Iperf::Parser;
2 2 $Net::Iperf::Parser::VERSION = '0.03';
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Parse a single iperf line result
7  
8  
9 2 has start => ( is => 'ro', isa => 'Int', default => 0 );
10 2 has end => ( is => 'ro', isa => 'Int', default => 0 );
11 2 has is_valid => ( is => 'ro', isa => 'Bool', default => 1 );
12 2 has is_process_avg => ( is => 'ro', isa => 'Bool', default => 1 );
13 2 has speed => ( is => 'ro', isa => 'Num', default => 0 );
14  
15  
16 sub duration {
17 2 ▶ my $s = shift;
Subroutine duration: called CC=1 SCAR=0.0
Pod undocumented
18 2 return $s->end - $s->start;
19 }
20  
21 sub is_global_avg {
22 6 ▶ my $s = shift;
Subroutine is_global_avg: called CC=4 SCAR=13.9
Pod documented
23 6 ▶ return ($s->is_process_avg && $s->start == 0 && $s->end > 5) || 0;
Condition87%7/8
$s->is_process_avg && $s->start == 0 && $s->end > 5 || 0
l!l
24
$s->is_process_avg and $s->start == 0 and $s->end > 5
!ll&&!rl&&r
222
$s->is_process_avg and $s->start == 0
!ll&&!rl&&r
204
MC/DC66%2/3
$s->is_process_avg && $s->start == 0 && $s->end > 5 || 0
$s->is_process_avg $s->start == 0 $s->end > 5
Truth table75%3/4
A and B and C
A= $s->is_process_avgB= $s->start == 0C= $s->end > 5
ABCresult
1111
0XX0
10X0
1100
24 }
25  
26 sub speedk {
27 0 ▶ return shift->speed / 1024;
Subroutine speedk: not called CC=1 SCAR=6.9
Pod documented
28 }
29  
30 sub speedm {
31 0 ▶ return shift->speed / (1024 * 1024);
Subroutine speedm: not called CC=1 SCAR=6.9
Pod documented
32 }
33  
34 sub dump {
35 0 ▶ my $s = shift;
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
36  
37 0 my @fld = qw/is_valid start end duration speed speedk speedm
38 is_process_avg is_global_avg/;
39  
40 0 my $ret = "{\n";
41  
42 0 foreach(@fld) {
43 0 $ret .= "\t$_ => " . $s->$_ . ",\n";
44 }
45  
46 0 $ret .= '}';
47  
48 0 return $ret;
49  
50 }
51  
52  
53 sub parsecsv {
54 5 ▶ my $s = shift;
Subroutine parsecsv: called CC=4 SCAR=13.9
Pod documented
55 5 ▶ my $row = shift || '';
Condition100%2/2
shift() || ''
l!l
32
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
56 5 ▶ if ($row =~ /\,/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row =~ /\,/) { } 3 2 5
57 3 $s->{is_valid} = 1;
58 3 my @itms = split(/,/,$row);
59  
60 3 my $t_range = $itms[6];
61 3 ($s->{start},$s->{end}) = map $_+0, split(/-/, $t_range);
62  
63 3 ▶ $s->{is_process_avg} = ($itms[5] == -1 || 0);
Condition100%2/2
$itms[5] == -1 || 0
l!l
21
MC/DC100%1/1
$itms[5] == -1 || 0
$itms[5] == -1
Truth table100%2/2
A
A= $itms[5] == -1
Aresult
11
00
64 #$s->{speed} = ($itms[-1] / $s->duration);
65 3 $s->{speed} = $itms[-1] + 0;
66 } else {
67 2 $s->{is_valid} = 0;
68 }
69 }
70  
71 sub parse {
72 5 ▶ my $s = shift;
Subroutine parse: called CC=5 SCAR=16.1
Pod documented
73 5 ▶ my $row = shift || '';
Condition100%2/2
shift() || ''
l!l
41
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
74 5 ▶ if ($row =~ /^\[((\s*\d+)|SUM)\]\s+\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row =~ /^\[((\s*\d+)|SUM)\]\s+\d/) { } 3 2 5
75 3 $s->{is_valid} = 1;
76 3 my @itms;
77 3 $row =~ /([\d\.]+-\s*[\d\.]+)\s+sec/;
78 3 my $t_range = $1;
79 3 ($s->{start},$s->{end}) = map $_+0, split(/-/, $t_range);
80  
81 3 ▶ $s->{is_process_avg} = ($row =~ /^\[SUM\]/ || 0);
Condition100%2/2
$row =~ /^\[SUM\]/ || 0
l!l
21
MC/DC100%1/1
$row =~ /^\[SUM\]/ || 0
$row =~ /^\[SUM\]/
Truth table100%2/2
A
A= $row =~ /^\[SUM\]/
Aresult
11
00
82 3 $row =~/\s+([\d\.]+)\s+(\w+)\/sec/;
83 3 ▶ if ($2 eq 'Mbits') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq 'Mbits') { } 3 0 3
84 3 $s->{speed} = ($1+0) * 1024 * 1024;
85 } else {
86 0 $s->{speed} = ($1+0) * 1024;
87 }
88 } else {
89 2 $s->{is_valid} = 0;
90 }
91 }
92  
93  
94 2 __PACKAGE__->meta->make_immutable;
95  
96 2 1;
97  
98 __END__
99  
100 =pod
101  
102 =encoding UTF-8
103  
104 =head1 NAME
105  
106 Net::Iperf::Parser - Parse a single iperf line result
107  
108 =head1 VERSION
109  
110 version 0.03
111  
112 =head1 SYNOPSIS
113  
114 use Net::Iperf::Parser;
115  
116 my $p = new Net::Iperf::Parser;
117  
118 my @rows = `iperf -c iperf.volia.net -P 2`;
119  
120 foreach (@rows) {
121 $p->parse($_);
122 print $p->dump if ($p->is_valid && $p->is_global_avg);
123 }
124  
125 and result is something like this
126  
127 {
128 is_valid => 1,
129 start => 0,
130 end => 10,
131 duration => 10,
132 speed => 129024,
133 speedk => 126,
134 speedm => 0.123046875,
135 is_process_avg => 1,
136 is_global_avg => 1,
137 }
138  
139 =head1 DESCRIPTION
140  
141 Parse a single iperf line result in default or CSV mode
142  
143 =head1 METHODS
144  
145 =head2 start
146  
147 Return the start time
148  
149 =head2 end
150  
151 Return the end time
152  
153 =head2 is_valid
154  
155 Return if the parsed row is a valid iperf row
156  
157 =head2 is_process_avg
158  
159 Return if the row is a process average value
160  
161 =head2 is_global_avg
162  
163 Return if the row is the last summary value
164  
165 =head2 speed
166  
167 Return the speed calculated in bps
168  
169 =head2 speedk
170  
171 Return the speed calculated in Kbps
172  
173 =head2 speedm
174  
175 Return the speed calculated in Mbps
176  
177 =head2 dump
178  
179 Return a to_string version of the object (like a Data::Dumper::dumper)
180  
181 =head2 parse($row)
182  
183 Parse a single iperf line result
184  
185 =head2 parsecsv($row)
186  
187 Parse a single iperf line result in CSV mode (-y C)
188  
189 =head1 SEE ALSO
190  
191 L<iperf|https://iperf.fr/>
192  
193 =head1 AUTHOR
194  
195 Emiliano Bruni <info@ebruni.it>
196  
197 =head1 COPYRIGHT AND LICENSE
198  
199 This software is copyright (c) 2019 by Emiliano Bruni.
200  
201 This is free software; you can redistribute it and/or modify it under
202 the same terms as the Perl 5 programming language system itself.
203  
204 =cut
« blib/lib/Net/Iperf/Parser.pm ↑ Summary