Reputation: 8670
I am trying to create a perl script that tests a bunch of mirror servers for the best mirror to use.
What is the best method to see how long it takes to download a file? I am trying to avoid system calls like
$starttime = time();
$res = `wget -o/dev/null SITE.com/file.txt`;
$endtime = time();
$elapsed = $endtime - $starttime;
I'd rather use Perl functions
Upvotes: 1
Views: 88
Reputation: 165396
You can use LWP::Simple or HTTP::Tiny (which is built in since 5.14.0).
use strict;
use warnings;
use v5.10; # for say()
use HTTP::Tiny;
use Time::HiRes qw(time); # to measure less than a second
use URI;
my $url = URI->new(shift);
# Add the HTTP scheme if the URL is schemeless.
$url = URI->new("http://$url") unless $url->scheme;
my $start = time;
my $response = HTTP::Tiny->new->get($url);
my $total = time - $start;
if( $response->{success} ) {
say "It took $total seconds to fetch $url";
say "The content was @{[ length $response->{content} ]} bytes";
}
else {
say "Fetching $url failed: $response->{status} $response->{reason}";
}
Upvotes: 5