我设置了两次Rails应用程序。一种使用MongoDB(Mongoid作为映射器),另一种使用MySQL和ActiveRecord。然后,我编写了一个rake任务,该任务将一些测试数据插入两个数据库(100.000个条目)。
我使用ruby Benchmark模块测量了每个数据库需要花费多长时间。我用100和10.000个条目进行了一些测试,其中mongodb总是比mysql快(大约1/3)。奇怪的是,在mongodb中插入100.000条目所花的时间比使用mysql长大约3倍。我不知道为什么mongodb有这种行为?我唯一知道的是cpu时间比总时间要低得多。 mongodb插入数据时是否有可能启动某种垃圾回收?刚开始时速度很快,但是随着插入更多数据mongodb,它变得越来越慢...对此有任何想法吗?
为了以某种方式获得这两个数据库的读取性能,我考虑过测量该数据库获得搜索查询并响应结果的时间。因为我需要一些精确的度量,所以我不想包括Rails处理从控制器到数据库的查询的时间。
如何直接在数据库而不是在Rails控制器中进行测量?有什么宝石/工具可以帮助我吗?
提前致谢!
编辑:根据我的当前情况更新了我的问题
最佳答案
如果您的基本目标是衡量数据库级别的数据库性能时间,那么我建议您熟悉MongoDB中的benchRun方法。
要执行您想做的事情,可以从链接页面上的示例开始,这是一个带有说明的变体:
// skipped dropping the table and reinitializing as I'm assuming you have your test dataset
// your database is called test and collection is foo in this code
ops = [
// this sets up an array of operations benchRun will run
{
// possible operations include find (added in 2.1), findOne, update, insert, delete, etc.
op : "find" ,
// your db.collection
ns : "test.foo" ,
// different operations have different query options - this matches based on _id
// using a random value between 0 and 100 each time
query : { _id : { "#RAND_INT" : [ 0 , 100 ] } }
}
]
for ( x = 1; x<=128; x*=2){
// actual call to benchRun, each time using different number of threads
res = benchRun( { parallel : x , // number of threads to run in parallel
seconds : 5 , // duration of run; can be fractional seconds
ops : ops // array of operations to run (see above)
} )
// res is a json object returned, easiest way to see everything in it:
printjson( res )
print( "threads: " + x + "\t queries/sec: " + res.query )
}
如果将其放在一个名为testing.js的文件中,则可以从mongo shell运行,如下所示:
> load("testing.js")
{
"note" : "values per second",
"errCount" : NumberLong(0),
"trapped" : "error: not implemented",
"queryLatencyAverageMs" : 69.3567923734754,
"insert" : 0,
"query" : 12839.4,
"update" : 0,
"delete" : 0,
"getmore" : 0,
"command" : 128.4
}
threads: 1 queries/sec: 12839.4
等等。