没有不值得去解决的问题,也没有不值得去学习的技术!

Simulate Concurrent HTTP Requests with Alibaba Cloud PTS

Viewing Performance Testing PTS / Report Details, the total number of requests is 49,000

1. I decided to simulate the scenario of using file_put_contents() to simultaneously write to a file at the same path in the test environment, and then observe whether reading the file content would return empty.

require.php

PHP
<?php
 
$i = rand();
$path = 'return.php';
$contents = [
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
    'g',
    'h',
    'i',
    'j'
];
 
if (file_exists($path)) {
    $read = file_get_contents($path);
    if (empty($read)) {
        file_put_contents('empty-' . rand() . '.php', $read, LOCK_EX);
    }
}
$code = '<?php';
$code .= "\n\n";
$contents = array_merge($contents, [$i]);
$code .= 'return ' . var_export($contents, true) . ';';
file_put_contents($path, $code, LOCK_EX);
 
?>

2. Since this is only for temporary use, I selected Pay-As-You-Go Resource Pack as the product type and Trial Edition as the resource pack type, allowing up to 1,000 virtual users to be simulated simultaneously. See Figure 1.

Since this is only for temporary use, I selected Pay-As-You-Go Resource Pack as the product type and Trial Edition as the resource pack type, allowing up to 1,000 virtual users to be simulated simultaneously
Figure 1

3. I created a test scenario, selecting Concurrent Mode (Virtual User Mode) as the stress mode, Manual Speed Control as the ramp-up mode, a maximum concurrency of 10, and a total stress testing duration of 2 minutes, with an estimated consumption of 1000 VUM. See Figure 2.

Created a test scenario, selected Concurrent Mode (Virtual User Mode) as the stress mode, Manual Speed Control as the ramp-up mode, a maximum concurrency of 10, and a total stress testing duration of 2 minutes, with an estimated consumption of 1000 VUM
Figure 2

4. For the API test, I selected the scenario just created under Performance Testing PTS / Stress Testing. See Figure 3.

API test, selected the scenario just created under Performance Testing PTS / Stress Testing
Figure 3

5. Viewing Performance Testing PTS / Report Details, the total number of requests is 49,000. See Figure 4.

Viewing Performance Testing PTS / Report Details, the total number of requests is 49,000
Figure 4

6. Checking the program execution results, I confirmed that over 20,000 files were generated, specifically: return.php, and a large number of files starting with empty-. See Figure 5.

Checking the program execution results, confirmed that over 20,000 files were generated, specifically: return.php, and a large number of files starting with empty-
Figure 5

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系