Skip to content

Instantly share code, notes, and snippets.

@cosimo
Created July 23, 2011 14:39
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cosimo/1101500 to your computer and use it in GitHub Desktop.
Save cosimo/1101500 to your computer and use it in GitHub Desktop.
Simple tool to detect TCP retransmit timeouts through HTTP requests
#!/usr/bin/env perl
#
# Fires HTTP request batches at the specified hostname
# and analyzes the response times.
#
# If you have suspicious frequency of 3.00x, 9.00x, 21.00x
# seconds, then most probably you have a problem of packet loss
# in your network.
#
# cosimo@opera.com, sometime in 2011
#
use strict;
use LWP::UserAgent ();
use Time::HiRes ();
$| = 1;
my $ua = LWP::UserAgent->new();
$ua->agent("$0/0.01");
# Tests this hostname
my $server = $ARGV[0] || die "Usage: $0 <hostname>\n";
# Picks the URLs to test in this list, one after the other
my @url_pool = qw(
/ping.html
);
my $total_reqs = 0;
my $total_elapsed = 0.0;
my $n_pick = 0;
my $url_to_fire;
my $max_elapsed = 0.0;
my $max_elapsed_when = '';
my $failed_reqs = 0;
my $slow_responses = 0;
my $terminate_now = 0;
sub output_report {
print "Report for: $server at " . localtime() . "\n";
printf "Total requests: %d in %.3f s\n", $total_reqs, $total_elapsed;
print "Failed requests: $failed_reqs\n";
print "Slow responses (>1s): $slow_responses (slowest $max_elapsed s at $max_elapsed_when)\n";
printf "Average response time: %.3f s (%.3f req/s)\n", $total_elapsed / $total_reqs, $total_reqs / $total_elapsed;
print "--------------------------------------------------------------------\n";
sleep 1;
return;
}
$SIG{INT} = sub { $terminate_now = 1 };
while (not $terminate_now) {
$url_to_fire = "http://" . $server . $url_pool[$n_pick];
my $t0 = [ Time::HiRes::gettimeofday() ];
my $resp = $ua->get($url_to_fire);
my $elapsed = Time::HiRes::tv_interval($t0);
$failed_reqs++ if ! $resp->is_success;
$total_reqs++;
$total_elapsed += $elapsed;
if ($elapsed > $max_elapsed) {
$max_elapsed = $elapsed;
$max_elapsed_when = scalar localtime;
printf "[SLOW] %s, %s served in %.3f s\n", $max_elapsed_when, $url_to_fire, $max_elapsed;
}
$slow_responses++ if $elapsed >= 0.5;
$n_pick = 0 if ++$n_pick > $#url_pool;
output_report() if $total_reqs > 0 and ($total_reqs % 1000 == 0);
}
continue {
Time::HiRes::usleep(100000);
}
output_report();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment