Network Speed and Jitter Perl Script

My friend Shawn and I have recently been burdened by terrible networks (both LAN and WAN).  Shawn finally got fed up before I did and decided to write a tool in perl to test network capability and calculate the deviance (jitter).  This is a lifesaver especially when measuring UDP for SIP.  I really appreciate him for this…except he needs to add a graph! (inside joke) =D  Bravo Shawn!  As always, the code was originally properly formatted, but wordpress just hates copy and pasting and seems to remove that on me.


#!/usr/bin/perl -w

use strict;

use Net::Ping;
use Time::HiRes;
use WWW::Curl;
use WWW::Curl::Easy;

use constant VERNUM => 2012092808;

my ($hostip,$proto);

printf "IP:\n";
$hostip = <>;
chomp ($hostip);

printf "PROTOCOL:\n";
$proto = <>;
chomp ($proto);
printf "\n";

sub PING {
my ($ret,$dur,$ip,$count,$bytes);
my ($px,$pn,$psum,$psos,$pvar,$psd,$pavg);
my ($time,$ploss,$grade,$gL);
my ($eff_latency,$mosR,$mosR2,$mosgrade);
my $noret = 0;

my @bytes = qw(8 12 16 32 64 128 256 512 768 1024);
my @pvals;

open (PT, ">", "/tmp/netbench.txt") or die $!;
foreach $bytes (@bytes) {
for ($count = 25; $count >= 1; $count--) {
my $p = Net::Ping->new($proto, "1", $bytes);
$p->hires();
($ret, $dur, $ip) = $p->ping($hostip);
$time = (1000 * $dur);
if ($ret == 0) {
printf PT ("[TIMEOUT]\n");
$noret++;
} elsif ($time > 500) {
printf PT ("[E] Size: $bytes - Time: $time\n");
} elsif ($time > 300) {
printf PT ("[D] Size: $bytes - Time: $time\n");
} elsif ($time > 150) {
printf PT ("[C] Size: $bytes - Time: $time\n");
} elsif ($time > 100) {
printf PT ("[B] Size: $bytes - Time: $time\n");
} else {
printf PT ("[A] Size: $bytes - Time: $time\n");
}
push(@pvals, (1000 * $dur));
foreach $px (@pvals) {
$psum += $px;
$pn++;
$psos += $px * $px;
}
$p->close();
}
$pvar = ($psos-(($psum*$psum)/$pn))/($pn-1);
$psd = sqrt($pvar);
$pavg = ($psum/$pn);
if (($pavg > 500) || ($psd > 150)) {
printf PT ("[E] Size: $bytes - Average: $pavg - Deviance: $psd\n");
} elsif (($pavg > 300) || ($psd > 100)) {
printf PT ("[D] Size: $bytes - Average: $pavg - Deviance: $psd\n");
} elsif (($pavg > 150) || ($psd > 50)) {
printf PT ("[C] Size: $bytes - Average: $pavg - Deviance: $psd\n");
} elsif (($pavg > 100) || ($psd > 30)) {
printf PT ("[B] Size: $bytes - Average: $pavg - Deviance: $psd\n");
} else {
printf PT ("[A] Size: $bytes - Average: $pavg - Deviance: $psd\n");
}
printf ("Size: $bytes - Average: $pavg - Deviance: $psd\n");

}
$ploss = (($noret/250) * 100);
if ($noret >= 40) {
printf PT ("[E] Packets lost: $noret - Loss percentage: $ploss\n");
} elsif ($noret >= 6) {
printf PT ("[D] Packets lost: $noret - Loss percentage: $ploss\n");
} elsif ($noret > 1) {
printf PT ("[C] Packets lost: $noret - Loss percentage: $ploss\n");
} elsif ($noret == 1) {
printf PT ("[B] Packets lost: $noret - Loss percentage: $ploss\n");
} else {
printf PT ("[A] Packets lost: $noret - Loss percentage: $ploss\n");
}
printf ("Packets lost: $noret - Loss percentage: $ploss\n");
$eff_latency = ($pavg+$psd*2+10);
if ($eff_latency < 160) {
$mosR = (93.2-($eff_latency/40));
} else {
$mosR = (93.2-($eff_latency-120)/10);
}
$mosR2 = ($mosR-($ploss*2.5));
$mosgrade = (1+(0.035)*$mosR2+(.000007)*$mosR2*($mosR2-60)*(100-$mosR2));
if ($mosgrade >= 4.30) {
printf PT ("[A] MOS: $mosgrade\n");
printf ("MOS: %.2f - Grade: [A]\n", $mosgrade);
} elsif ($mosgrade >= 4.20) {
printf PT ("[B] MOS: $mosgrade\n");
printf ("MOS: %.2f - Grade: [B]\n", $mosgrade);
} elsif ($mosgrade >= 4.00) {
printf PT ("[C] MOS: $mosgrade\n");
printf ("MOS: %.2f - Grade: [C]\n", $mosgrade);
} elsif ($mosgrade >= 2.50) {
printf PT ("[D] MOS: $mosgrade\n");
printf ("MOS: %.2f - Grade: [D]\n", $mosgrade);
} elsif ($mosgrade < 2.50) {
printf PT ("[E] MOS: $mosgrade\n");
printf ("MOS: %.2f - Grade: [E]\n", $mosgrade);
}
close (PT);
}

sub CURL {
my $curl = WWW::Curl::Easy->new;
my $cstart = time();
my ($fsize,$timedif,$c_spd);

$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, 'http://speedtest.wdc01.softlayer.com/downloads/test100.zip');

my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);

my $retcode = $curl->perform;

if ($retcode == 0) {
$fsize = 104874307;
} else {
print("ERROR: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}
my $cend = time();
$timedif = ($cend-$cstart);
$c_spd = int((((($fsize/$timedif)/1000)/1000)*8));
printf ("SPEED: $c_spd mbps\n");
}

PING();
CURL();

Leave a Reply

Your email address will not be published. Required fields are marked *