[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]
- PHP Docs list Function
- PHP Docs sprintf Function
- PHP Docs print Function
- PHP Docs system Function
- PHP Docs explode Function
- PHP Docs microtime Function
[/reference]