Categories
Perl

Test List of URLs for Fastest Response

This Perl script will take a list of URLs and sort them by the fastest ping response.

Originally written to test CPAN mirrors but you could use this for any list of URLs.


!#/usr/bin/perl
use strict;
use warnings;
use Net::Ping;
use Time::HiRes;
# takes a list of urls, pings them, returns a sorted list
# originally was written when choosing cpan modules
# $order is supposed to match the list provided by perl 5.8.8
# $order isn't useful for testing other types of URLS
# but the rest of the script should be
# I've already commented out ones that are frequently down
my %sortedURLS;
my %URLS = (
#'02' => 'bc1.hpc.lsu.edu',
'03' => 'carroll.cac.psu.edu',
'04' => 'cpan-du.viaverio.com',
'05' => 'cpan-sj.viaverio.com',
#'06' => 'cpan.calvin.edu',
'07' => 'cpan.cs.utah.edu',
'08' => 'cpan.erlbaum.net',
'09' => 'cpan.hexten.net',
#'10' => 'cpan.hostrack.net',
'11' => 'cpan.llarian.net',
'12' => 'cpan.mirrors.redwire.net',
'13' => 'cpan.mirrors.tds.net',
'14' => 'cpan.netnitco.net',
'15' => 'cpan.pair.com',
'16' => 'csociety-ftp.ecn.purdue.edu',
'17' => 'ftp-mirror.internap.com',
'18' => 'ftp.ccs.neu.edu',
'19' => 'ftp.dc.aleron.net',
'20' => 'ftp.epix.net',
'21' => 'ftp.mednor.net',
'22' => 'ftp.ncsu.edu',
'23' => 'ftp.ndlug.nd.edu',
'24' => 'ftp.osuosl.org',
'25' => 'ftp.saintjoe.edu',
'26' => 'ftp.uwsg.iu.edu',
'27' => 'ftp.wayne.edu',
'28' => 'mirror.candidhosting.com',
'29' => 'mirror.datapipe.net',
'30' => 'mirror.hiwaay.net',
#'31' => 'mirror.nyi.net',
'32' => 'mirror.sit.wisc.edu',
'33' => 'mirror.xmission.com',
'34' => 'mirrors.24-7-solutions.net',
'35' => 'mirrors.ibiblio.org',
'36' => 'mirrors.kernerl.org',
'37' => 'mirrors.phenominet.com',
'38' => 'osl.uoregon.edu',
'39' => 'perl.secsup.org',
'40' => 'cpan-2.mirrors.nks.net',
'41' => 'cpan.belfry.net',
'42' => 'cpan.glines.org',
'43' => 'cpan.groovis.net',
'44' => 'cpan.hoxt.com',
#'45' => 'cpan.mirror.facebook.com',
'46' => 'cpan.mirrorgeek.com',
'47' => 'cpan.mirrors.hoobly.com',
'48' => 'cpan.mirrors.nks.net'
);
checkURLS();
sortURLS();
sub checkURLS {
  print "TESTING URLS FROM LIST, PLEASE WAIT...\n\n";
  my $x = 0;
  foreach my $order ( keys %URLS ){
    my $cpan      = $order;
    my $location  = $URLS{$order};
    my $host      = trim($location);
    my $p = Net::Ping->new('icmp');
    $p->hires();
    my ($success, $timed, $ip) = $p->ping($host);
    if ($success) {
    $sortedURLS{$timed} = "$order $host";
    print "success: $host\n";
    } else {
      print "failed : $host\n";
    }
  }
}
sub sortURLS {
  print "\nFINAL RESULTS:\n";
  foreach my $timed ( sort (keys %sortedURLS) ) {
    my ($order, $host) = split(/ /,$sortedURLS{$timed});
    printf ("$order\t%.2f ms\t $host\n", $timed * 1000);
  }
}
#function that trims a domain
sub trim {
  $_ = my $input = shift;
  $_ =~ s/^\s+//;     # strip spaces at start of string
  $_ =~ s/\s+$//;     # strip spaces at end of string
  $_ =~ s|^http://||; # strip http:// at begining
  $_ =~ s|^ftp://||;  # strip ftp:// at beginning
  $_ =~ s|/.*$||;     # strip everything after the first /
  my $trimmed = $_;
  return $trimmed;
}

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.