C# MongoDb中MongoServer.Create()和mongoClient.GetServer()之间的性能差异
考虑以下代码:C# MongoDb中MongoServer.Create()和mongoClient.GetServer()之间的性能差异,c#,performance,mongodb,C#,Performance,Mongodb,考虑以下代码: MongoServer mongo = MongoServer.Create(); mongo.Connect(); Console.WriteLine("Connected"); Console.WriteLine(); MongoDatabase db = mongo.GetDatabase("tutorial"); Stopwatch stopwatch=new Stopwatch
MongoServer mongo = MongoServer.Create();
mongo.Connect();
Console.WriteLine("Connected");
Console.WriteLine();
MongoDatabase db = mongo.GetDatabase("tutorial");
Stopwatch stopwatch=new Stopwatch();
stopwatch.Start();
using (mongo.RequestStart(db))
{
MongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("books");
for (int i = 0; i < 100000; i++)
{
var nested = new BsonDocument
{
{"name", "John Doe"},
};
collection.Insert(nested);
}
}
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.ReadLine();
在代码上方运行时,输出时间为14225
(在我的电脑上大约为10到14秒)。
为什么我会因为新版本mongoDb上的重构代码而获得这样的性能时间?我缺少了什么?很可能您看到了在使用推荐的连接模式时,新驱动程序中默认启用的写关注点之间的差异(正如您在第二个示例中所做的) 变更发生在2012年11月: 在此更改之前,默认情况下不会确认写入,因此您会看到“更快”的写入 关于C#变化,还有更多细节 如果要尝试使用新样式的连接,可以在测试中禁用数据库连接的WriteConcern:
MongoDatabase db = server.GetDatabase("tutorial", WriteConcern.Unacknowledged);
然后重新运行测试以比较性能。这正是我想要的。使用WriteConcern。未确认集,我获得了相同的性能。谢谢。
MongoDatabase db = server.GetDatabase("tutorial", WriteConcern.Unacknowledged);