之前的测试,发现MongoDB的插入操作在使用PHP驱动时比使用mongo控制台更快。测试发现查询也是类似的情况(以下测试都使用WT引擎)
http://blog.chinaunix.net/uid-20726500-id-4998173.html

1. 插入数据

点击(此处)折叠或打开

  1. -bash-4.1$ cat test.php
  2. <?php
  3. header("Content-Type:text/html;charset=utf-8");
  4. //MongoDB有用户名密码并指定数据库admin
  5. $conn = new Mongo("mongodb://127.0.0.1:27017/benchmark");
  6.   
  7. $db = $conn->admin;
  8. //定制结果集(表名things)
  9. $collection = $db->members;
  10. //$time1 = xdebug_time_index();

  11. for($i=1;$i<1000000;$i++){
  12. $user = array('uname' => 'chuchuchu_'.$i, 'name' => '褚褚褚', 'password' => 'e10adc3949ba59abbe56e057f20f883e', 'email' => '[email protected]');
  13. $collection->insert($user);
  14. }

  15. $conn->close();
  16. //$time2 = xdebug_time_index();
  17. //echo "MongoDB响应时间为:".($time2-$time1)."秒";
  18. ?>

  19. -bash-4.1$ time php test.php
  20.  

  21. real    1m45.062s
  22. user    0m22.609s
  23. sys    0m12.569s

2. 建索引

点击(此处)折叠或打开

  1. -bash-4.1$ mongo benchmark
  2. ...
  3. > use admin
  4. switched to db admin
  5. > db.members.ensureIndex({uname:1})
  6. {
  7.     "createdCollectionAutomatically" : false,
  8.     "numIndexesBefore" : 1,
  9.     "numIndexesAfter" : 2,
  10.     "ok" : 1
  11. }

3.单点索引查询

点击(此处)折叠或打开

  1. -bash-4.1$ cat test_sel.php
  2. <?php
  3. header("Content-Type:text/html;charset=utf-8");
  4. $conn = new Mongo("mongodb://127.0.0.1:27017/benchmark");

  5. $db = $conn->admin;
  6. $collection = $db->members;

  7. for($i=1;$i<1000000;$i++){
  8. $query = array( 'uname' => 'chuchuchu_'.$i);
  9. $cursor = $collection->find( $query );
  10. while( $cursor->hasNext() ) {
  11.     $var=$cursor->getNext();
  12. }
  13. }

  14. $conn->close();

  15. ?>

  16. -bash-4.1$ time php test_sel.php

  17. real    1m42.998s
  18. user    0m22.888s
  19. sys    0m13.160s

top的结果

点击(此处)折叠或打开

  1. [root@hanode1 ~]# top
  2. top - 03:27:45 up 8 days, 8:04, 6 users, load average: 0.35, 0.35, 0.17
  3. Tasks: 152 total, 1 running, 151 sleeping, 0 stopped, 0 zombie
  4. Cpu(s): 18.7%us, 5.2%sy, 0.0%ni, 74.2%id, 0.0%wa, 0.0%hi, 2.0%si, 0.0%st
  5. Mem: 1019320k total, 952192k used, 67128k free, 155224k buffers
  6. Swap: 2064376k total, 184k used, 2064192k free, 332756k cached
  7. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  8. 24809 postgres 20 0 575m 294m 11m S 65.2 29.6 3:23.02 mongod
  9. 28124 postgres 20 0 208m 7016 4528 S 34.9 0.7 0:02.05 php

索引查询速度大约每秒1w次,mongod消耗CPU 65.2%,经过计算将1个cpu core用满可以达到1.5w/s(1/0.652)。

4. 一级索引的测试

上面是二级索引的测试,那么"_id"的一级索引,性能会不会还有提升空间呢?也测一下看看。

4.1 先把之前是数据删掉。

点击(此处)折叠或打开

  1. -bash-4.1$ mongo benchmark
  2. ...
  3. > use admin
  4. switched to db admin
  5. > db.members.drop()
  6. true

4.2 插入数据

点击(此处)折叠或打开

  1. -bash-4.1$ cat test_2.php
  2. <?php
  3. header("Content-Type:text/html;charset=utf-8");
  4. //MongoDB有用户名密码并指定数据库admin
  5. $conn = new Mongo("mongodb://127.0.0.1:27017/benchmark");
  6.   
  7. $db = $conn->admin;
  8. //定制结果集(表名things)
  9. $collection = $db->members;
  10. //$time1 = xdebug_time_index();

  11. for($i=1;$i<1000000;$i++){
  12. $user = array('_id' => 'chuchuchu_'.$i, 'name' => '褚褚褚', 'password' => 'e10adc3949ba59abbe56e057f20f883e', 'email' => '[email protected]');
  13. $collection->insert($user);
  14. }

  15. $conn->close();
  16. //$time2 = xdebug_time_index();
  17. //echo "MongoDB响应时间为:".($time2-$time1)."秒";
  18. ?>

  19. -bash-4.1$ time php test_2.php
  20.  

  21. real    1m43.274s
  22. user    0m21.286s
  23. sys    0m12.985s
原来test.php中的'uname'被改成了'_id'


4.3 查询数据

点击(此处)折叠或打开
  1. -bash-4.1$ cat test_sel_2.php
  2. <?php
  3. header("Content-Type:text/html;charset=utf-8");
  4. $conn = new Mongo("mongodb://127.0.0.1:27017/benchmark");

  5. $db = $conn->admin;
  6. $collection = $db->members;

  7. for($i=1;$i<1000000;$i++){
  8. $query = array( '_id' => 'chuchuchu_'.$i);
  9. $cursor = $collection->find( $query );
  10. while( $cursor->hasNext() ) {
  11.     $var=$cursor->getNext();
  12. }
  13. }

  14. $conn->close();

  15. ?>

  16. -bash-4.1$ time php test_sel_2.php

  17. real    1m19.557s
  18. user    0m21.236s
  19. sys    0m12.156s
原来test_sel.php中的'uname'被改成了'_id'

查询top结果

点击(此处)折叠或打开

  1. [root@hanode1 ~]# top
  2. top - 03:40:51 up 8 days, 8:17, 6 users, load average: 0.21, 0.37, 0.28
  3. Tasks: 152 total, 2 running, 150 sleeping, 0 stopped, 0 zombie
  4. Cpu(s): 17.5%us, 5.5%sy, 0.0%ni, 74.9%id, 0.0%wa, 0.0%hi, 2.1%si, 0.0%st
  5. Mem: 1019320k total, 787364k used, 231956k free, 83380k buffers
  6. Swap: 2064376k total, 184k used, 2064192k free, 142384k cached
  7. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  8. 24809 postgres 20 0 711m 420m 11m R 58.2 42.2 6:02.83 mongod
  9. 29480 postgres 20 0 208m 7016 4528 S 41.6 0.7 0:02.78 php

索引查询速度大约每秒1.26w次,mongod消耗CPU 58.2% ,经过计算将1个cpu core用满可以达到2.2w/s(1/0.582)。

5. 总结

在我的测试环境下,假设mongod 用满1个CPU core,一级索引的单点查询QPS可达到2.2w/s,二级索引则大约是1.5w/s。



01-01 17:09