Optimal way to query time range

Hi all,

Thank you in advance for replying to this thread.
I’m currently implementing a program that capture the time taken for find operation of a time range. However, I’m not sure if the snippets of code below are considered as a proper representation of MongoDB’s performance.

It would be great if I could get some pointer on how to best iterate over a cursor and capture its execution time with performance in mind.

EX 1: Using distance as an attempt to execute all elements in cursor

// Create document
start = std::chrono::high_resolution_clock::now();
auto currDocument = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("timestamp",
bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("$gte", bsoncxx::types::b_date(std::chrono::seconds(queryStartTime))), bsoncxx::builder::basic::kvp("$lte", bsoncxx::types::b_date(std::chrono::seconds(queryEndTime))))));

// Dispatch query
mongocxx::cursor cursor = mongoCollection.find(currDocument.view());

std::distance(cursor.begin(), cursor.end());
end = std::chrono::high_resolution_clock::now();

EX 2: Loop over all elements in cursor without doing anything. Note that it is on-purpose not to perform to_json here since that would negatively impact the performance.
`
// Create document
start = std::chrono::high_resolution_clock::now();
auto currDocument = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp(“timestamp”,
bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp(“$gte”, bsoncxx::types::b_date(std::chrono::seconds(queryStartTime))), bsoncxx::builder::basic::kvp(“$lte”, bsoncxx::types::b_date(std::chrono::seconds(queryEndTime))))));

// Dispatch query
mongocxx::cursor cursor = mongoCollection.find(currDocument.view());

for (bsoncxx::document::view currDoc : cursor) { }

end = std::chrono::high_resolution_clock::now();
`

We have a few benchmarks in the C++ driver repo that benchmark the driver - mongo-cxx-driver/benchmark at master · mongodb/mongo-cxx-driver · GitHub

You can take a look at them for reference.

PS: The C++ driver benchmarks are intended to benchmark the driver. If you want to benchmark the server, they can use monitoring events to get durations: example.

Thanks, your resource about monitoring events was very helpful. I was able to validate that the approach in my “EX 2” is more correct since its execution time closely matches with the measurements from monitoring events.
Although, I don’t think it captures the round-trip networking time between client/server.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.