Ask Your Question

What is the procedure to benchmark the insertion, reading, and deletion of C# .NET MongoDB using BenchmarkDotNet?

asked 2021-08-26 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-04-13 14:00:00 +0000

nofretete gravatar image

Here are the steps to benchmark the insertion, reading, and deletion of C# .NET MongoDB using BenchmarkDotNet:

  1. First, create a new .NET project in Visual Studio and install the following NuGet packages: MongoDB.Driver and BenchmarkDotNet.

  2. Next, create a new class that will contain the benchmark methods. For example, you can name it MongoDBBenchmark.

  3. Inside the MongoDBBenchmark class, create three public methods: InsertionBenchmark, ReadingBenchmark, and DeletionBenchmark. These methods will contain the code that you want to benchmark.

  4. In each method, create a new instance of MongoClient and use it to connect to your MongoDB database. Then, create a new instance of the MongoCollection class for the respective benchmark operation (insertion, reading, or deletion).

  5. For the InsertionBenchmark method, create a new object to be inserted into the collection and use the InsertOne method to insert it.

  6. For the ReadingBenchmark method, use the Find method to retrieve documents from the collection. Optionally, you can use the Sort and Limit methods to sort and limit the results.

  7. For the DeletionBenchmark method, use the DeleteOne method to delete a document from the collection.

  8. After writing the benchmark methods, annotate them with the [Benchmark] attribute to indicate that they are benchmark methods.

  9. Finally, create a new class that will run the benchmark and invoke the methods. For example, you can create a new class named Program that contains a static Main method. Inside the Main method, create a new instance of the MongoDBBenchmark class and invoke the benchmark methods using the BenchmarkRunner.Run method.

Your code should look something like this:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using MongoDB.Driver;

public class MongoDBBenchmark
    private MongoClient _client;
    private IMongoCollection<MyObject> _collection;

    public void GlobalSetup()
        _client = new MongoClient("mongodb://localhost:27017");
        var database = _client.GetDatabase("test");
        _collection = database.GetCollection<MyObject>("myCollection");

    public void InsertionBenchmark()
        var obj = new MyObject { Id = 1, Name = "Test Object" };

    public void ReadingBenchmark()
        var filter = Builders<MyObject>.Filter.Empty;
        var sort = Builders<MyObject>.Sort.Ascending(obj => obj.Id);
        var limit = 10;
        var results = _collection.Find(filter).Sort(sort).Limit(limit).ToList();

    public void DeletionBenchmark()
        var filter = Builders<MyObject>.Filter.Eq(obj => obj.Id, 1);

class Program
    static void Main(string[] args)

Once you run your program, BenchmarkDotNet will run the benchmarks and produce a report with the results. You can analyze the report to see the performance of your MongoDB CRUD operations.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2021-08-26 11:00:00 +0000

Seen: 7 times

Last updated: Apr 13 '22