Home > Article > Backend Development > nosql - 高并发下Apache+mongodb的php驱动不稳定
我的环境 : Apache/2.2.21 (Win32) PHP/5.4.3
测试代码 :
<?php $db=new Mongo("mongodb://localhost:27017"); $c=$db->selectDB("npm")->selectCollection("user"); $user = $c->findOne(array('userName' => 'admin','passWord'=>'admin')); if($user){ echo var_dump($user); } echo var_dump($db); ?>
使用apache并发测试工具 ab.exe 测试,测试命令如下
D:\Apache2.2\bin>ab.exe -n 100 http://192.168.15.21/a.php
测试结果:
Concurrency Level: 1 Time taken for tests: 0.266 seconds Complete requests: 100 Failed requests: 58 (Connect: 0, Receive: 0, Length: 58, Exceptions: 0) Write errors: 0 Total transferred: 888192 bytes HTML transferred: 870668 bytes Requests per second: 376.47 [#/sec] (mean) Time per request: 2.656 [ms] (mean) Time per request: 2.656 [ms] (mean, across all concurrent requests) Transfer rate: 3265.41 [Kbytes/sec] received
php错误信息:
[13-Jun-2012 07:40:26 UTC] PHP Notice: Unknown: localhost:27017: pool done (0x3877a10) in Unknown on line 0
我的环境 : Apache/2.2.21 (Win32) PHP/5.4.3
测试代码 :
<?php $db=new Mongo("mongodb://localhost:27017"); $c=$db->selectDB("npm")->selectCollection("user"); $user = $c->findOne(array('userName' => 'admin','passWord'=>'admin')); if($user){ echo var_dump($user); } echo var_dump($db); ?>
使用apache并发测试工具 ab.exe 测试,测试命令如下
D:\Apache2.2\bin>ab.exe -n 100 http://192.168.15.21/a.php
测试结果:
Concurrency Level: 1 Time taken for tests: 0.266 seconds Complete requests: 100 Failed requests: 58 (Connect: 0, Receive: 0, Length: 58, Exceptions: 0) Write errors: 0 Total transferred: 888192 bytes HTML transferred: 870668 bytes Requests per second: 376.47 [#/sec] (mean) Time per request: 2.656 [ms] (mean) Time per request: 2.656 [ms] (mean, across all concurrent requests) Transfer rate: 3265.41 [Kbytes/sec] received
php错误信息:
[13-Jun-2012 07:40:26 UTC] PHP Notice: Unknown: localhost:27017: pool done (0x3877a10) in Unknown on line 0
首先建议你设置一下php mongo的pool size,在你初始化Mongo
对象之前
MongoPool::setSize(100);
这样可以提高mongo的连接效率,特别是在大并发的情况下。另外,你的ab命令使用有误
-n 100
,这个数目太小了,一般都要设置在上千次以上-c
参数设置并发数,导致ab是根据总数的极限来压并发,这样并不科学你应该使用
ab.exe -n 1000 -c 100 http://192.168.15.21/a.php
这样是在100的并发下,完成1000次请求。你可以根据自己的情况调整这两个参数。
最后要说的是,有时候出现报错,并不是不稳定,每个服务器都有它的极限,比如你现在用的是windows xp来测试服务器性能,它的硬件,还有软件优化程度(比如半开连接数),都会很大的影响最终性能。
如果你想科学的测试服务器性能,建议使用专门的服务器,并选用经过标准优化了的linux操作系统。
Try the lastest version of mongo php driver
Check this out:
https://github.com/mongodb/mongo-php-...