Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
test_system_resource_collector.cpp File Reference
#include <gtest/gtest.h>
#include <kcenon/monitoring/collectors/system_resource_collector.h>
#include <thread>
#include <chrono>
#include <unordered_set>
Include dependency graph for test_system_resource_collector.cpp:

Go to the source code of this file.

Classes

class  SystemResourceCollectorTest
 

Functions

 TEST_F (SystemResourceCollectorTest, Initialization)
 
 TEST_F (SystemResourceCollectorTest, CollectMetrics)
 
 TEST_F (SystemResourceCollectorTest, ContextSwitchMonitoring)
 
 TEST_F (SystemResourceCollectorTest, DiskMetricsCollection)
 
 TEST_F (SystemResourceCollectorTest, NetworkMetricsCollection)
 
 TEST_F (SystemResourceCollectorTest, GetMetricTypesIncludesNewMetrics)
 
 TEST_F (SystemResourceCollectorTest, CollectionFiltersWork)
 
 TEST_F (SystemResourceCollectorTest, ConfigConstructorWorks)
 
 TEST_F (SystemResourceCollectorTest, GetConfigReturnsCurrentSettings)
 

Function Documentation

◆ TEST_F() [1/9]

TEST_F ( SystemResourceCollectorTest ,
CollectionFiltersWork  )

Definition at line 221 of file test_system_resource_collector.cpp.

221 {
222 // Disable disk and network metrics using config API
223 system_metrics_config filter_config;
224 filter_config.collect_cpu = true;
225 filter_config.collect_memory = true;
226 filter_config.collect_disk = false;
227 filter_config.collect_network = false;
228 collector->set_config(filter_config);
229
230 auto metrics = collector->collect();
231
232 bool has_cpu = false;
233 bool has_disk = false;
234 bool has_network = false;
235
236 for (const auto& m : metrics) {
237 if (m.name == "system.cpu.usage_percent") has_cpu = true;
238 if (m.name == "system.disk.usage_percent") has_disk = true;
239 if (m.name == "system.network.rx_bytes_per_sec") has_network = true;
240 }
241
242 EXPECT_TRUE(has_cpu) << "CPU metrics should be collected when enabled";
243 EXPECT_FALSE(has_disk) << "Disk metrics should not be collected when disabled";
244 EXPECT_FALSE(has_network) << "Network metrics should not be collected when disabled";
245
246 // Re-enable all metrics using new config API
248 config.collect_cpu = true;
249 config.collect_memory = true;
250 config.collect_disk = true;
251 config.collect_network = true;
252 collector->set_config(config);
253
254 metrics = collector->collect();
255 has_disk = false;
256 has_network = false;
257
258 for (const auto& m : metrics) {
259 if (m.name == "system.disk.usage_percent") has_disk = true;
260 if (m.name == "system.network.rx_bytes_per_sec") has_network = true;
261 }
262
263 EXPECT_TRUE(has_disk) << "Disk metrics should be collected when re-enabled";
264 EXPECT_TRUE(has_network) << "Network metrics should be collected when re-enabled";
265}

References kcenon::monitoring::system_metrics_config::collect_cpu, kcenon::monitoring::system_metrics_config::collect_disk, kcenon::monitoring::system_metrics_config::collect_memory, and kcenon::monitoring::system_metrics_config::collect_network.

◆ TEST_F() [2/9]

TEST_F ( SystemResourceCollectorTest ,
CollectMetrics  )

Definition at line 25 of file test_system_resource_collector.cpp.

25 {
26 // First collection might be zero for rates
27 auto metrics1 = collector->collect();
28 EXPECT_FALSE(metrics1.empty());
29
30 // Check metric names exist (using new system.* naming convention)
31 bool has_cpu = false;
32 bool has_memory = false;
33 bool has_context = false;
34
35 for (const auto& m : metrics1) {
36 if (m.name == "system.cpu.usage_percent") has_cpu = true;
37 if (m.name == "system.memory.usage_percent") has_memory = true;
38 if (m.name == "system.context_switches.total") has_context = true;
39 }
40
41 EXPECT_TRUE(has_cpu);
42 EXPECT_TRUE(has_memory);
43 EXPECT_TRUE(has_context);
44}

◆ TEST_F() [3/9]

TEST_F ( SystemResourceCollectorTest ,
ConfigConstructorWorks  )

Definition at line 267 of file test_system_resource_collector.cpp.

267 {
269 config.collect_cpu = true;
270 config.collect_memory = false;
271 config.collect_disk = false;
272 config.collect_network = false;
273 config.collect_process = false;
274
275 auto custom_collector = std::make_unique<system_resource_collector>(config);
276 auto metrics = custom_collector->collect();
277
278 bool has_cpu = false;
279 bool has_memory = false;
280
281 for (const auto& m : metrics) {
282 if (m.name == "system.cpu.usage_percent") has_cpu = true;
283 if (m.name == "system.memory.usage_percent") has_memory = true;
284 }
285
286 EXPECT_TRUE(has_cpu) << "CPU metrics should be collected";
287 EXPECT_FALSE(has_memory) << "Memory metrics should not be collected when disabled";
288}

References kcenon::monitoring::system_metrics_config::collect_cpu, kcenon::monitoring::system_metrics_config::collect_disk, kcenon::monitoring::system_metrics_config::collect_memory, kcenon::monitoring::system_metrics_config::collect_network, and kcenon::monitoring::system_metrics_config::collect_process.

◆ TEST_F() [4/9]

TEST_F ( SystemResourceCollectorTest ,
ContextSwitchMonitoring  )

Definition at line 46 of file test_system_resource_collector.cpp.

46 {
47 // First collection
48 auto metrics1 = collector->collect();
49 uint64_t csw_total_1 = 0;
50
51 for (const auto& m : metrics1) {
52 if (m.name == "system.context_switches.total") {
53 try {
54 csw_total_1 = static_cast<uint64_t>(std::get<double>(m.value));
55 } catch (...) {
56 try { csw_total_1 = static_cast<uint64_t>(std::get<int64_t>(m.value)); } catch(...) {}
57 }
58 }
59 }
60
61 // On Windows, context switch monitoring is not implemented (returns 0)
62#if defined(_WIN32)
63 EXPECT_EQ(csw_total_1, 0u) << "Context switches are not collected on Windows";
64#else
65 EXPECT_GT(csw_total_1, 0) << "Context switches should be non-zero";
66#endif
67
68 // Sleep to allow context switches to happen
69 std::this_thread::sleep_for(std::chrono::milliseconds(100));
70
71 // Second collection
72 auto metrics2 = collector->collect();
73 uint64_t csw_total_2 = 0;
74 double csw_rate = 0.0;
75
76 for (const auto& m : metrics2) {
77 if (m.name == "system.context_switches.total") {
78 try {
79 csw_total_2 = static_cast<uint64_t>(std::get<double>(m.value));
80 } catch (...) {
81 // Try int64_t if double fails
82 try { csw_total_2 = static_cast<uint64_t>(std::get<int64_t>(m.value)); } catch(...) {}
83 }
84 }
85 if (m.name == "system.context_switches.per_sec") {
86 try {
87 csw_rate = std::get<double>(m.value);
88 } catch (...) {
89 try { csw_rate = static_cast<double>(std::get<int64_t>(m.value)); } catch(...) {}
90 }
91 }
92 }
93
94 #if defined(__linux__)
95 // On Linux, system-wide context switches should be monotonically increasing
96 EXPECT_GE(csw_total_2, csw_total_1);
97 #elif defined(__APPLE__)
98 // On macOS, we read process-level context switches which may not be
99 // monotonically increasing in CI environments due to process lifecycle
100 // Just verify we got valid readings
101 EXPECT_GT(csw_total_1, 0u);
102 EXPECT_GT(csw_total_2, 0u);
103 #elif defined(_WIN32)
104 // On Windows, context switch monitoring is not implemented
105 // Both readings should be zero
106 EXPECT_EQ(csw_total_2, 0u);
107 #endif
108}

◆ TEST_F() [5/9]

TEST_F ( SystemResourceCollectorTest ,
DiskMetricsCollection  )

Definition at line 110 of file test_system_resource_collector.cpp.

110 {
111 // First collection to initialize state
112 collector->collect();
113
114 // Wait a bit to allow I/O to happen
115 std::this_thread::sleep_for(std::chrono::milliseconds(100));
116
117 // Second collection should have disk metrics
118 auto metrics = collector->collect();
119
120 bool has_disk_usage = false;
121 bool has_disk_total = false;
122 bool has_disk_used = false;
123 bool has_disk_available = false;
124 bool has_disk_read_bytes = false;
125 bool has_disk_write_bytes = false;
126
127 double disk_usage_percent = 0.0;
128 double disk_total = 0.0;
129
130 for (const auto& m : metrics) {
131 if (m.name == "system.disk.usage_percent") {
132 has_disk_usage = true;
133 disk_usage_percent = std::get<double>(m.value);
134 }
135 if (m.name == "system.disk.total_bytes") {
136 has_disk_total = true;
137 disk_total = std::get<double>(m.value);
138 }
139 if (m.name == "system.disk.used_bytes") has_disk_used = true;
140 if (m.name == "system.disk.available_bytes") has_disk_available = true;
141 if (m.name == "system.disk.read_bytes_per_sec") has_disk_read_bytes = true;
142 if (m.name == "system.disk.write_bytes_per_sec") has_disk_write_bytes = true;
143 }
144
145 // All disk metrics should be present
146 EXPECT_TRUE(has_disk_usage) << "system.disk.usage_percent metric should be present";
147 EXPECT_TRUE(has_disk_total) << "system.disk.total_bytes metric should be present";
148 EXPECT_TRUE(has_disk_used) << "system.disk.used_bytes metric should be present";
149 EXPECT_TRUE(has_disk_available) << "system.disk.available_bytes metric should be present";
150 EXPECT_TRUE(has_disk_read_bytes) << "system.disk.read_bytes_per_sec metric should be present";
151 EXPECT_TRUE(has_disk_write_bytes) << "system.disk.write_bytes_per_sec metric should be present";
152
153 // Disk usage should be a valid percentage
154 EXPECT_GE(disk_usage_percent, 0.0);
155 EXPECT_LE(disk_usage_percent, 100.0);
156
157 // Total disk should be non-zero
158 EXPECT_GT(disk_total, 0.0) << "Total disk space should be non-zero";
159}

◆ TEST_F() [6/9]

TEST_F ( SystemResourceCollectorTest ,
GetConfigReturnsCurrentSettings  )

Definition at line 290 of file test_system_resource_collector.cpp.

290 {
292 config.collect_cpu = true;
293 config.collect_memory = false;
294 config.collect_disk = true;
295 config.collect_network = false;
296 config.collect_process = true;
297
298 collector->set_config(config);
299 auto retrieved = collector->get_config();
300
301 EXPECT_EQ(retrieved.collect_cpu, config.collect_cpu);
302 EXPECT_EQ(retrieved.collect_memory, config.collect_memory);
303 EXPECT_EQ(retrieved.collect_disk, config.collect_disk);
304 EXPECT_EQ(retrieved.collect_network, config.collect_network);
305 EXPECT_EQ(retrieved.collect_process, config.collect_process);
306}

References kcenon::monitoring::system_metrics_config::collect_cpu, kcenon::monitoring::system_metrics_config::collect_disk, kcenon::monitoring::system_metrics_config::collect_memory, kcenon::monitoring::system_metrics_config::collect_network, and kcenon::monitoring::system_metrics_config::collect_process.

◆ TEST_F() [7/9]

TEST_F ( SystemResourceCollectorTest ,
GetMetricTypesIncludesNewMetrics  )

Definition at line 202 of file test_system_resource_collector.cpp.

202 {
203 auto types = collector->get_metric_types();
204
205 // Convert to set for easier lookup
206 std::unordered_set<std::string> type_set(types.begin(), types.end());
207
208 // Check disk metrics are listed
209 EXPECT_TRUE(type_set.count("system.disk.usage_percent") > 0);
210 EXPECT_TRUE(type_set.count("system.disk.total_bytes") > 0);
211 EXPECT_TRUE(type_set.count("system.disk.read_bytes_per_sec") > 0);
212 EXPECT_TRUE(type_set.count("system.disk.read_ops_per_sec") > 0);
213
214 // Check network metrics are listed
215 EXPECT_TRUE(type_set.count("system.network.rx_bytes_per_sec") > 0);
216 EXPECT_TRUE(type_set.count("system.network.tx_bytes_per_sec") > 0);
217 EXPECT_TRUE(type_set.count("system.network.rx_errors") > 0);
218 EXPECT_TRUE(type_set.count("system.network.rx_dropped") > 0);
219}

◆ TEST_F() [8/9]

TEST_F ( SystemResourceCollectorTest ,
Initialization  )

Definition at line 19 of file test_system_resource_collector.cpp.

19 {
20 std::unordered_map<std::string, std::string> config;
21 EXPECT_TRUE(collector->initialize(config));
22 EXPECT_EQ(collector->get_name(), "system_resource_collector");
23}

◆ TEST_F() [9/9]

TEST_F ( SystemResourceCollectorTest ,
NetworkMetricsCollection  )

Definition at line 161 of file test_system_resource_collector.cpp.

161 {
162 // First collection to initialize state
163 collector->collect();
164
165 // Wait a bit to allow network activity
166 std::this_thread::sleep_for(std::chrono::milliseconds(100));
167
168 // Second collection should have network metrics
169 auto metrics = collector->collect();
170
171 bool has_rx_bytes = false;
172 bool has_tx_bytes = false;
173 bool has_rx_packets = false;
174 bool has_tx_packets = false;
175 bool has_rx_errors = false;
176 bool has_tx_errors = false;
177 bool has_rx_dropped = false;
178 bool has_tx_dropped = false;
179
180 for (const auto& m : metrics) {
181 if (m.name == "system.network.rx_bytes_per_sec") has_rx_bytes = true;
182 if (m.name == "system.network.tx_bytes_per_sec") has_tx_bytes = true;
183 if (m.name == "system.network.rx_packets_per_sec") has_rx_packets = true;
184 if (m.name == "system.network.tx_packets_per_sec") has_tx_packets = true;
185 if (m.name == "system.network.rx_errors") has_rx_errors = true;
186 if (m.name == "system.network.tx_errors") has_tx_errors = true;
187 if (m.name == "system.network.rx_dropped") has_rx_dropped = true;
188 if (m.name == "system.network.tx_dropped") has_tx_dropped = true;
189 }
190
191 // All network metrics should be present
192 EXPECT_TRUE(has_rx_bytes) << "system.network.rx_bytes_per_sec metric should be present";
193 EXPECT_TRUE(has_tx_bytes) << "system.network.tx_bytes_per_sec metric should be present";
194 EXPECT_TRUE(has_rx_packets) << "system.network.rx_packets_per_sec metric should be present";
195 EXPECT_TRUE(has_tx_packets) << "system.network.tx_packets_per_sec metric should be present";
196 EXPECT_TRUE(has_rx_errors) << "system.network.rx_errors metric should be present";
197 EXPECT_TRUE(has_tx_errors) << "system.network.tx_errors metric should be present";
198 EXPECT_TRUE(has_rx_dropped) << "system.network.rx_dropped metric should be present";
199 EXPECT_TRUE(has_tx_dropped) << "system.network.tx_dropped metric should be present";
200}