[CSHARP-4222] General Poor Performance Created: 20/Jun/22 Updated: 27/Oct/23 Resolved: 20/Jul/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Unknown |
| Reporter: | Sebastian Stehle | Assignee: | Dmitry Lukyanov (Inactive) |
| Resolution: | Gone away | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
| Description |
|
I also created a topic here, just fyi: https://www.mongodb.com/community/forums/t/poor-driver-performance/170536 The scenarioI am using .NET Core 6 with the new newest mongodb driver and I am inserting documents with many fields of around 2 MB per document. MongoDB is hosted in docker on the developer machine. The following screenshot is from New Relic:
For each operation I make 3 inserts or updates:
I made two observations: 1. I would expect the performance of (1) and (2) to be almost identical, because the input objects are almost identical, but (2) is almost 50% slower in all cases. I know, that the performance of the Newtonsoft.JSON serializer is better, I have made a few benchmarks in the past, but it does not explain why the difference is so big. I assume it has something to do with the number of fields. I am using the mongo profiler to get information about the queries and I see the following results:
What you can see is the update statements for the snapshot collections sorted by milliseconds in descending order. Even the slowest example has a huge difference to the shown graph above, where the update on the client side takes around 67ms.
I made another test. I insert large documents into the database and compare the performance of the C# side, with Mongo side. For comparison I also make other tests:
If you take the HTTP performance (3ms) and add the MongoDB time (3ms), then it should take 6ms, not 30ms. The test is very simple
If you compare it with MongoDB you see a very big overhead (screenshot shows old result of test program)
My assumption was that it could have been caused by serialization, but it does not make a difference whether I use BsonDocument or a custom class. |
| Comments |
| Comment by PM Bot [ 20/Jul/22 ] | |
|
There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information. | |
| Comment by Dmitry Lukyanov (Inactive) [ 05/Jul/22 ] | |
|
Thanks for your effort,
I'm not sure which time exactly you mean, but if it's about what timings you see in server logs, then yes, it doesn't include time spent on preparation request on the client side. This time you can see in these events "ConnectionSentMessagesEvent"/"ConnectionReceivedMessageEvent" in fields like SerializationDuration/NetworkDuration and etc, see the gist mentioned above for details how to configure it. | |
| Comment by Sebastian Stehle [ 05/Jul/22 ] | |
|
Hi Dimitry,
Let me explain my thoughts again:
So there are two possible explanations for me:
I still don't understand what is going on here. | |
| Comment by Dmitry Lukyanov (Inactive) [ 04/Jul/22 ] | |
|
Hey mail2stehle@gmail.com, thanks for your report I made some initial research and this is what I found for now:
doesn't represent what driver does during serialization since we serialize input entity directly into bytes. See attached gist and Serialize method to see driver's logic. This is why you don't see a difference here: "but it does not make a difference whether I use BsonDocument or a custom class." where ToBson step makes just serialization step similar to what Newtonsoft does without driver specific logic and Full encoding represent timing that happens during all serialization steps. I can confirm that our serialization is a bit slower and we've already considered work to improve it in this ticket, we will consider your finding there. | |
| Comment by Sebastian Stehle [ 22/Jun/22 ] | |
|
Hi James, I don't think that the MongoDB driver is more configurable than Newtonsoft.JSON, but this is another story. For me the main difference in performance of the serializer are probably these two areas:
But this is not what I do not understand: If you take this line:
I compare the full insert time (first column) with BSON serializer performance (4th column). MongoDB reports 3ms in the profiler. So you have 30ms - 6ms - 3ms = 21ms. What are these 21 ms spent on?
| |
| Comment by James Kovacs [ 21/Jun/22 ] | |
|
Thank you for filing this issue. Due to the complexities of serialization in our driver - which are much more configurable than Newtonsoft.JSON - there is going to be a certain amount of per-field overhead. I'm going to spend some time understanding your reproduction later this week to understand if there are areas where we can improve our performance in your scenario. I'll update this ticket with my findings and whether I require any additional input from you. I appreciate your patience as we investigate further. Sincerely, |