Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Net::Ping timing not correct

by gepebril69 (Scribe)
on Feb 21, 2015 at 14:53 UTC ( [id://1117426]=perlquestion: print w/replies, xml ) Need Help??

gepebril69 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there

I want to replace in my script the system call to ping with Net::ping but the response times seem to be incorrect.

used code:

use Net::Ping; use Time::HiRes; $host = "213.155.3.25"; # High precision syntax (requires Time::HiRes) $p = Net::Ping->new(); $p->hires(); ($ret, $duration, $ip) = $p->ping($host, 5.5); printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration) if $ret; $p->close(); exit;

On local machine ping +/- 162ms, via script 1495.149 msec. Me in NL, server in Russia.

If I do the same with a machine in my local network (192.168.1.133)

On local machine ping +/- 1ms, via script 1007.297 msec

Que pasa?

Replies are listed 'Best First'.
Re: Net::Ping timing not correct
by BrowserUk (Patriarch) on Feb 21, 2015 at 15:39 UTC
    Que pasa?

    Take a look inside the module. It doesn't time how long it takes to get a response.

    It times how long it takes to:

    1. Set up a dispatch table to the chosen ping method:

      It does this every time you call the ping method; despite that the method is chosen when you call new().

    2. Plus: how long it takes to call (indirectly) a subroutine that implements the chosen method;
    3. Plus: all the internal housekeeping involved in maintaining the state of the "ping object";

      including setting up and tearing down the sent and received packets and unpacking and validating IPs et la kitchen sink.

    It's like including the building of the stadium, and the opening & closing ceremonies, in the timing of the 100m sprint.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      Just for curiosity, do you think that if he subrtacts from results a previous average time obtained pinging 127.0.0.1 can have a reasonably 'clean' result?

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Hm. Worth a try I suppose, but I wouldn't want to base any conclusions upon the accuracy of the results.

        From what I saw, it looks like the module predates the availability of a HiRes timer and has been (badly) adapted to use it as an afterthought.

        When the granularity of the timing was whole seconds, network speeds were 4MHz for LANs and 56kbps for WANs; timing all the housekeeping probably made little difference to the outcome, but now it just swamps the actual time measured.

        The module needs serious attention. Some of which would be fairly easy. By setting up the appropriate implementation func in new():

        if ($self->{"proto"} eq "udp") # Open a socket ... $self->{func} = \&ping_tcp; } elsif ($self->{"proto"} eq "icmp") { ... $self->{func} = \&ping_icmp; } ...

        Then ping() can be reduced from:

        sub ping { my ($self, $host, # Name or IP number of host to ping $timeout, # Seconds after which ping times out ) = @_; my ($ip, # Packed IP number of $host $ret, # The return value $ping_time, # When ping began ); croak("Usage: \$p->ping(\$host [, \$timeout])") unless @_ == 2 || @_ + == 3; $timeout = $self->{"timeout"} unless $timeout; croak("Timeout must be greater than 0 seconds") if $timeout <= 0; $ip = inet_aton($host); return () unless defined($ip); # Does host exist? # Dispatch to the appropriate routine. $ping_time = &time(); if ($self->{"proto"} eq "external") { $ret = $self->ping_external($ip, $timeout); } elsif ($self->{"proto"} eq "udp") { $ret = $self->ping_udp($ip, $timeout); } elsif ($self->{"proto"} eq "icmp") { $ret = $self->ping_icmp($ip, $timeout); } elsif ($self->{"proto"} eq "tcp") { $ret = $self->ping_tcp($ip, $timeout); } elsif ($self->{"proto"} eq "stream") { $ret = $self->ping_stream($ip, $timeout); } elsif ($self->{"proto"} eq "syn") { $ret = $self->ping_syn($host, $ip, $ping_time, $ping_time+$timeout +); } else { croak("Unknown protocol \"$self->{proto}\" in ping()"); } return wantarray ? ($ret, &time() - $ping_time, $self->ntop($ip)) : +$ret; }

        To this:

        sub ping { my ($self, $host, # Name or IP number of host to ping $timeout, # Seconds after which ping times out ) = @_; my ($ip, # Packed IP number of $host $ret, # The return value $ping_time, # When ping began ); croak("Usage: \$p->ping(\$host [, \$timeout])") unless @_ == 2 || @_ + == 3; $timeout = $self->{"timeout"} unless $timeout; croak("Timeout must be greater than 0 seconds") if $timeout <= 0; $ip = inet_aton($host); return () unless defined($ip); # Does host exist? $ping_time = &time(); $ret = $self->{func}->( $self, $ip, $timeout ); return wantarray ? ($ret, &time() - $ping_time, $self->ntop($ip)) : +$ret; }

        And if each of the implementation funcs did the timing of the critical part and returned it, it could become:

        sub ping { my ($self, $host, # Name or IP number of host to ping $timeout, # Seconds after which ping times out ) = @_; my ($ip, # Packed IP number of $host $ret, # The return value $ping_time, # When ping began ); croak("Usage: \$p->ping(\$host [, \$timeout])") unless @_ == 2 || @_ + == 3; $timeout = $self->{"timeout"} unless $timeout; croak("Timeout must be greater than 0 seconds") if $timeout <= 0; $ip = inet_aton($host); return () unless defined($ip); # Does host exist? ( $ping_time, $ret ) = $self->{func}->( $self, $ip, $timeout ); return wantarray ? ($ret, $ping_time, $self->ntop($ip)) : $ret; }

        But that would take someone with motivation to work out exactly where to start and stop the timers.

        Personally I don't have that motivation. A quick call out to ping.exe satisfies my needs.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: Net::Ping timing not correct
by flexvault (Monsignor) on Feb 21, 2015 at 15:38 UTC

    gepebril69,

    I used your script for both a local server and for the server in Russia with the following results:

    Local Machine: 0.27 ms Russia Server: 160.25 ms

    This was with Version 2.38 of Net::Ping. Hope it helps.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      Thanks,

      maybe it's the version.

      Here: Net::Ping is up to date (2.43).....

        On CPAN there is no version 2.38.....

        I tried to install 2.26 without luck

        On CPAN there is no version 2.38.....

        I tried to install 2.36 without luck

Re: Net::Ping timing not correct
by Discipulus (Canon) on Feb 21, 2015 at 15:47 UTC
    I told you that 'exactly',speaking about time, is big word.. ;=)

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      @Discipulus,

      Yup, you were spot on!

        Thanks for all help!

        Shame that a good module has lost one excellent functionality.

        Change doesn't always mean progress ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1117426]
Approved by Laurent_R
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-19 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found