Initially I ran into a big performance difference with the C# Mongodb driver running on my local windows 11 and with the same data set and code running in a Linux docker image on a Linux box.
Some difference would be expected because of the difference in performance on the hosts but as the query returned more rows the performance became unexpectable.
For exploration into the performance I condensed the query code down to python and a compatible c# version to compare performance on various hosts but no Docker this time.
Here’s the results as simple timed console outputs where I run both code sets on windows and linux
The performance drop of C# on Linux is crushing and this is the query and data set that our actual application would be using.
The collection being used is on Atlas and because of the code similarities and the way I’ve run it in various scenarios I cannot see what else could be causing the performance issue other than the C# mongo driver when built for linux via visual studio 2022. I’m using .net8.0
If anyone has any advice on something to help identify what might be going on here I’d be very happy to hear it and try it out.
Thank you
Here’s the python version (sorry about the code formatting the fencing went wonky and I couldn’t change it)
`````from pymongo import MongoClient
from datetime import datetime, timezone
from bson.objectid import ObjectId
import timeit
import pprint
print('hello')
CONNECTION_STRING = ''
client = None
db = None
coll = None
cartIds = [
ObjectId("657aac82c5505fd69c69cdb8"),
ObjectId("657aad7dc5505fd69c69cdb9"),
ObjectId("657aaddbc5505fd69c69cdba"),
ObjectId("657aaf0dc5505fd69c69cdbb"),
ObjectId("657ab0dbc5505fd69c69cdbc"),
ObjectId("657abccec5505fd69c69cdbd"),
ObjectId("657abe3ec5505fd69c69cdbe"),
ObjectId("657ac76bc5505fd69c69cdbf"),
ObjectId("657ac88ec5505fd69c69cdc0"),
ObjectId("657ac90611075a550153fd74"),
ObjectId("65c1129dd514fb5eeb002aad"),
ObjectId("65c15922d1666792496206ae"),
ObjectId("65e001f8ef75fac698b5774a")
];
def startup():
global client
global db
global coll
client = MongoClient(CONNECTION_STRING)
db = client['']
coll = db['']
taken = timeit.timeit(startup, number=1)
print('Startup: ' + str(taken))
count = 0
def query():
global count
items = coll.find(
{
'$and': [
{'Eid': { '$in': cartIds }},
{'Data.Ts': { '$gte': datetime(2024, 3, 15, tzinfo=timezone.utc) }},
{'Data.Ts': { '$lt': datetime(2024, 4, 1, tzinfo=timezone.utc) }}
]
},
{
'Data.Lcn':1,
'Eid':1,
'Data.Flags':1,
'Data.Rnd.Diff':1,
'_id':0
}
)#.explain()
#pprint.pp(items)
for item in items:
if (item['Eid'] == ''):
count = count+2
count = count + 1
for i in range(3):
count = 0
taken = timeit.timeit(query, number=1)
print('Count: ' + str(count) + ' in ' + str(taken))
`
and for C#
`class Program
{
private static string connectionString = ""; // Set your connection string here
private static MongoClient client = null!;
private static IMongoDatabase db = null!;
private static IMongoCollection<RawBsonDocument> coll = null!;
private static List<ObjectId> cartIds = new List<ObjectId>
{
new ObjectId("657aac82c5505fd69c69cdb8"),
new ObjectId("657aad7dc5505fd69c69cdb9"),
new ObjectId("657aaddbc5505fd69c69cdba"),
new ObjectId("657aaf0dc5505fd69c69cdbb"),
new ObjectId("657ab0dbc5505fd69c69cdbc"),
new ObjectId("657abccec5505fd69c69cdbd"),
new ObjectId("657abe3ec5505fd69c69cdbe"),
new ObjectId("657ac76bc5505fd69c69cdbf"),
new ObjectId("657ac88ec5505fd69c69cdc0"),
new ObjectId("657ac90611075a550153fd74"),
new ObjectId("65c1129dd514fb5eeb002aad"),
new ObjectId("65c15922d1666792496206ae"),
new ObjectId("65e001f8ef75fac698b5774a")
};
static void Main(string[] args)
{
Console.WriteLine("hello");
Stopwatch stopwatch = Stopwatch.StartNew();
Startup();
Console.WriteLine($"Startup: {stopwatch.Elapsed.TotalSeconds}");
for (int i = 0; i < 3; i++)
{
stopwatch.Restart();
int count = Query();
stopwatch.Stop();
Console.WriteLine($"Count: {count} in {stopwatch.Elapsed.TotalSeconds}");
}
}
private static void Startup()
{
client = new MongoClient(connectionString);
db = client.GetDatabase("");
coll = db.GetCollection<RawBsonDocument>("");
}
private static int Query()
{
int count = 0;
var filter = Builders<RawBsonDocument>.Filter.And(
Builders<RawBsonDocument>.Filter.In("Eid", cartIds),
Builders<RawBsonDocument>.Filter.Gte("Data.Ts", new DateTime(2024, 3, 15, 0, 0, 0, DateTimeKind.Utc)),
Builders<RawBsonDocument>.Filter.Lt("Data.Ts", new DateTime(2024, 4, 1, 0, 0, 0, DateTimeKind.Utc))
);
var projection = Builders<RawBsonDocument>.Projection.Include("Data.Lcn")
.Include("Eid")
.Include("Data.Flags")
.Include("Data.Rnd.Diff")
.Exclude("_id");
var items = coll.Find(filter).Project(projection).ToEnumerable();
foreach (var item in items)
{
// Just a reundant thing to make sure stuff is created
if (item.GetValue("Eid").Equals(""))
{
count += 2;
}
count++;
}
return count;
}
}`

