Skip to content

Instantly share code, notes, and snippets.

@drags
Created May 9, 2012 22:56
Show Gist options
  • Save drags/2649522 to your computer and use it in GitHub Desktop.
Save drags/2649522 to your computer and use it in GitHub Desktop.
Test load times of awesm redirect vs destination URL
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw/time/;
use LWP::UserAgent;
###############################################################################
#
# compare lookup times of awe.sm versus the source urls themselves
#
# shares.csv is the input file, the first column should contain the awesm url
#
# limit tests to around 100 items unless you are the extremely thorough and
# patient type
#
###############################################################################
my $DEBUG=0;
#turn off print buffering:
$| = 1;
my $ua = LWP::UserAgent->new();
$ua->max_redirect(0);
open my $csv, '<', 'shares.csv' or die("Unable to open file\n");
my %awesm_lookups;
my %destination_loads;
print "\n";
print "Testing lookups\n";
while(<$csv>) {
next unless m/http/;
my($short_url,undef) = split /,/;
$short_url =~ s/"//g;
my $awesm_lookup_start = time;
my $response = $ua->get($short_url);
my $awesm_lookup_end = time;
$awesm_lookups{$short_url} = $awesm_lookup_end - $awesm_lookup_start;
print "Looked up $short_url on awesm redirector in ". $awesm_lookups{$short_url} ." seconds.\n" if $DEBUG;
my $long_url = $response->header('Location');
if (! $long_url) {
print "Did not get a long url for: $short_url \n";
}
print $long_url . "\n" if $DEBUG;
my $destination_lookup_start = time;
$response = $ua->get($long_url);
my $destination_lookup_end = time;
$destination_loads{$short_url} = $destination_lookup_end - $destination_lookup_start;
print "Loaded $long_url on destination in " . $destination_loads{$short_url}. " seconds.\n" if $DEBUG;
print "\n---\n\n" if $DEBUG;
print "." unless $DEBUG;
}
print "\n";
{
my ($lookup_total, $load_total, $num_lookups);
my ($min_awesm, $mean_awesm, $max_awesm) = 0;
my ($min_awesm_url, $max_awesm_url);
my ($min_destination, $mean_destination, $max_destination) = 0;
my ($min_destination_url, $max_destination_url);
$min_awesm=100000;
$min_destination=100000;
for (keys %awesm_lookups) {
$lookup_total += $awesm_lookups{$_};
$load_total += $destination_loads{$_};
$num_lookups++;
if ($awesm_lookups{$_} < $min_awesm) {
$min_awesm = $awesm_lookups{$_};
$min_awesm_url = $_;
}
if ($awesm_lookups{$_} > $max_awesm) {
$max_awesm = $awesm_lookups{$_};
$max_awesm_url = $_;
}
if ($destination_loads{$_} < $min_destination) {
$min_destination = $destination_loads{$_};
$min_destination_url = $_;
}
if ($destination_loads{$_} > $max_destination) {
$max_destination = $destination_loads{$_};
$max_destination_url = $_;
}
}
$mean_awesm = $lookup_total / $num_lookups;
$mean_destination = $load_total / $num_lookups;
print "Average awesm lookup time: $mean_awesm\n";
print "Min awesm lookup time: $min_awesm ($min_awesm_url)\n";
print "Max awesm lookup time: $max_awesm ($max_awesm_url)\n";
print "Average destination load time: $mean_destination\n";
print "Min destination load time: $min_destination ($min_destination_url)\n";
print "Max destination load time: $max_destination ($max_destination_url)\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment