温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB性能测试代码

发布时间:2020-06-27 23:05:17 来源:网络 阅读:856 作者:ztfriend 栏目:MongoDB数据库

写入100万条记录,建立索引,使用索引连续查询 10万次。

  1. use admin;//连接资料库  
  2. db.auth("mongoAdmin","123456");//登录  
  3. use test;//连接测试库  
  4. db.test_collection.dropIndexes();  
  5. db.test_collection.drop();  
  6. print("insert begin: "+Date());//写入开始时间  
  7. people = ["Marc""Bill""George""Eliot""Matt""Trey""Tracy""Greg""Steve""Kristina""Katie""Jeff"];  
  8. for(var i=10; i<1000000; i++){  
  9.     name = people[Math.floor(Math.random()*people.length)];  
  10.     user_id = i;  
  11.     boolean = [truefalse][Math.floor(Math.random()*2)];  
  12.     added_at = new Date();  
  13.     number = Math.floor(Math.random()*10001);  
  14.     db.test_collection1.save({"name":name, "user_id":user_id, "boolean"boolean"added_at":added_at, "number":number });  
  15. };  
  16. print("insert End: "+Date());//写入结束时间  
  17. db.test_collection.ensureIndex({user_id:1});  
  18. print("find begin: "+Date());//查询开始时间  
  19. var i=0;  
  20. var tempResult=null;  
  21. while(i<100000){  
  22.     i=i+1;  
  23.     tempResult=db.test_collection.findOne({"user_id":Math.floor(Math.random()*1000000)});  
  24. };  
  25. print("find end: " + Date());//查询结束时间  
  26. 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多条。写性能慢不觉得奇怪,毕竟是一台普通的笔记本电脑上配了一个集群呢。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI