之前的测试,发现MongoDB的插入操作在使用PHP驱动时比使用mongo控制台更快。测试发现查询也是类似的情况
(以下测试都使用WT引擎)。
http://blog.chinaunix.net/uid-20726500-id-4998173.html
1. 插入数据
-
-bash-4.1$ cat test.php
-
<?php
-
header("Content-Type:text/html;charset=utf-8");
-
//MongoDB有用户名密码并指定数据库admin
-
$conn = new Mongo("mongodb://127.0.0.1:27017/benchmark");
-
-
$db = $conn->admin;
-
//定制结果集(表名things)
-
$collection = $db->members;
-
//$time1 = xdebug_time_index();
-
-
for($i=1;$i<1000000;$i++){
-
$user = array('uname' => 'chuchuchu_'.$i, 'name' => '褚褚褚', 'password' => 'e10adc3949ba59abbe56e057f20f883e', 'email' => 'dhaig@yahoo.com.cn');
-
$collection->insert($user);
-
}
-
-
$conn->close();
-
//$time2 = xdebug_time_index();
-
//echo "MongoDB响应时间为:".($time2-$time1)."秒";
-
?>
-
-
-bash-4.1$ time php test.php
-
-
-
real 1m45.062s
-
user 0m22.609s
-
sys 0m12.569s
2. 建索引
-
-bash-4.1$ mongo benchmark
-
...
-
> use admin
-
switched to db admin
-
> db.members.ensureIndex({uname:1})
-
{
-
"createdCollectionAutomatically" : false,
-
"numIndexesBefore" : 1,
-
"numIndexesAfter" : 2,
-
"ok" : 1
-
}
3.单点索引查询
-
-bash-4.1$ cat test_sel.php
-
<?php
-
header("Content-Type:text/html;charset=utf-8");
-
$conn = new Mongo("mongodb://127.0.0.1:27017/benchmark");
-
-
$db = $conn->admin;
-
$collection = $db->members;
-
-
for($i=1;$i<1000000;$i++){
-
$query = array( 'uname' => 'chuchuchu_'.$i);
-
$cursor = $collection->find( $query );
-
while( $cursor->hasNext() ) {
-
$var=$cursor->getNext();
-
}
-
}
-
-
$conn->close();
-
-
?>
-
-
-bash-4.1$ time php test_sel.php
-
-
real 1m42.998s
-
user 0m22.888s
-
sys 0m13.160s
top的结果
-
[root@hanode1 ~]# top
-
top - 03:27:45 up 8 days, 8:04, 6 users, load average: 0.35, 0.35, 0.17
-
Tasks: 152 total, 1 running, 151 sleeping, 0 stopped, 0 zombie
-
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
-
Mem: 1019320k total, 952192k used, 67128k free, 155224k buffers
-
Swap: 2064376k total, 184k used, 2064192k free, 332756k cached
-
-
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
-
24809 postgres 20 0 575m 294m 11m S 65.2 29.6 3:23.02 mongod
-
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 先把之前是数据删掉。
-
-bash-4.1$ mongo benchmark
-
...
-
> use admin
-
switched to db admin
-
> db.members.drop()
-
true
4.2 插入数据
-
-bash-4.1$ cat test_2.php
-
<?php
-
header("Content-Type:text/html;charset=utf-8");
-
//MongoDB有用户名密码并指定数据库admin
-
$conn = new Mongo("mongodb://127.0.0.1:27017/benchmark");
-
-
$db = $conn->admin;
-
//定制结果集(表名things)
-
$collection = $db->members;
-
//$time1 = xdebug_time_index();
-
-
for($i=1;$i<1000000;$i++){
-
$user = array('_id' => 'chuchuchu_'.$i, 'name' => '褚褚褚', 'password' => 'e10adc3949ba59abbe56e057f20f883e', 'email' => 'dhaig@yahoo.com.cn');
-
$collection->insert($user);
-
}
-
-
$conn->close();
-
//$time2 = xdebug_time_index();
-
//echo "MongoDB响应时间为:".($time2-$time1)."秒";
-
?>
-
-
-bash-4.1$ time php test_2.php
-
-
-
real 1m43.274s
-
user 0m21.286s
-
sys 0m12.985s
原来
test.php中的'uname'被改成了'_id'
4.3 查询数据
点击(此处)折叠或打开
-
-bash-4.1$ cat test_sel_2.php
-
<?php
-
header("Content-Type:text/html;charset=utf-8");
-
$conn = new Mongo("mongodb://127.0.0.1:27017/benchmark");
-
-
$db = $conn->admin;
-
$collection = $db->members;
-
-
for($i=1;$i<1000000;$i++){
-
$query = array( '_id' => 'chuchuchu_'.$i);
-
$cursor = $collection->find( $query );
-
while( $cursor->hasNext() ) {
-
$var=$cursor->getNext();
-
}
-
}
-
-
$conn->close();
-
-
?>
-
-
-bash-4.1$ time php test_sel_2.php
-
-
real 1m19.557s
-
user 0m21.236s
-
sys 0m12.156s
原来test_sel.php中的'uname'被改成了'_id'
查询top结果
-
[root@hanode1 ~]# top
-
top - 03:40:51 up 8 days, 8:17, 6 users, load average: 0.21, 0.37, 0.28
-
Tasks: 152 total, 2 running, 150 sleeping, 0 stopped, 0 zombie
-
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
-
Mem: 1019320k total, 787364k used, 231956k free, 83380k buffers
-
Swap: 2064376k total, 184k used, 2064192k free, 142384k cached
-
-
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
-
24809 postgres 20 0 711m 420m 11m R 58.2 42.2 6:02.83 mongod
-
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。
阅读(1469) | 评论(0) | 转发(0) |