views:

216

answers:

2

Does anybody know a module to test the speed of internet-connection?

+5  A: 

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:

  1. You can try to leverage ibmonitor

  2. 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.

DVK
+2  A: 
#!/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;
sid_com
`$ten` is a poor name for that variable.
Brad Gilbert
It's not easy - when I try to find good variable-names they mostly grow to much in length.
sid_com
It's really easy. That's not really $ten, it's $file_size, or something similar. Think about what it represents, not what it is.
brian d foy
Then I think I should be $test_file_size.
sid_com
Hope I can keep the upvote.
sid_com