我正在编写一个脚本来输出获取页面的总时间。
curl -I -L -o /dev/null -w "Connect: %{time_connect}: TTFB: %{time_starttransfer} Total time: %{time_total} \n" $1 2>/dev/null
但是我遇到的问题是它返回最终 URL 的时间。
示例
www.apple.com 的总时间是 3 秒
www.chinesegooseberry.com 重定向 到 www.kiwifruit.com 总时间为 3 秒
但实际上它是这样的 www.chinesegooseberry.com(1.5 秒重定向) www.kiwifruit.com(3 秒) 所以它应该给我 4.5 秒的总时间
有什么想法吗?
最佳答案
这个快速的 perl 脚本可能会有所帮助:
#! /usr/bin/perl
use LWP::UserAgent;
use Time::HiRes qw/&time/;
sub timereq {
my ($url) = @_;
my $ua = LWP::UserAgent->new;
$ua->requests_redirectable ([qw/GET POST/]);
my $start = time;
my $ret = $ua->get($url);
if ($ret->is_success()) {
return time-$start;
}
print STDERR "req to $url failed\n" unless $ret->is_success();
return undef;
}
$| = 1;
foreach my $url(@ARGV) {
printf("%6.3f %s\n",timereq($url),$url);
}
对于在命令行上传递的每个 url,它将以秒为单位给出响应时间(端到端),作为 float 。
希望这对你有用。
关于linux - URL curl 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10010193/