快速记录请求
我正在尝试测试我的 PHP 脚本,该脚本创建 48 个 cURL 多句柄,每个句柄包含大约 1500 个请求,全部并行执行。我试图确保所有请求均已实际发送,为此,我将请求记录在我的服务器上。这是我的 dump.php,我用我的脚本向它发出请求,它会将请求记录到文件中:
<?php
ob_start();
echo $_SERVER["REQUEST_URI"];
$dump = ob_get_clean();
$log = "dump.txt";
$fh = fopen($log, 'a') or die("can't open file");
fwrite($fh, $dump . "\r\n\r\n");
fclose($fh);
echo "Dump Completed";
?>
但是,我的四核 PC 能够以大约每 10 秒 1500 个,并且很可能使我的服务器过载,因为它尝试同时打开文件。我在发送每个请求之间设置了 0.1 秒的延迟,但我认为我的服务器仍然无法打开文件、转储文本并在下一个请求到达之前关闭它。结果,我的 PHP 文件显示发送了大约 72k 个请求,但我的服务器显示只收到了大约 14k 个请求。这可能是由于我的 PHP 程序使我的 PC 或端口系统超载,并且某些请求从未成功发送,或者我的服务器速度不够快,无法每秒处理 150 个请求。
有什么可能的解决方案来尝试安全地知道我的所有请求都已发送吗?当我的客户使用该脚本时,至少 99.5% 的请求必须实际发送,并且他的服务器足够强大,可以毫无问题地处理该请求。出于安全原因,我无法在他的服务器上进行测试,但我的服务器不够强大,无法模拟工作条件。
I'm trying to test my PHP script that creates 48 cURL multi handles, each holding about 1500 requests, all executed in parallel. I'm trying to make sure that all requests are actually sent, and to do so, I'm logging the requests on my server. Here is my dump.php, which I make requests to, with my script, and it will log the request to a file:
<?php
ob_start();
echo $_SERVER["REQUEST_URI"];
$dump = ob_get_clean();
$log = "dump.txt";
$fh = fopen($log, 'a') or die("can't open file");
fwrite($fh, $dump . "\r\n\r\n");
fclose($fh);
echo "Dump Completed";
?>
However, there is a slight problem of my quad core PC being able to fire off these requests at rate of about 1500 every 10 seconds, and most likely overloading my server as it tries to open the file at the same time. I've put a delay of 0.1 seconds between the sending of each request, but my server, I think still cannot open the file, dump the text, and close it before the next request arrives. As a result, my PHP file says that about 72k of requests were sent, but my server says that only about 14k were received. This could be due to my PHP program overloading my PC, or the port system, and some requests were never successfully sent, or my server isn't fast enough to handle 150 requests a second.
Any possible solutions to attempt to securely know that all my requests were sent? When my client uses the script, it is imperative that at least 99.5% of the requests are actually sent, and his server is powerful enough to handle that with no problem. I can't test it on his servers for security reasons, yet mine isn't powerful enough for me to simulate the working conditions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理想情况下,我会将这些值写入关键缓存(例如 memcached 实例),然后最终使用单线程脚本写入数据库/平面文件。这将为您提供跟上请求的速度,以及将数据存储到磁盘的能力。
话虽如此,仅使用数据库作为快速解决方法就可以了。
Ideally, i'd write these values to a key cache such as memcached instance and then eventually to a database/flat file with a single-threaded script. This will give you the speed of being able to keep up with requests, and also the ability to store the data to disk.
Having said that, just using a database as a quick workaround would work.
如果您需要有关请求的复杂信息,数据库绝对是最佳选择。
如果您关心的只是计数,我推荐 Membase (http://www.membase.org/downloads) 计数器,使用 Memcache 原子添加操作 (http://php.net/manual/en/memcache.add .php)
A database is definitely the best option here if you need complex information about the requests.
If all you care about is the count, I recommend a Membase (http://www.membase.org/downloads) counter, using the Memcache atomic add operation (http://php.net/manual/en/memcache.add.php)