MongoDb C # driver slow for first request after application start

I am testing MongoDB (3.0) with a C # driver and I noticed something strange.

I started testing db localy and then created a test console application. There I do this to initialize the driver:

var client = new MongoClient();
var database = client.GetDatabase("test");

      

So far so good, but then I tried to get data based on id (indexed by default), so I added the following:

var collection = database.GetCollection<BsonDocument>("myData");
var filter = Builders<BsonDocument>.Filter.Eq("id", "F1234");
var result = collection.Find(filter).ToListAsync().Result;

      

To get the average time consumption, I added some code to measure the time for the request (by decorating it with two DateTime.Nows and then subtracting them - dirty, I know) and put all the code in a loop. Also, I let each display of the iteration time in the console

Now, every time I press F5, the first iteration takes about 150ms, but the next iterations take about 1ms on average. The important thing is that this has nothing to do with the cache, because each iteration can ask for a different ID, and the result is exactly the same. Also, I can re-initialize MongoClient with every iteration, but it always behaves as I described.

Every first request after launching the application takes longer.

I need to know if this is indeed caused by some initialization inside MongoClient (maybe some static classes are initialized with the first request?), Or if this could happen at any time when the application starts up.

Note : I've also tried replacing the Thread.Sleep request to make sure it's not a .NET issue. This is not the case. It really is "collection.Find" and takes longer from the first use.

Thank!

Note after a while

So, after a while, I found out that every CRUD operation needs a "warm-first-request". Sometimes every CRUD operation for every collection. It's strange. I am posting this so others will know :-)

+3


source to share





All Articles