added more metrics to statement_cache
This commit is contained in:
parent
4f017ac1de
commit
2e2fcd01b6
|
|
@ -40,16 +40,23 @@ public:
|
|||
"Failed to execute statement because it is already in use"
|
||||
});
|
||||
}
|
||||
metrics.lock_acquired = std::chrono::steady_clock::now();
|
||||
// publish
|
||||
|
||||
auto guard = statement_guard(*this);
|
||||
if (auto conn = pool_.acquire(connection_id_); !conn.valid()) {
|
||||
if (const auto conn = pool_.acquire(connection_id_); !conn.valid()) {
|
||||
return utils::failure(utils::error{
|
||||
error_code::EXECUTE_FAILED,
|
||||
"Failed to execute statement because couldn't lock connection"
|
||||
});
|
||||
}
|
||||
|
||||
return statement_->execute(bindings);
|
||||
metrics.execution_start = std::chrono::steady_clock::now();
|
||||
auto execution_result = statement_->execute(bindings);
|
||||
metrics.execution_end = std::chrono::steady_clock::now();
|
||||
|
||||
// publish
|
||||
return execution_result;
|
||||
});
|
||||
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -22,6 +22,71 @@ 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};
|
||||
// };
|
||||
// void example_with_metrics() {
|
||||
// connection_pool pool("noop://noop.db", 4);
|
||||
// statement_cache cache(pool, 5);
|
||||
//
|
||||
// // Metriken-Observer hinzufügen
|
||||
// auto metrics = std::make_shared<MetricsObserver>();
|
||||
// cache.subscribe(*metrics);
|
||||
//
|
||||
// // Nach der Ausführung der Statements
|
||||
// std::cout << "Durchschnittliche Lock-Wartezeit: "
|
||||
// << metrics->get_average_lock_wait_time() << " s\n";
|
||||
// std::cout << "Durchschnittliche Ausführungszeit: "
|
||||
// << metrics->get_average_execution_time() << " s\n";
|
||||
// std::cout << "Anzahl der Lock-Failures: "
|
||||
// << metrics->get_lock_failure_count() << "\n";
|
||||
// }
|
||||
|
||||
class RecordingObserver final {
|
||||
public:
|
||||
explicit RecordingObserver(message_bus &bus) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue