Skip to content

Instantly share code, notes, and snippets.

@olegwtf
Created August 30, 2011 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegwtf/1180610 to your computer and use it in GitHub Desktop.
Save olegwtf/1180610 to your computer and use it in GitHub Desktop.
measure speed with lwp
use LWP::UserAgent;
use Time::HiRes;
use strict;
my $ua = LWP::UserAgent->new();
my $start = Time::HiRes::time();
my $maxbytes = 1024*1024;
my $curspeed = 0;
my $received_bytes = 0;
my @speed_variations;
$ua->get("http://mirror.yandex.ru/debian/ls-lR.gz", ':content_cb' => sub {
$received_bytes += length($_[0]);
$curspeed = $received_bytes / (Time::HiRes::time() - $start);
printf "your speed is %f kb/s; %d kb was downloaded\n", $curspeed / 1024, $received_bytes / 1024;
die if $received_bytes > $maxbytes;
if (@speed_variations == 5) {
my $ok = 1;
for my $s (@speed_variations) {
if (abs($s - $curspeed) > 15 * 1024) {
$ok = 0;
last;
}
}
die if $ok;
shift @speed_variations;
}
push @speed_variations, $curspeed;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment