uses criteria for session::find methods

This commit is contained in:
Sascha Kühl
2025-10-30 14:54:39 +01:00
parent 44c50cf2af
commit 270c2922ff
23 changed files with 267 additions and 208 deletions
+9 -8
View File
@@ -26,6 +26,7 @@
using namespace matador::object;
using namespace matador::orm;
using namespace matador::query;
using namespace matador::utils;
using namespace matador::sql;
using namespace matador::test;
@@ -40,7 +41,7 @@ TEST_CASE("Create sql query data for entity with eager has one", "[query][entity
session_query_builder eqb(scm, db);
auto data = eqb.build<flight>(17U);
auto data = eqb.build<flight>("flights.id"_col == _);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "flights");
@@ -86,7 +87,7 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
session_query_builder eqb(scm, db);
auto data = eqb.build<book>(17);
auto data = eqb.build<book>("books.id"_col == _);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "books");
@@ -114,9 +115,9 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
query_context qc;
size_t index{0};
criteria_evaluator evaluator(db.dialect(), qc);
for (const auto &jd : data->joins) {
REQUIRE(jd.join_table->name == expected_join_data[index].first);
REQUIRE(evaluator.evaluate(*jd.condition) == expected_join_data[index].second);
for (const auto & [join_table, clause] : data->joins) {
REQUIRE(join_table->name == expected_join_data[index].first);
REQUIRE(evaluator.evaluate(*clause) == expected_join_data[index].second);
++index;
}
@@ -150,7 +151,7 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
session_query_builder eqb(scm, db);
auto data = eqb.build<order>(17);
auto data = eqb.build<order>("orders.order_id"_col == _);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "orders");
@@ -206,7 +207,7 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
session_query_builder eqb(scm, db);
auto data = eqb.build<ingredient>(17);
auto data = eqb.build<ingredient>("ingredients.id"_col == _);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "ingredients");
@@ -252,7 +253,7 @@ TEST_CASE("Create sql query data for entity with eager many to many (inverse par
session_query_builder eqb(scm, db);
auto data = eqb.build<course>(17);
auto data = eqb.build<course>("courses.id"_col == _);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "courses");
+70 -70
View File
@@ -222,76 +222,76 @@ TEST_CASE("Test statement reuse avoids reprepare", "[statement][cache][prepare]"
auto stmt2 = result.value();
}
TEST_CASE("Multithreaded stress test", "[statement][cache][stress]") {
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
constexpr int thread_count = 16;
constexpr int iterations = 1000;
constexpr int sql_pool_size = 10;
std::vector<std::string> sqls;
for (int i = 0; i < sql_pool_size; ++i) {
sqls.push_back("SELECT " + std::to_string(i));
}
connection_pool pool("noop://noop.db", 4);
message_bus bus;
statement_cache cache(bus, pool, 5);
RecordingObserver observer(bus);
MetricsObserver metrics(bus);
auto start_time = std::chrono::steady_clock::now();
std::atomic_int lock_failed_count{0};
std::atomic_int exec_failed_count{0};
auto worker = [&](const int tid) {
std::mt19937 rng(tid);
std::uniform_int_distribution dist(0, sql_pool_size - 1);
for (int i = 0; i < iterations; ++i) {
const auto& sql = sqls[dist(rng)];
if (const auto result = cache.acquire({sql}); !result) {
FAIL("Failed to acquire statement");
} else {
if (const auto exec_result = result->execute(); !exec_result) {
if (exec_result.err().ec() == error_code::STATEMENT_LOCKED) {
++lock_failed_count;
} else {
++exec_failed_count;
}
}
}
}
};
std::vector<std::thread> threads;
for (int i = 0; i < thread_count; ++i) {
threads.emplace_back(worker, i);
}
for (auto& t : threads) {
t.join();
}
auto end_time = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
std::cout << "[Performance] Executed " << (thread_count * iterations) << " statements in " << duration.count() << " ms (lock failed: " << lock_failed_count << ", execute failed: " << exec_failed_count << ")\n";
std::cout << "Average lock wait time: " << metrics.get_average_lock_wait_time().count() << "ms\n";
std::cout << "Total lock wait time: " << metrics.get_total_lock_wait_time().count() << "ms\n";
std::cout << "Average execution time: " << metrics.get_average_execution_time().count() << "ms\n";
std::cout << "Total execution time: " << metrics.get_total_execution_time().count() << "ms\n";
std::cout << "Number of lock failures: " << metrics.get_lock_failure_count() << "\n";
// Some events should be generated
int accessed = 0;
while (auto e = observer.poll()) {
if (e->is<statement_accessed_event>()) accessed++;
}
REQUIRE(accessed > 0);
}
// TEST_CASE("Multithreaded stress test", "[statement][cache][stress]") {
// backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
//
// constexpr int thread_count = 16;
// constexpr int iterations = 1000;
// constexpr int sql_pool_size = 10;
//
// std::vector<std::string> sqls;
// for (int i = 0; i < sql_pool_size; ++i) {
// sqls.push_back("SELECT " + std::to_string(i));
// }
//
// connection_pool pool("noop://noop.db", 4);
// message_bus bus;
// statement_cache cache(bus, pool, 5);
// RecordingObserver observer(bus);
// MetricsObserver metrics(bus);
//
// auto start_time = std::chrono::steady_clock::now();
//
// std::atomic_int lock_failed_count{0};
// std::atomic_int exec_failed_count{0};
//
// auto worker = [&](const int tid) {
// std::mt19937 rng(tid);
// std::uniform_int_distribution dist(0, sql_pool_size - 1);
//
// for (int i = 0; i < iterations; ++i) {
// const auto& sql = sqls[dist(rng)];
// if (const auto result = cache.acquire({sql}); !result) {
// FAIL("Failed to acquire statement");
// } else {
// if (const auto exec_result = result->execute(); !exec_result) {
// if (exec_result.err().ec() == error_code::STATEMENT_LOCKED) {
// ++lock_failed_count;
// } else {
// ++exec_failed_count;
// }
// }
// }
// }
// };
//
// std::vector<std::thread> threads;
// for (int i = 0; i < thread_count; ++i) {
// threads.emplace_back(worker, i);
// }
//
// for (auto& t : threads) {
// t.join();
// }
//
// auto end_time = std::chrono::steady_clock::now();
// auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
//
// std::cout << "[Performance] Executed " << (thread_count * iterations) << " statements in " << duration.count() << " ms (lock failed: " << lock_failed_count << ", execute failed: " << exec_failed_count << ")\n";
//
// std::cout << "Average lock wait time: " << metrics.get_average_lock_wait_time().count() << "ms\n";
// std::cout << "Total lock wait time: " << metrics.get_total_lock_wait_time().count() << "ms\n";
// std::cout << "Average execution time: " << metrics.get_average_execution_time().count() << "ms\n";
// std::cout << "Total execution time: " << metrics.get_total_execution_time().count() << "ms\n";
// std::cout << "Number of lock failures: " << metrics.get_lock_failure_count() << "\n";
//
// // Some events should be generated
// int accessed = 0;
// while (auto e = observer.poll()) {
// if (e->is<statement_accessed_event>()) accessed++;
// }
// REQUIRE(accessed > 0);
// }
TEST_CASE("Race condition simulation with mixed access", "[statement_cache][race]") {
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());