Simulating slow response is a common and necessary means when performing interface performance testing. Whether it is to test the fault-tolerant processing mechanism of the front-end or the stability of the back-end service in the case of network jitter or high latency, a controllable "slow service" can greatly improve the authenticity and comprehensiveness of the test. In PHP, we can accurately control the delayed execution of scripts through the built-in function time_nanosleep , thereby easily building a slow response interface. This article will introduce in detail how to use this function to build a test service that can adjust the response time.
The time_nanosleep(int $seconds, int $nanoseconds): The array|bool function can make the script pause the specified time, and the units are accurate to the nanosecond level. Compared with sleep() and usleep() , time_nanosleep has higher time accuracy and is suitable for scenarios with stricter latency requirements.
// Example:pause1Second500毫Second
time_nanosleep(1, 500000000); // 1Second + 0.5Second = 1.5Second
Let’s take the example of building a simple PHP interface, where users can control the delay response time of the interface by passing parameters:
<?php
// slow-response.php
// Set return to JSON Format
header('Content-Type: application/json');
// Get the delay time parameters passed by the user,单位为毫Second
$delay = isset($_GET['delay']) ? (int)$_GET['delay'] : 0;
// 将毫Second转换为Second和纳Second
$seconds = floor($delay / 1000);
$nanoseconds = ($delay % 1000) * 1000000;
// Limit maximum delay,Prevent abuse(For example, maximum delay10Second)
$maxDelay = 10000;
if ($delay > $maxDelay) {
http_response_code(400);
echo json_encode([
'error' => 'Delay too long. Maximum allowed is ' . $maxDelay . ' ms.'
]);
exit;
}
// Execution delay
time_nanosleep($seconds, $nanoseconds);
// Construct return data
$response = [
'status' => 'success',
'requested_delay_ms' => $delay,
'timestamp' => time(),
];
// Return data
echo json_encode($response);
After we deploy the script, we can test it in the following ways:
curl "https://api.gitbox.net/slow-response.php?delay=3000"
The above command will invoke the interface and let the server wait for 3000 milliseconds (3 seconds) before responding to the result. This interface can be easily integrated into your automated test scripts to simulate various delay scenarios.
Front-end performance test : Verify whether the loading animation, timeout prompt, etc. of the front-end when the interface is delayed is normal.
Back-end fault tolerance testing : Test whether the timeout time can be correctly set and downgraded through slow service testing.
Network environment simulation : Used together with proxy servers or traffic control tools (such as TC) to simulate more realistic network jitter and latency.
Resource Control : Exposure of similar test interfaces should be avoided in production environments. It is recommended to use permission control or deploy to a standalone test environment.
Exception handling : Pay attention to exception handling during high concurrency testing. If concurrency exceeds the service processing capacity, it may cause system resource bottlenecks.
System Accuracy Limitation : Although time_nanosleep supports nanoseconds, the actual accuracy is limited by the operating system scheduling mechanism and is usually more reliable at the millisecond level.
Building a slow response service through time_nanosleep is a simple and effective interface performance testing method. It does not rely on external tools and can be achieved using native PHP, which provides great convenience for scenarios such as test automation, performance tuning and fault injection. I hope this article can help you build a stable and robust interface system more efficiently.