Created
August 30, 2011 10:25
-
-
Save olegwtf/1180610 to your computer and use it in GitHub Desktop.
measure speed with lwp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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