Reputation: 25117
Does anybody know a module to test the speed of internet-connection?
Upvotes: 4
Views: 3897
Reputation: 129509
Speed as in bandwidth? Or as in latency? For the latter, just use Net::Ping.
For bandwidth, I don't know if there's anything ready made, there's 2 approaches:
You can try to leverage ibmonitor
Otherwise, to measure download bandwidth find a web site that lets you measure bandwidth by downloading a large file (or find such a large file on high-performance site); start the timer, start downloading that file (e.g. using LWP or any other module you wish - or Net::FTP if your file is on FTP site) - measure how long it takes and divide by the file size.
Similar logic for measuring upload bandwidth, but instead of finding large file, you need to find a place on the internet (like a public repository) that'd allow uploading one.
Upvotes: 8
Reputation: 25117
#!/usr/bin/env perl
use warnings; use strict;
use 5.010;
use Time::HiRes qw(gettimeofday tv_interval);
use LWP::Simple;
use File::stat;
my %h = (
'500x500' => 505544,
'750x750' => 1118012,
'1000x1000' => 1986284,
'1500x1500' => 4468241,
'2000x2000' => 7907740,
);
my $pixel = '1000x1000';
my $url_file = 'http://speedserver/file'.$pixel.'.jpg';
my $file = 'file'.$pixel.'.jpg';
unlink $file or die $! if -e $file;
my $start = gettimeofday;
my $response = getstore( $url_file, $file );
my $end = gettimeofday;
open my $fh, '>>', 'speed_test.txt' or die $!;
say $fh scalar localtime;
if ( not is_success $response ) {
say $fh "error occured:";
say $fh "HTTP response code = $response";
}
else {
my $size = stat( $file )->size if -e $file;
$size ||= 0;
if ( $size == $h{$pixel} ) {
my $bit = $size * 8;
my $time = $end - $start;
my $kbps = int( ( $bit / $time ) / 1000 );
say $fh "$kbps kbit/s";
say $fh "$pixel : $size";
}
else {
say $fh "error occured:";
say $fh "file_size is $size - file_size expected $h{$pixel}";
}
}
say $fh "";
close $fh;
Upvotes: 3