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

[/problem]

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

[/solution]

[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

[/example]

[reference]

[tags]UNIX, Network testing, ping, PHP, microtime, PHP Coding School[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

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