pinging host measured in micro seconds

Problem

You have a requirement to capture network speed, in microseconds.

The required speed was less than 0.25 of a second (or quarter of second).



Solution

We use PHP to capture the current time in microseconds. Run a system ping command. Capture the new time and minus one from the other.



Example


Here's the PHP code, using the microtime function and a simple ping.

<?php

list($usec, $sec)=
explode(" ",microtime());

$thisDate=sprintf("%f", ($usec+$sec));

$host="$argv[1]";

system("ping -c1 $host > /dev/null 2>&1",$response);

if($response==0) {

list($usec, $sec)=explode(" ",microtime());

$nextDate=sprintf("%f", ($usec+$sec));

printf("%0.4fn",$nextDate-$thisDate);

} else {

print("ping failed to host: $hostn");

}

?>

Here is a run through:

$ php -q pingHost.php bagend
0.0098
$ php -q pingHost.php bagend
0.0066
$ php -q pingHost.php bagend
0.0065
$ php -q pingHost.php bagend
0.0066





Leave a Reply

Your email address will not be published. Required fields are marked *