我正在尝试创建一个perl脚本来测试一堆镜像服务器,以便使用最好的镜像。
查看下载文件需要多长时间的最佳方法是什么?我试图避免像
这样的系统调用 $starttime = time();
$res = `wget -o/dev/null SITE.com/file.txt`;
$endtime = time();
$elapsed = $endtime - $starttime;
我宁愿使用Perl函数
答案 0 :(得分:5)
您可以使用LWP::Simple或HTTP::Tiny(自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}";
}