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