简体   繁体   中英

Is there a Perl module to test internet connection speed?

有人知道测试互联网连接速度的模块吗?

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 (eg 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.

#!/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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM