#!perl -w
use strict;
use warnings;
use LWP::UserAgent;
use LWPx::TimedHTTP qw(:autoinstall);
use URI;
our @TIMINGS = qw(Client-Request-Dns-Time Client-Request-Connect-Time Client-Request-Transmit-Time Client-Response-Server-Time Client-Response-Receive-Time);
our $EXIT = 0;
$|++;
$SIG{INT} = sub { $EXIT++ };
=head1 NAME
fastly_speed_test - benchmark the speed difference between a site cached by Fastly
=head1 USAGE
fastly_speed_test [--runs <number of runs>] <url> [fastly domain]
Default number of runs is 10.
=cut
my $runs = 10;
while (@ARGV && $ARGV[0] =~ /^-+(.+)/) {
my $name = $1; shift;
my $val = shift || die "You must pass in a param to $name\n";
if ("runs" eq $name) {
$runs = $val;
die "Runs must be more than 0\n" unless $runs>0;
}
}
my $url = shift || die "You must pass in a url\n";
my $plain = URI->new($url);
my $host = URI->new(shift || "$plain")->host;
my $fastly = $plain->clone; $fastly->host("$host.a.prod.fastly.net");
print "Benchmarking $plain vs $fastly over $runs runs\n\n";
my %plain_results;
my %fastly_results;
my $ua = LWP::UserAgent->new;
foreach my $run (0..$runs) {
print "Run $run \r";
my $pr = $ua->get($plain);
my $fr = $ua->get($fastly);
next unless $run; # give them a chance to warm the cache
foreach my $timing (@TIMINGS) {
$plain_results{$timing} += 0+$pr->header($timing);
$fastly_results{$timing} += 0+$fr->header($timing);
$plain_results{'total'} += 0+$pr->header($timing);
$fastly_results{'total'} += 0+$fr->header($timing);
}
last if $EXIT;
}
print " \r";
print " Timing Plain/s Fastly/s Diff\n";
foreach my $timing ((@TIMINGS, 'total')) {
print "---------------------------------------------\n" if $timing eq $TIMINGS[0] || $timing eq 'total';
my $name = (split('-', $timing))[2] || $timing;
my $plaint = $plain_results{$timing}/$runs;
my $fastlyt = $fastly_results{$timing}/$runs;
my $diff = $fastlyt - $plaint;
printf "%8s %8s %8s %8s\n", $name, sprintf("%.3f",$plaint), sprintf("%.3f",$fastlyt), sprintf("%.3f", $diff);
}
=head1 COPYRIGHT
Copyright 2011 - Fastly Inc
Mail support at fastly dot com if you have problems.
=head1 DEVELOPERS
http://github.com/fastly/fastly-perl
http://www.fastly.com/documentation
=cut