Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
test_performance_monitor.cpp File Reference
#include <gtest/gtest.h>
#include <chrono>
#include <string>
#include <vector>
#include <atomic>
#include <thread>
#include "database/monitoring/performance_monitor.h"
#include "database/monitoring/pool_metrics.h"
Include dependency graph for test_performance_monitor.cpp:

Go to the source code of this file.

Classes

class  PerformanceMonitorQueryTest
 
class  MonitoringEnabledTest
 
class  FilteredSummaryTest
 
class  QueryHistoryTest
 
class  AlertSystemTest
 
class  MetricLifecycleTest
 
class  ConnectionMetricsTest
 
class  MetricsOutputTest
 
class  PerformanceAlertTest
 
class  PoolMetricsTest
 
class  PrometheusExporterTest
 
class  QueryTimerTest
 

Functions

static query_metrics make_query (const std::string &hash, std::chrono::microseconds exec_time, bool success, database_types db_type=database_types::postgres, const std::string &error_msg="")
 
 TEST_F (PerformanceMonitorQueryTest, RecordAndRetrieveSingleQuery)
 
 TEST_F (PerformanceMonitorQueryTest, RecordMultipleQueries)
 
 TEST_F (PerformanceMonitorQueryTest, AvgQueryTimeCalculation)
 
 TEST_F (MonitoringEnabledTest, DisabledDoesNotRecordQueryMetrics)
 
 TEST_F (MonitoringEnabledTest, DisabledDoesNotRecordConnectionMetrics)
 
 TEST_F (MonitoringEnabledTest, DisabledDoesNotUpdateConnectionCount)
 
 TEST_F (MonitoringEnabledTest, ReEnableResumesRecording)
 
 TEST_F (FilteredSummaryTest, PostgresSummaryFiltersCorrectly)
 
 TEST_F (FilteredSummaryTest, SqliteSummaryFiltersCorrectly)
 
 TEST_F (FilteredSummaryTest, UnusedDbTypeReturnsZero)
 
 TEST_F (FilteredSummaryTest, OverallSummaryIncludesAll)
 
 TEST_F (QueryHistoryTest, GetRecentQueriesWithinWindow)
 
 TEST_F (QueryHistoryTest, GetRecentQueriesEmptyWindow)
 
 TEST_F (QueryHistoryTest, GetSlowQueriesAboveThreshold)
 
 TEST_F (QueryHistoryTest, GetSlowQueriesNoneAboveThreshold)
 
 TEST_F (QueryHistoryTest, GetSlowQueriesPreservesMetadata)
 
 TEST_F (AlertSystemTest, SlowQueryTriggersAlert)
 
 TEST_F (AlertSystemTest, RegisterAlertHandlerReceivesCallback)
 
 TEST_F (AlertSystemTest, MultipleHandlersAllCalled)
 
 TEST_F (AlertSystemTest, GetRecentAlertsEmptyByDefault)
 
 TEST_F (AlertSystemTest, RecordSlowQueryEmitsAlert)
 
 TEST_F (AlertSystemTest, ConnectionPoolExhaustionAlert)
 
 TEST_F (MetricLifecycleTest, ClearMetricsRemovesEverything)
 
 TEST_F (MetricLifecycleTest, CleanupOldMetricsRemovesExpired)
 
 TEST_F (MetricLifecycleTest, CleanupKeepsRecentMetrics)
 
 TEST_F (ConnectionMetricsTest, UpdateConnectionCountStoresValues)
 
 TEST_F (ConnectionMetricsTest, GetConnectionMetricsUnknownDbType)
 
 TEST_F (ConnectionMetricsTest, ConnectionSummaryIncludesMultipleTypes)
 
 TEST_F (MetricsOutputTest, GetMetricsJsonContainsFields)
 
 TEST_F (MetricsOutputTest, GetMetricsJsonEmptyMonitor)
 
 TEST_F (PerformanceAlertTest, ConstructionAndAccessors)
 
 TEST_F (PerformanceAlertTest, AllAlertTypes)
 
 TEST_F (PoolMetricsTest, InitialStateIsZero)
 
 TEST_F (PoolMetricsTest, RecordSuccessfulAcquisition)
 
 TEST_F (PoolMetricsTest, RecordFailedAcquisition)
 
 TEST_F (PoolMetricsTest, RecordMultipleAcquisitionsTracksMinMax)
 
 TEST_F (PoolMetricsTest, RecordTimeout)
 
 TEST_F (PoolMetricsTest, UpdateActiveTracksPeak)
 
 TEST_F (PoolMetricsTest, UpdateQueuedTracksPeak)
 
 TEST_F (PoolMetricsTest, RecordHealthCheck)
 
 TEST_F (PoolMetricsTest, AverageWaitTime)
 
 TEST_F (PoolMetricsTest, AverageWaitTimeZeroWhenNoAcquisitions)
 
 TEST_F (PoolMetricsTest, SuccessRate)
 
 TEST_F (PoolMetricsTest, SuccessRateNoAcquisitions)
 
 TEST_F (PoolMetricsTest, ResetClearsStatistics)
 
 TEST_F (PrometheusExporterTest, FormatContainsMetricNames)
 
 TEST_F (PrometheusExporterTest, ExportMetricsReturnsTrue)
 
 TEST_F (PrometheusExporterTest, ExportAlertsReturnsTrue)
 
 TEST_F (QueryTimerTest, DestructorRecordsMetrics)
 
 TEST_F (QueryTimerTest, SetErrorMarksFailure)
 
 TEST_F (QueryTimerTest, SetRowsAffected)
 

Function Documentation

◆ make_query()

static query_metrics make_query ( const std::string & hash,
std::chrono::microseconds exec_time,
bool success,
database_types db_type = database_types::postgres,
const std::string & error_msg = "" )
static

Definition at line 27 of file test_performance_monitor.cpp.

33{
35 m.query_hash = hash;
36 m.start_time = std::chrono::steady_clock::now();
37 m.end_time = m.start_time + exec_time;
38 m.execution_time = exec_time;
39 m.success = success;
40 m.error_message = error_msg;
41 m.db_type = db_type;
42 m.rows_affected = success ? 1 : 0;
43 return m;
44}
Metrics for individual query execution.
std::chrono::steady_clock::time_point start_time
std::chrono::steady_clock::time_point end_time
std::chrono::microseconds execution_time

References database::monitoring::query_metrics::db_type, database::monitoring::query_metrics::end_time, database::monitoring::query_metrics::error_message, database::monitoring::query_metrics::execution_time, database::monitoring::query_metrics::query_hash, database::monitoring::query_metrics::rows_affected, database::monitoring::query_metrics::start_time, database::success, and database::monitoring::query_metrics::success.

Referenced by FilteredSummaryTest::SetUp(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ TEST_F() [1/51]

TEST_F ( AlertSystemTest ,
ConnectionPoolExhaustionAlert  )

Definition at line 346 of file test_performance_monitor.cpp.

346 {
348 cm.total_connections.store(10);
349 cm.active_connections.store(10); // 100% utilization > 90% threshold
350
351 monitor_->record_connection_metrics(database_types::postgres, cm);
352
353 auto alerts = monitor_->get_recent_alerts(std::chrono::minutes(5));
354 bool found_pool_alert = false;
355 for (const auto& alert : alerts) {
356 if (alert.type() == performance_alert::alert_type::connection_pool_exhaustion) {
357 found_pool_alert = true;
358 }
359 }
360 EXPECT_TRUE(found_pool_alert);
361}
Metrics for database connection usage.

References database::monitoring::connection_metrics::active_connections, and database::monitoring::connection_metrics::total_connections.

◆ TEST_F() [2/51]

TEST_F ( AlertSystemTest ,
GetRecentAlertsEmptyByDefault  )

Definition at line 332 of file test_performance_monitor.cpp.

332 {
333 auto alerts = monitor_->get_recent_alerts(std::chrono::minutes(5));
334 EXPECT_TRUE(alerts.empty());
335}

◆ TEST_F() [3/51]

TEST_F ( AlertSystemTest ,
MultipleHandlersAllCalled  )

Definition at line 313 of file test_performance_monitor.cpp.

313 {
314 std::atomic<int> handler1_count{0};
315 std::atomic<int> handler2_count{0};
316
317 monitor_->register_alert_handler([&handler1_count](const performance_alert&) {
318 handler1_count.fetch_add(1);
319 });
320 monitor_->register_alert_handler([&handler2_count](const performance_alert&) {
321 handler2_count.fetch_add(1);
322 });
323
324 monitor_->set_alert_thresholds(0.05, std::chrono::microseconds(10));
325 monitor_->record_query_metrics(
326 make_query("slow", std::chrono::microseconds(1000), true));
327
328 EXPECT_GE(handler1_count.load(), 1);
329 EXPECT_GE(handler2_count.load(), 1);
330}
Alert system for performance thresholds.
static query_metrics make_query(const std::string &hash, std::chrono::microseconds exec_time, bool success, database_types db_type=database_types::postgres, const std::string &error_msg="")

References make_query().

Here is the call graph for this function:

◆ TEST_F() [4/51]

TEST_F ( AlertSystemTest ,
RecordSlowQueryEmitsAlert  )

Definition at line 337 of file test_performance_monitor.cpp.

337 {
338 monitor_->record_slow_query("SELECT * FROM huge_table",
339 std::chrono::microseconds(5000000));
340
341 auto alerts = monitor_->get_recent_alerts(std::chrono::minutes(5));
342 ASSERT_GE(alerts.size(), 1u);
343 EXPECT_EQ(alerts.back().type(), performance_alert::alert_type::slow_query);
344}

◆ TEST_F() [5/51]

TEST_F ( AlertSystemTest ,
RegisterAlertHandlerReceivesCallback  )

Definition at line 299 of file test_performance_monitor.cpp.

299 {
300 std::atomic<int> callback_count{0};
301 monitor_->register_alert_handler([&callback_count](const performance_alert&) {
302 callback_count.fetch_add(1);
303 });
304
305 // Trigger alert via slow query
306 monitor_->set_alert_thresholds(0.05, std::chrono::microseconds(10));
307 monitor_->record_query_metrics(
308 make_query("slow", std::chrono::microseconds(1000), true));
309
310 EXPECT_GE(callback_count.load(), 1);
311}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [6/51]

TEST_F ( AlertSystemTest ,
SlowQueryTriggersAlert  )

Definition at line 279 of file test_performance_monitor.cpp.

279 {
280 // Set low latency threshold so our query triggers an alert
281 monitor_->set_alert_thresholds(0.05, std::chrono::microseconds(100));
282
283 monitor_->record_query_metrics(
284 make_query("slow", std::chrono::microseconds(500), true));
285
286 auto alerts = monitor_->get_recent_alerts(std::chrono::minutes(5));
287 ASSERT_GE(alerts.size(), 1u);
288
289 bool found_slow_query_alert = false;
290 for (const auto& alert : alerts) {
291 if (alert.type() == performance_alert::alert_type::slow_query) {
292 found_slow_query_alert = true;
293 EXPECT_FALSE(alert.message().empty());
294 }
295 }
296 EXPECT_TRUE(found_slow_query_alert);
297}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [7/51]

TEST_F ( ConnectionMetricsTest ,
ConnectionSummaryIncludesMultipleTypes  )

Definition at line 444 of file test_performance_monitor.cpp.

444 {
445 // get_performance_summary() requires at least one query in history
446 // to reach connection metrics aggregation (early return if empty)
447 monitor_->record_query_metrics(
448 make_query("q1", std::chrono::microseconds(100), true));
449
450 monitor_->update_connection_count(database_types::postgres, 3, 10);
451 monitor_->update_connection_count(database_types::sqlite, 2, 5);
452
453 auto summary = monitor_->get_performance_summary();
454 EXPECT_EQ(summary.total_connections, 15u);
455 EXPECT_EQ(summary.active_connections, 5u);
456 EXPECT_DOUBLE_EQ(summary.connection_utilization, 5.0 / 15.0);
457}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [8/51]

TEST_F ( ConnectionMetricsTest ,
GetConnectionMetricsUnknownDbType  )

Definition at line 438 of file test_performance_monitor.cpp.

438 {
439 auto cm = monitor_->get_connection_metrics(database_types::sqlite);
440 EXPECT_EQ(cm.total_connections.load(), 0u);
441 EXPECT_EQ(cm.active_connections.load(), 0u);
442}

◆ TEST_F() [9/51]

TEST_F ( ConnectionMetricsTest ,
UpdateConnectionCountStoresValues  )

Definition at line 430 of file test_performance_monitor.cpp.

430 {
431 monitor_->update_connection_count(database_types::postgres, 5, 10);
432
433 auto cm = monitor_->get_connection_metrics(database_types::postgres);
434 EXPECT_EQ(cm.active_connections.load(), 5u);
435 EXPECT_EQ(cm.total_connections.load(), 10u);
436}

◆ TEST_F() [10/51]

TEST_F ( FilteredSummaryTest ,
OverallSummaryIncludesAll  )

Definition at line 193 of file test_performance_monitor.cpp.

193 {
194 auto summary = monitor_->get_performance_summary();
195 EXPECT_EQ(summary.total_queries, 4u);
196}

◆ TEST_F() [11/51]

TEST_F ( FilteredSummaryTest ,
PostgresSummaryFiltersCorrectly  )

Definition at line 173 of file test_performance_monitor.cpp.

173 {
174 auto summary = monitor_->get_performance_summary(database_types::postgres);
175 EXPECT_EQ(summary.total_queries, 2u);
176 EXPECT_EQ(summary.successful_queries, 2u);
177 EXPECT_EQ(summary.failed_queries, 0u);
178}

◆ TEST_F() [12/51]

TEST_F ( FilteredSummaryTest ,
SqliteSummaryFiltersCorrectly  )

Definition at line 180 of file test_performance_monitor.cpp.

180 {
181 auto summary = monitor_->get_performance_summary(database_types::sqlite);
182 EXPECT_EQ(summary.total_queries, 2u);
183 EXPECT_EQ(summary.successful_queries, 1u);
184 EXPECT_EQ(summary.failed_queries, 1u);
185 EXPECT_DOUBLE_EQ(summary.error_rate, 0.5);
186}

◆ TEST_F() [13/51]

TEST_F ( FilteredSummaryTest ,
UnusedDbTypeReturnsZero  )

Definition at line 188 of file test_performance_monitor.cpp.

188 {
189 auto summary = monitor_->get_performance_summary(database_types::mongodb);
190 EXPECT_EQ(summary.total_queries, 0u);
191}

◆ TEST_F() [14/51]

TEST_F ( MetricLifecycleTest ,
CleanupKeepsRecentMetrics  )

Definition at line 405 of file test_performance_monitor.cpp.

405 {
406 monitor_->set_metrics_retention_period(std::chrono::minutes(60));
407
408 monitor_->record_query_metrics(
409 make_query("recent", std::chrono::microseconds(100), true));
410
411 monitor_->cleanup_old_metrics();
412
413 auto summary = monitor_->get_performance_summary();
414 EXPECT_EQ(summary.total_queries, 1u);
415}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [15/51]

TEST_F ( MetricLifecycleTest ,
CleanupOldMetricsRemovesExpired  )

Definition at line 392 of file test_performance_monitor.cpp.

392 {
393 // Set very short retention period
394 monitor_->set_metrics_retention_period(std::chrono::minutes(0));
395
396 monitor_->record_query_metrics(
397 make_query("old", std::chrono::microseconds(100), true));
398
399 monitor_->cleanup_old_metrics();
400
401 auto summary = monitor_->get_performance_summary();
402 EXPECT_EQ(summary.total_queries, 0u);
403}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [16/51]

TEST_F ( MetricLifecycleTest ,
ClearMetricsRemovesEverything  )

Definition at line 376 of file test_performance_monitor.cpp.

376 {
377 monitor_->record_query_metrics(
378 make_query("q1", std::chrono::microseconds(100), true));
379 monitor_->update_connection_count(database_types::postgres, 5, 10);
380 monitor_->record_slow_query("SELECT 1", std::chrono::microseconds(999999));
381
382 monitor_->clear_metrics();
383
384 auto summary = monitor_->get_performance_summary();
385 EXPECT_EQ(summary.total_queries, 0u);
386 EXPECT_EQ(summary.total_connections, 0u);
387
388 auto alerts = monitor_->get_recent_alerts(std::chrono::minutes(60));
389 EXPECT_TRUE(alerts.empty());
390}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [17/51]

TEST_F ( MetricsOutputTest ,
GetMetricsJsonContainsFields  )

Definition at line 472 of file test_performance_monitor.cpp.

472 {
473 monitor_->record_query_metrics(
474 make_query("q1", std::chrono::microseconds(1000), true));
475
476 std::string json = monitor_->get_metrics_json();
477 EXPECT_NE(json.find("total_queries"), std::string::npos);
478 EXPECT_NE(json.find("successful_queries"), std::string::npos);
479 EXPECT_NE(json.find("failed_queries"), std::string::npos);
480 EXPECT_NE(json.find("avg_query_time_us"), std::string::npos);
481 EXPECT_NE(json.find("error_rate"), std::string::npos);
482 EXPECT_NE(json.find("active_connections"), std::string::npos);
483}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [18/51]

TEST_F ( MetricsOutputTest ,
GetMetricsJsonEmptyMonitor  )

Definition at line 485 of file test_performance_monitor.cpp.

485 {
486 std::string json = monitor_->get_metrics_json();
487 EXPECT_FALSE(json.empty());
488 EXPECT_NE(json.find("total_queries"), std::string::npos);
489}

◆ TEST_F() [19/51]

TEST_F ( MonitoringEnabledTest ,
DisabledDoesNotRecordConnectionMetrics  )

Definition at line 117 of file test_performance_monitor.cpp.

117 {
118 monitor_->set_monitoring_enabled(false);
119
121 cm.total_connections.store(10);
122 cm.active_connections.store(5);
123 monitor_->record_connection_metrics(database_types::postgres, cm);
124
125 auto retrieved = monitor_->get_connection_metrics(database_types::postgres);
126 EXPECT_EQ(retrieved.total_connections.load(), 0u);
127}

References database::monitoring::connection_metrics::active_connections, and database::monitoring::connection_metrics::total_connections.

◆ TEST_F() [20/51]

TEST_F ( MonitoringEnabledTest ,
DisabledDoesNotRecordQueryMetrics  )

Definition at line 108 of file test_performance_monitor.cpp.

108 {
109 monitor_->set_monitoring_enabled(false);
110 monitor_->record_query_metrics(
111 make_query("q1", std::chrono::microseconds(1000), true));
112
113 auto summary = monitor_->get_performance_summary();
114 EXPECT_EQ(summary.total_queries, 0u);
115}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [21/51]

TEST_F ( MonitoringEnabledTest ,
DisabledDoesNotUpdateConnectionCount  )

Definition at line 129 of file test_performance_monitor.cpp.

129 {
130 monitor_->set_monitoring_enabled(false);
131 monitor_->update_connection_count(database_types::sqlite, 5, 10);
132
133 auto retrieved = monitor_->get_connection_metrics(database_types::postgres);
134 EXPECT_EQ(retrieved.total_connections.load(), 0u);
135}

◆ TEST_F() [22/51]

TEST_F ( MonitoringEnabledTest ,
ReEnableResumesRecording  )

Definition at line 137 of file test_performance_monitor.cpp.

137 {
138 monitor_->set_monitoring_enabled(false);
139 monitor_->record_query_metrics(
140 make_query("ignored", std::chrono::microseconds(100), true));
141
142 monitor_->set_monitoring_enabled(true);
143 monitor_->record_query_metrics(
144 make_query("recorded", std::chrono::microseconds(200), true));
145
146 auto summary = monitor_->get_performance_summary();
147 EXPECT_EQ(summary.total_queries, 1u);
148}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [23/51]

TEST_F ( PerformanceAlertTest ,
AllAlertTypes  )

Definition at line 509 of file test_performance_monitor.cpp.

509 {
510 auto now = std::chrono::steady_clock::now();
511 const performance_alert::alert_type types[] = {
512 performance_alert::alert_type::high_latency,
513 performance_alert::alert_type::high_error_rate,
514 performance_alert::alert_type::connection_pool_exhaustion,
515 performance_alert::alert_type::slow_query,
516 performance_alert::alert_type::memory_usage,
517 performance_alert::alert_type::cpu_usage
518 };
519
520 for (auto type : types) {
521 performance_alert alert(type, "test", now);
522 EXPECT_EQ(alert.type(), type);
523 }
524}

References database::monitoring::performance_alert::type().

Here is the call graph for this function:

◆ TEST_F() [24/51]

TEST_F ( PerformanceAlertTest ,
ConstructionAndAccessors  )

Definition at line 497 of file test_performance_monitor.cpp.

497 {
498 auto now = std::chrono::steady_clock::now();
499 performance_alert alert(
500 performance_alert::alert_type::high_latency,
501 "Latency too high",
502 now);
503
504 EXPECT_EQ(alert.type(), performance_alert::alert_type::high_latency);
505 EXPECT_EQ(alert.message(), "Latency too high");
506 EXPECT_EQ(alert.timestamp(), now);
507}

References database::monitoring::performance_alert::message(), database::monitoring::performance_alert::timestamp(), and database::monitoring::performance_alert::type().

Here is the call graph for this function:

◆ TEST_F() [25/51]

TEST_F ( PerformanceMonitorQueryTest ,
AvgQueryTimeCalculation  )

Definition at line 85 of file test_performance_monitor.cpp.

85 {
86 monitor_->record_query_metrics(
87 make_query("q1", std::chrono::microseconds(1000), true));
88 monitor_->record_query_metrics(
89 make_query("q2", std::chrono::microseconds(3000), true));
90
91 auto summary = monitor_->get_performance_summary();
92 EXPECT_EQ(summary.avg_query_time.count(), 2000);
93}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [26/51]

TEST_F ( PerformanceMonitorQueryTest ,
RecordAndRetrieveSingleQuery  )

Definition at line 59 of file test_performance_monitor.cpp.

59 {
60 auto m = make_query("hash1", std::chrono::microseconds(5000), true);
61 monitor_->record_query_metrics(m);
62
63 auto summary = monitor_->get_performance_summary();
64 EXPECT_EQ(summary.total_queries, 1u);
65 EXPECT_EQ(summary.successful_queries, 1u);
66 EXPECT_EQ(summary.failed_queries, 0u);
67}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [27/51]

TEST_F ( PerformanceMonitorQueryTest ,
RecordMultipleQueries  )

Definition at line 69 of file test_performance_monitor.cpp.

69 {
70 monitor_->record_query_metrics(
71 make_query("q1", std::chrono::microseconds(1000), true));
72 monitor_->record_query_metrics(
73 make_query("q2", std::chrono::microseconds(2000), true));
74 monitor_->record_query_metrics(
75 make_query("q3", std::chrono::microseconds(3000), false,
76 database_types::postgres, "timeout"));
77
78 auto summary = monitor_->get_performance_summary();
79 EXPECT_EQ(summary.total_queries, 3u);
80 EXPECT_EQ(summary.successful_queries, 2u);
81 EXPECT_EQ(summary.failed_queries, 1u);
82 EXPECT_GT(summary.error_rate, 0.0);
83}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [28/51]

TEST_F ( PoolMetricsTest ,
AverageWaitTime  )

Definition at line 612 of file test_performance_monitor.cpp.

612 {
613 metrics_.record_acquisition(100, true);
614 metrics_.record_acquisition(300, true);
615
616 // average_wait_time_us divides by total_acquisitions (2), not successful
617 EXPECT_DOUBLE_EQ(metrics_.average_wait_time_us(), 200.0);
618}

◆ TEST_F() [29/51]

TEST_F ( PoolMetricsTest ,
AverageWaitTimeZeroWhenNoAcquisitions  )

Definition at line 620 of file test_performance_monitor.cpp.

620 {
621 EXPECT_DOUBLE_EQ(metrics_.average_wait_time_us(), 0.0);
622}

◆ TEST_F() [30/51]

TEST_F ( PoolMetricsTest ,
InitialStateIsZero  )

Definition at line 535 of file test_performance_monitor.cpp.

535 {
536 EXPECT_EQ(metrics_.total_acquisitions.load(), 0u);
537 EXPECT_EQ(metrics_.successful_acquisitions.load(), 0u);
538 EXPECT_EQ(metrics_.failed_acquisitions.load(), 0u);
539 EXPECT_EQ(metrics_.timeouts.load(), 0u);
540 EXPECT_EQ(metrics_.current_active.load(), 0u);
541 EXPECT_EQ(metrics_.current_queued.load(), 0u);
542}

◆ TEST_F() [31/51]

TEST_F ( PoolMetricsTest ,
RecordFailedAcquisition  )

Definition at line 555 of file test_performance_monitor.cpp.

555 {
556 metrics_.record_acquisition(0, false);
557
558 EXPECT_EQ(metrics_.total_acquisitions.load(), 1u);
559 EXPECT_EQ(metrics_.successful_acquisitions.load(), 0u);
560 EXPECT_EQ(metrics_.failed_acquisitions.load(), 1u);
561}

◆ TEST_F() [32/51]

TEST_F ( PoolMetricsTest ,
RecordHealthCheck  )

Definition at line 603 of file test_performance_monitor.cpp.

603 {
604 metrics_.record_health_check(0);
605 metrics_.record_health_check(2);
606 metrics_.record_health_check(1);
607
608 EXPECT_EQ(metrics_.health_checks_performed.load(), 3u);
609 EXPECT_EQ(metrics_.unhealthy_connections_removed.load(), 3u);
610}

◆ TEST_F() [33/51]

TEST_F ( PoolMetricsTest ,
RecordMultipleAcquisitionsTracksMinMax  )

Definition at line 563 of file test_performance_monitor.cpp.

563 {
564 metrics_.record_acquisition(100, true);
565 metrics_.record_acquisition(500, true);
566 metrics_.record_acquisition(200, true);
567
568 EXPECT_EQ(metrics_.total_acquisitions.load(), 3u);
569 EXPECT_EQ(metrics_.successful_acquisitions.load(), 3u);
570 EXPECT_EQ(metrics_.total_wait_time_us.load(), 800u);
571 EXPECT_EQ(metrics_.min_wait_time_us.load(), 100u);
572 EXPECT_EQ(metrics_.max_wait_time_us.load(), 500u);
573}

◆ TEST_F() [34/51]

TEST_F ( PoolMetricsTest ,
RecordSuccessfulAcquisition  )

Definition at line 544 of file test_performance_monitor.cpp.

544 {
545 metrics_.record_acquisition(500, true);
546
547 EXPECT_EQ(metrics_.total_acquisitions.load(), 1u);
548 EXPECT_EQ(metrics_.successful_acquisitions.load(), 1u);
549 EXPECT_EQ(metrics_.failed_acquisitions.load(), 0u);
550 EXPECT_EQ(metrics_.total_wait_time_us.load(), 500u);
551 EXPECT_EQ(metrics_.min_wait_time_us.load(), 500u);
552 EXPECT_EQ(metrics_.max_wait_time_us.load(), 500u);
553}

◆ TEST_F() [35/51]

TEST_F ( PoolMetricsTest ,
RecordTimeout  )

Definition at line 575 of file test_performance_monitor.cpp.

575 {
576 metrics_.record_timeout();
577 metrics_.record_timeout();
578
579 EXPECT_EQ(metrics_.timeouts.load(), 2u);
580}

◆ TEST_F() [36/51]

TEST_F ( PoolMetricsTest ,
ResetClearsStatistics  )

Definition at line 636 of file test_performance_monitor.cpp.

636 {
637 metrics_.record_acquisition(500, true);
638 metrics_.record_timeout();
639 metrics_.update_active(3);
640 metrics_.record_health_check(1);
641
642 metrics_.reset();
643
644 EXPECT_EQ(metrics_.total_acquisitions.load(), 0u);
645 EXPECT_EQ(metrics_.successful_acquisitions.load(), 0u);
646 EXPECT_EQ(metrics_.timeouts.load(), 0u);
647 EXPECT_EQ(metrics_.total_wait_time_us.load(), 0u);
648 EXPECT_EQ(metrics_.min_wait_time_us.load(), UINT64_MAX);
649 EXPECT_EQ(metrics_.max_wait_time_us.load(), 0u);
650 EXPECT_EQ(metrics_.health_checks_performed.load(), 0u);
651 // current_active should remain (reflects current state)
652 EXPECT_EQ(metrics_.current_active.load(), 3u);
653 // peak_active reset to current_active
654 EXPECT_EQ(metrics_.peak_active.load(), 3u);
655}

◆ TEST_F() [37/51]

TEST_F ( PoolMetricsTest ,
SuccessRate  )

Definition at line 624 of file test_performance_monitor.cpp.

624 {
625 metrics_.record_acquisition(100, true);
626 metrics_.record_acquisition(200, true);
627 metrics_.record_acquisition(0, false);
628
629 EXPECT_NEAR(metrics_.success_rate(), 66.666, 0.01);
630}

◆ TEST_F() [38/51]

TEST_F ( PoolMetricsTest ,
SuccessRateNoAcquisitions  )

Definition at line 632 of file test_performance_monitor.cpp.

632 {
633 EXPECT_DOUBLE_EQ(metrics_.success_rate(), 100.0);
634}

◆ TEST_F() [39/51]

TEST_F ( PoolMetricsTest ,
UpdateActiveTracksPeak  )

Definition at line 582 of file test_performance_monitor.cpp.

582 {
583 metrics_.update_active(1);
584 metrics_.update_active(1);
585 metrics_.update_active(1); // peak = 3
586 metrics_.update_active(-1); // current = 2
587
588 EXPECT_EQ(metrics_.current_active.load(), 2u);
589 EXPECT_EQ(metrics_.peak_active.load(), 3u);
590}

◆ TEST_F() [40/51]

TEST_F ( PoolMetricsTest ,
UpdateQueuedTracksPeak  )

Definition at line 592 of file test_performance_monitor.cpp.

592 {
593 metrics_.update_queued(1);
594 metrics_.update_queued(1);
595 metrics_.update_queued(1); // peak = 3
596 metrics_.update_queued(-1);
597 metrics_.update_queued(-1); // current = 1
598
599 EXPECT_EQ(metrics_.current_queued.load(), 1u);
600 EXPECT_EQ(metrics_.peak_queued.load(), 3u);
601}

◆ TEST_F() [41/51]

TEST_F ( PrometheusExporterTest ,
ExportAlertsReturnsTrue  )

Definition at line 685 of file test_performance_monitor.cpp.

685 {
686 prometheus_exporter exporter("localhost", 9090);
687 std::vector<performance_alert> alerts;
688 alerts.emplace_back(
689 performance_alert::alert_type::slow_query, "test",
690 std::chrono::steady_clock::now());
691
692 EXPECT_TRUE(exporter.export_alerts(alerts));
693}
Export metrics in Prometheus format.

References database::monitoring::prometheus_exporter::export_alerts().

Here is the call graph for this function:

◆ TEST_F() [42/51]

TEST_F ( PrometheusExporterTest ,
ExportMetricsReturnsTrue  )

Definition at line 678 of file test_performance_monitor.cpp.

678 {
679 prometheus_exporter exporter("localhost", 9090);
680 performance_summary summary;
681
682 EXPECT_TRUE(exporter.export_metrics(summary));
683}

References database::monitoring::prometheus_exporter::export_metrics().

Here is the call graph for this function:

◆ TEST_F() [43/51]

TEST_F ( PrometheusExporterTest ,
FormatContainsMetricNames  )

Definition at line 663 of file test_performance_monitor.cpp.

663 {
664 prometheus_exporter exporter("localhost", 9090);
665 performance_summary summary;
666 summary.total_queries = 100;
667 summary.avg_query_time = std::chrono::microseconds(500);
668 summary.error_rate = 0.02;
669 summary.active_connections = 5;
670
671 std::string output = exporter.format_prometheus_metrics(summary);
672 EXPECT_NE(output.find("database_queries_total 100"), std::string::npos);
673 EXPECT_NE(output.find("database_query_duration_microseconds 500"), std::string::npos);
674 EXPECT_NE(output.find("database_error_rate 0.02"), std::string::npos);
675 EXPECT_NE(output.find("database_connections_active 5"), std::string::npos);
676}

References database::monitoring::performance_summary::active_connections, database::monitoring::performance_summary::avg_query_time, database::monitoring::performance_summary::error_rate, database::monitoring::prometheus_exporter::format_prometheus_metrics(), and database::monitoring::performance_summary::total_queries.

Here is the call graph for this function:

◆ TEST_F() [44/51]

TEST_F ( QueryHistoryTest ,
GetRecentQueriesEmptyWindow  )

Definition at line 221 of file test_performance_monitor.cpp.

221 {
222 monitor_->record_query_metrics(
223 make_query("q1", std::chrono::microseconds(100), true));
224
225 // Use zero-length window — queries just recorded should still be within
226 // the window since their start_time is "now"
227 auto recent = monitor_->get_recent_queries(std::chrono::minutes(0));
228 // With 0-minute window, cutoff == now, so queries at exactly now are borderline
229 // Result depends on timing; we just verify no crash
230 EXPECT_LE(recent.size(), 1u);
231}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [45/51]

TEST_F ( QueryHistoryTest ,
GetRecentQueriesWithinWindow  )

Definition at line 211 of file test_performance_monitor.cpp.

211 {
212 monitor_->record_query_metrics(
213 make_query("q1", std::chrono::microseconds(100), true));
214 monitor_->record_query_metrics(
215 make_query("q2", std::chrono::microseconds(200), true));
216
217 auto recent = monitor_->get_recent_queries(std::chrono::minutes(5));
218 EXPECT_EQ(recent.size(), 2u);
219}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [46/51]

TEST_F ( QueryHistoryTest ,
GetSlowQueriesAboveThreshold  )

Definition at line 233 of file test_performance_monitor.cpp.

233 {
234 monitor_->record_query_metrics(
235 make_query("fast", std::chrono::microseconds(100), true));
236 monitor_->record_query_metrics(
237 make_query("medium", std::chrono::microseconds(5000), true));
238 monitor_->record_query_metrics(
239 make_query("slow", std::chrono::microseconds(50000), true));
240
241 auto slow = monitor_->get_slow_queries(std::chrono::microseconds(4000));
242 EXPECT_EQ(slow.size(), 2u);
243}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [47/51]

TEST_F ( QueryHistoryTest ,
GetSlowQueriesNoneAboveThreshold  )

Definition at line 245 of file test_performance_monitor.cpp.

245 {
246 monitor_->record_query_metrics(
247 make_query("fast", std::chrono::microseconds(100), true));
248
249 auto slow = monitor_->get_slow_queries(std::chrono::microseconds(1000000));
250 EXPECT_TRUE(slow.empty());
251}

References make_query().

Here is the call graph for this function:

◆ TEST_F() [48/51]

TEST_F ( QueryHistoryTest ,
GetSlowQueriesPreservesMetadata  )

Definition at line 253 of file test_performance_monitor.cpp.

253 {
254 auto original = make_query("slow_hash", std::chrono::microseconds(99999), false,
255 database_types::sqlite, "timeout");
256 monitor_->record_query_metrics(original);
257
258 auto slow = monitor_->get_slow_queries(std::chrono::microseconds(1000));
259 ASSERT_EQ(slow.size(), 1u);
260 EXPECT_EQ(slow[0].query_hash, "slow_hash");
261 EXPECT_FALSE(slow[0].success);
262 EXPECT_EQ(slow[0].error_message, "timeout");
263 EXPECT_EQ(slow[0].db_type, database_types::sqlite);
264}
#define ASSERT_EQ(expected, actual, message)

References ASSERT_EQ, make_query(), and database::success.

Here is the call graph for this function:

◆ TEST_F() [49/51]

TEST_F ( QueryTimerTest ,
DestructorRecordsMetrics  )

Definition at line 708 of file test_performance_monitor.cpp.

708 {
709 {
710 query_timer timer("SELECT 1", database_types::postgres, monitor_);
711 // Timer will record on destruction
712 }
713
714 auto summary = monitor_->get_performance_summary();
715 EXPECT_EQ(summary.total_queries, 1u);
716 EXPECT_EQ(summary.successful_queries, 1u);
717}
RAII timer for measuring query execution time.

◆ TEST_F() [50/51]

TEST_F ( QueryTimerTest ,
SetErrorMarksFailure  )

Definition at line 719 of file test_performance_monitor.cpp.

719 {
720 {
721 query_timer timer("SELECT bad", database_types::postgres, monitor_);
722 timer.set_error("syntax error");
723 }
724
725 auto summary = monitor_->get_performance_summary();
726 EXPECT_EQ(summary.total_queries, 1u);
727 EXPECT_EQ(summary.failed_queries, 1u);
728}

References database::monitoring::query_timer::set_error().

Here is the call graph for this function:

◆ TEST_F() [51/51]

TEST_F ( QueryTimerTest ,
SetRowsAffected  )

Definition at line 730 of file test_performance_monitor.cpp.

730 {
731 {
732 query_timer timer("INSERT INTO test VALUES (1)", database_types::sqlite, monitor_);
733 timer.set_rows_affected(42);
734 }
735
736 auto recent = monitor_->get_recent_queries(std::chrono::minutes(5));
737 ASSERT_EQ(recent.size(), 1u);
738 EXPECT_EQ(recent[0].rows_affected, 42u);
739}

References ASSERT_EQ, and database::monitoring::query_timer::set_rows_affected().

Here is the call graph for this function: