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