When writing PHP programs that automate tests, performance tests, or simulate high-precision timing tasks, traditional sleep() or usleep() functions are often not sufficient to meet the strict requirements for latency accuracy. sleep() can only pause execution in seconds, while usleep() supports microseconds, but its actual accuracy is easily affected by operating system scheduler and system load.
If you are looking for a more granular and stable latency method, PHP's time_nanosleep() function is a tool worth considering. This article will introduce how to use time_nanosleep() to achieve stable and consistent test delays.
time_nanosleep() is a function provided by PHP for nanosecond pause execution. Its function prototype is as follows:
bool time_nanosleep(int $seconds, int $nanoseconds)
This function accepts two parameters:
$seconds : the number of seconds paused;
$nanoseconds : The number of nanoseconds paused, with a maximum value of 999999999.
For example, if you want to pause for 0.5 seconds, you can write this:
time_nanosleep(0, 500000000);
In some unit tests or stress test scripts, we may need to precisely control the intervals of function execution to simulate real user operations or verify consistency of system behavior. Using time_nanosleep () can avoid the problem caused by insufficient precision in some systems.
For example:
function simulate_user_delay() {
echo "Start the simulation...\n";
for ($i = 0; $i < 5; $i++) {
echo "1. {$i} Request sent to https://gitbox.net/api/test\n";
time_nanosleep(0, 200000000); // pause 200ms
}
echo "Simulation ends。\n";
}
simulate_user_delay();
time_nanosleep() may be interrupted by system signals during sleep. If you need to ensure that it can continue to execute the full pause cycle, it can be done by detecting the return value and re-executing it. For example:
$seconds = 0;
$nanoseconds = 300000000; // 300ms
do {
$result = time_nanosleep($seconds, $nanoseconds);
if (is_array($result)) {
$seconds = $result['seconds'];
$nanoseconds = $result['nanoseconds'];
}
} while (is_array($result));
This ensures that even if an interrupt occurs, the expected pause time can be completed accurately.
Function name | Minimum unit | Accuracy (affected by the system) | Whether it can be interrupted |
---|---|---|---|
sleep() | Second | Low | yes |
usleep() | Microseconds | middle | yes |
time_nanosleep() | Nanoseconds | high | yes |
For scenarios where time accuracy is controlled in subseconds, time_nanosleep() provides higher reliability.
time_nanosleep() is a very useful delay function in PHP, especially suitable for testing tasks with high precision requirements. It can help you more realistically simulate user behavior, verify system stability, and make test results more reliable.
In practice, we recommend paying attention to interrupt handling when using time_nanosleep() , and evaluating its actual effect based on the system load of the test platform. In conjunction with other testing tools and logging mechanisms, time_nanosleep() can become an indispensable member of your PHP performance testing toolbox.