sauj
sauj

Reputation: 148

URL curl timings

I'm writing a script to output the total time to get a page.

curl -I -L -o /dev/null -w "Connect: %{time_connect}: TTFB: %{time_starttransfer} Total time: %{time_total} \n" $1 2>/dev/null

However the problem I'm having is that it returns the time for the final URL.

Example

www.apple.com time total is 3secs

www.chinesegooseberry.com redirects to www.kiwifruit.com total time is 3 secs

but in real terms it goes something like www.chinesegooseberry.com (1.5 secs to redirect) www.kiwifruit.com (3 secs) so it should give me a total time of 4.5 secs

any ideas?

Upvotes: 1

Views: 713

Answers (1)

Michael Slade
Michael Slade

Reputation: 13877

This quick perl script might help:

#! /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);
}

For each url passed on the command line, it will give the response time (end-to-end) in seconds, as a floating point number.

Hope this works for you.

Upvotes: 1

Related Questions