4

I'm trying to test the performance of MongoDB before actually putting it to use. I'm trying to see how many documents can I update per second. I'm using C# (Mono + Ubuntu) MongoDB Driver v1.9 and MongoDB v2.4.6.

I believe one of the most effective MongoDB parameters on write performance is Write Concern. As it is stated in documentation, the most relaxed value for write concern would be -1, then 0 and finally 1 is the slowest one.

After searching I found that I can set write concerns in C# embedded in connection string like this:

var client = new MongoClient("mongodb://localhost/?w=-1");

Here are the results of me playing around with different values for w:

  1. Fastest results are achieved when I set w to 1!
  2. Setting w=0 is slower than w=1 28 times!
  3. w=-1 will lead to an exception thrown with error message W value must be greater than or equal to zero!

Does anyone have any explanations on these results? Am I doing anything wrong?

[UPDATE]

I think it is necessary to set the test procedure maybe there's something hidden within it. So here it goes:

I've got a database with 100M documents in a single collection. Each document is created like this using mongo shell:

{ "counter" : 0, "last_update" : new Date() }

Here's the output of db.collection.stats();:

{
    "ns" : "test.collection",
    "count" : 100000100,
    "size" : 6400006560,
    "avgObjSize" : 64.0000015999984,
    "storageSize" : 8683839472,
    "numExtents" : 27,
    "nindexes" : 2,
    "lastExtentSize" : 2146426864,
    "paddingFactor" : 1,
    "systemFlags" : 1,
    "userFlags" : 0,
    "totalIndexSize" : 5769582448,
    "indexSizes" : {
        "_id_" : 3251652432,
        "last_update_1" : 2517930016
    },
    "ok" : 1
}

Using Mono 3.2.1 in Ubuntu 12.04 I've written a C# project which connects to MongoDB and tries to update the documents like this:

FindAndModifyArgs args = new FindAndModifyArgs();
args.SortBy = SortBy.Ascending("last_update");
args.Update = Update<Entity>.Set(e => e.last_update, DateTime.Now);
args.Fields = Fields.Include(new string[] { "counter", "_id" });
var m = collection.FindAndModify(args);

Entity ent = m.GetModifiedDocumentAs<Entity>();
var query = Query<Entity>.EQ(e => e.Id, ent.Id);
var update = Update<Entity>.Set(e => e.counter, ent.counter+1);
collection.Update(query, update);

To summarize what this piece of code does; it selects the oldest last_update and while it sets the last_update to current date, it also increments its counter (update happens in two steps).

I ran this code 10k for each of four different types of Write Concerns, w=-1, w=0, w=1 and w=1&j=true. While w=-1 throws an exception and gives out no results, here are the results for the rest of them:

enter image description here

Since the figure is a little hard to read, here're the same results in numbers:

            w=-1    w=0                 w=1                 w=1&j=true
Average     N/A     244.0948611492      7064.5143923477     238.1846428156
STDEV       N/A     1.7787457992        511.892765742       21.0230097306

And the question is: Does anyone have any explanations why w=0 is much slower than w=1 and why w=-1 is not supported?

[UPDATE]

I've also tested RequestStart in my code like this:

using (server.RequestStart(database)) {
    FindAndModifyArgs args = new FindAndModifyArgs();
    args.SortBy = SortBy.Ascending("last_update");
    args.Update = Update<Entity>.Set(e => e.last_update, DateTime.Now);
    args.Fields = Fields.Include(new string[] { "counter", "_id" });
    var m = collection.FindAndModify(args);

    Entity ent = m.GetModifiedDocumentAs<Entity>();
    var query = Query<Entity>.EQ(e => e.Id, ent.Id);
    var update = Update<Entity>.Set(e => e.counter, ent.counter+1);
    collection.Update(query, update);
}

It had no significant effect on any of the results, what so ever.

4

1 回答 1

0

要解决该w=-1错误,您可能需要直接使用该WriteConcern.Unacknowledged属性

您的测试代码可能会遇到一致性问题。查看RequestStart/RequestDone将查询放到同一个连接上。根据文档:

可能需要这样做的一个示例是,当 WriteConcern 为 w=0 时快速连续调用一系列插入,并且您想在此后立即以一致的方式查询该数据(WriteConcern 为 w=0,写入可以在服务器上排队,并且可能不会立即对其他连接可见

Aphyr 写了一篇很好的博客文章,介绍了 MongoDB 写入关注的各种设置如何影响其一致性近似。它可以帮助您解决此问题。

最后,您可以在build.ps1中找到您的驱动程序版本

于 2013-10-04T18:51:42.227 回答