加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 服务器 > 系统 > 正文

MongoDB性能检测代码

发布时间:2023-10-14 15:12:15 所属栏目:系统 来源:
导读:写入100万条记录,建立索引,使用索引连续查询 10万次。

use admin;//连接资料库

db.auth("mongoAdmin","123456");//登录

use test;//连接测试库

db.test_collection.dropIndexes();

db
写入100万条记录,建立索引,使用索引连续查询 10万次。
 
use admin;//连接资料库  
 
db.auth("mongoAdmin","123456");//登录  
 
use test;//连接测试库  
 
db.test_collection.dropIndexes();  
 
db.test_collection.drop();  
 
print("insert begin: "+Date());//写入开始时间  
 
people = ["Marc", "Bill", "George", "Eliot", "Matt", "Trey", "Tracy", "Greg", "Steve", "Kristina", "Katie", "Jeff"];  
 
for(var i=10; i<1000000; i++){  
 
    name = people[Math.floor(Math.random()*people.length)];  
 
    user_id = i;  
 
    boolean = [true, false][Math.floor(Math.random()*2)];  
 
    added_at = new Date();  
 
    number = Math.floor(Math.random()*10001);  
 
    db.test_collection1.save({"name":name, "user_id":user_id, "boolean": boolean, "added_at":added_at, "number":number });  
 
};  
 
print("insert End: "+Date());//写入结束时间  
 
db.test_collection.ensureIndex({user_id:1});  
 
print("find begin: "+Date());//查询开始时间  
 
var i=0;  
 
var tempResult=null;  
 
while(i<100000){  
 
    i=i+1;  
 
    tempResult=db.test_collection.findOne({"user_id":Math.floor(Math.random()*1000000)});  
 
};  
 
print("find end: " + Date());//查询结束时间  
 
print("game over");//最后一行,保证上一行执行  
 
参照《10分钟配置MongoDB集群》 ,本人笔记本电脑(4G内存)上性能测试结果如下:
 
insert begin: Mon Dec 24 2012 15:06:26 GMT+0800
 
  insert End: Mon Dec 24 2012 15:10:14 GMT+0800
 
find begin: Mon Dec 24 2012 15:10:14 GMT+0800
 
  find end: Mon Dec 24 2012 15:10:37 GMT+0800
 
大概就是平均一秒钟写入400多条,创建索引速度极快,使用索引平均每秒查询3500多条。写性能慢不觉得奇怪,毕竟是一台普通的笔记本电脑上配了一个集群呢。
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章