added message event handling

This commit is contained in:
Sascha Kühl 2025-08-11 16:59:48 +02:00
parent 789be1174b
commit 01d7179604
1 changed files with 55 additions and 51 deletions

View File

@ -22,54 +22,57 @@ using namespace matador::sql;
using namespace matador::query; using namespace matador::query;
using namespace matador::utils; using namespace matador::utils;
// Beispiel für einen Observer, der die Metriken auswertet class MetricsObserver {
// class MetricsObserver : public statement_cache_observer_interface { public:
// public: MetricsObserver(message_bus &bus) {
// void on_event(const statement_cache_event& evt) override {
// std::lock_guard<std::mutex> lock(mutex_); }
// void on_event(const statement_cache_event& evt) override {
// switch (evt.type) { std::lock_guard<std::mutex> lock(mutex_);
// case statement_cache_event::Type::LockFailed:
// lock_failure_count_++; switch (evt.type) {
// if (evt.duration) { case statement_cache_event::Type::LockFailed:
// total_lock_wait_time_ += evt.duration.value(); lock_failure_count_++;
// } if (evt.duration) {
// break; total_lock_wait_time_ += evt.duration.value();
// }
// case statement_cache_event::Type::ExecutionEnded: break;
// execution_count_++;
// if (evt.duration) { case statement_cache_event::Type::ExecutionEnded:
// total_execution_time_ += evt.duration.value(); execution_count_++;
// } if (evt.duration) {
// break; total_execution_time_ += evt.duration.value();
// } }
// } break;
// }
// // Metrik-Zugriffsmethoden }
// double get_average_lock_wait_time() const {
// std::lock_guard<std::mutex> lock(mutex_); // Metrik-Zugriffsmethoden
// if (lock_failure_count_ == 0) return 0.0; double get_average_lock_wait_time() const {
// return std::chrono::duration<double>(total_lock_wait_time_).count() / lock_failure_count_; std::lock_guard lock(mutex_);
// } if (lock_failure_count_ == 0) return 0.0;
// return std::chrono::duration<double>(total_lock_wait_time_).count() / lock_failure_count_;
// double get_average_execution_time() const { }
// std::lock_guard<std::mutex> lock(mutex_);
// if (execution_count_ == 0) return 0.0; double get_average_execution_time() const {
// return std::chrono::duration<double>(total_execution_time_).count() / execution_count_; std::lock_guard lock(mutex_);
// } if (execution_count_ == 0) return 0.0;
// return std::chrono::duration<double>(total_execution_time_).count() / execution_count_;
// size_t get_lock_failure_count() const { }
// std::lock_guard<std::mutex> lock(mutex_);
// return lock_failure_count_; size_t get_lock_failure_count() const {
// } std::lock_guard lock(mutex_);
// return lock_failure_count_;
// private: }
// mutable std::mutex mutex_;
// size_t lock_failure_count_{0}; private:
// size_t execution_count_{0}; mutable std::mutex mutex_;
// std::chrono::nanoseconds total_lock_wait_time_{0}; size_t lock_failure_count_{0};
// std::chrono::nanoseconds total_execution_time_{0}; size_t execution_count_{0};
// }; std::chrono::nanoseconds total_lock_wait_time_{0};
std::chrono::nanoseconds total_execution_time_{0};
};
// void example_with_metrics() { // void example_with_metrics() {
// connection_pool pool("noop://noop.db", 4); // connection_pool pool("noop://noop.db", 4);
// statement_cache cache(pool, 5); // statement_cache cache(pool, 5);
@ -304,9 +307,10 @@ TEST_CASE("Race condition simulation with mixed access", "[statement_cache][race
auto task = [&](int /*id*/) { auto task = [&](int /*id*/) {
for (int i = 0; i < operations; ++i) { for (int i = 0; i < operations; ++i) {
auto sql = "SELECT " + std::to_string(i % 10); const auto sql = "SELECT " + std::to_string(i % 10);
auto result = cache.acquire({sql}); if (const auto result = cache.acquire({sql}); !result) {
REQUIRE(result); FAIL("Statement should not be available");
}
// if (i % 50 == 0) { // if (i % 50 == 0) {
// cache.cleanup_expired_connections(); // cache.cleanup_expired_connections();