| 1 | package Net::Iperf::Parser; | ||
| 2 | $Net::Iperf::Parser::VERSION = '0.03'; | ||
| 3 | use Moose; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | # ABSTRACT: Parse a single iperf line result | ||
| 7 | |||
| 8 | |||
| 9 | has start => ( is => 'ro', isa => 'Int', default => 0 ); | ||
| 10 | has end => ( is => 'ro', isa => 'Int', default => 0 ); | ||
| 11 | has is_valid => ( is => 'ro', isa => 'Bool', default => 1 ); | ||
| 12 | has is_process_avg => ( is => 'ro', isa => 'Bool', default => 1 ); | ||
| 13 | has speed => ( is => 'ro', isa => 'Num', default => 0 ); | ||
| 14 | |||
| 15 | |||
| 16 | sub duration { | ||
| 17 | my $s = shift; | ||
| 18 | return $s->end - $s->start; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub is_global_avg { | ||
| 22 | my $s = shift; | ||
| 23 | return ($s->is_process_avg && $s->start == 0 && $s->end > 5) || 0; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub speedk { | ||
| 27 | return shift->speed / 1024; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub speedm { | ||
| 31 | return shift->speed / (1024 * 1024); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub dump { | ||
| 35 | my $s = shift; | ||
| 36 | |||
| 37 | my @fld = qw/is_valid start end duration speed speedk speedm | ||
| 38 | is_process_avg is_global_avg/; | ||
| 39 | |||
| 40 | my $ret = "{\n"; | ||
| 41 | |||
| 42 | foreach(@fld) { | ||
| 43 | $ret .= "\t$_ => " . $s->$_ . ",\n"; | ||
| 44 | } | ||
| 45 | |||
| 46 | $ret .= '}'; | ||
| 47 | |||
| 48 | return $ret; | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub parsecsv { | ||
| 54 | my $s = shift; | ||
| 55 | my $row = shift || ''; | ||
| 56 | if ($row =~ /\,/) { | ||
| 57 | $s->{is_valid} = 1; | ||
| 58 | my @itms = split(/,/,$row); | ||
| 59 | |||
| 60 | my $t_range = $itms[6]; | ||
| 61 | ($s->{start},$s->{end}) = map $_+0, split(/-/, $t_range); | ||
| 62 | |||
| 63 | $s->{is_process_avg} = ($itms[5] == -1 || 0); | ||
| 64 | #$s->{speed} = ($itms[-1] / $s->duration); | ||
| 65 | $s->{speed} = $itms[-1] + 0; | ||
| 66 | } else { | ||
| 67 | $s->{is_valid} = 0; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub parse { | ||
| 72 | my $s = shift; | ||
| 73 | my $row = shift || ''; | ||
| 74 | if ($row =~ /^\[((\s*\d+)|SUM)\]\s+\d/) { | ||
| 75 | $s->{is_valid} = 1; | ||
| 76 | my @itms; | ||
| 77 | $row =~ /([\d\.]+-\s*[\d\.]+)\s+sec/; | ||
| 78 | my $t_range = $1; | ||
| 79 | ($s->{start},$s->{end}) = map $_+0, split(/-/, $t_range); | ||
| 80 | |||
| 81 | $s->{is_process_avg} = ($row =~ /^\[SUM\]/ || 0); | ||
| 82 | $row =~/\s+([\d\.]+)\s+(\w+)\/sec/; | ||
| 83 | if ($2 eq 'Mbits') { | ||
| 84 | $s->{speed} = ($1+0) * 1024 * 1024; | ||
| 85 | } else { | ||
| 86 | $s->{speed} = ($1+0) * 1024; | ||
| 87 | } | ||
| 88 | } else { | ||
| 89 | $s->{is_valid} = 0; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | __PACKAGE__->meta->make_immutable; | ||
| 95 | |||
| 96 | 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 |