48 auto metrics1 = collector->collect();
49 uint64_t csw_total_1 = 0;
51 for (
const auto& m : metrics1) {
52 if (m.name ==
"system.context_switches.total") {
54 csw_total_1 =
static_cast<uint64_t
>(std::get<double>(m.value));
56 try { csw_total_1 =
static_cast<uint64_t
>(std::get<int64_t>(m.value)); }
catch(...) {}
63 EXPECT_EQ(csw_total_1, 0u) <<
"Context switches are not collected on Windows";
65 EXPECT_GT(csw_total_1, 0) <<
"Context switches should be non-zero";
69 std::this_thread::sleep_for(std::chrono::milliseconds(100));
72 auto metrics2 = collector->collect();
73 uint64_t csw_total_2 = 0;
74 double csw_rate = 0.0;
76 for (
const auto& m : metrics2) {
77 if (m.name ==
"system.context_switches.total") {
79 csw_total_2 =
static_cast<uint64_t
>(std::get<double>(m.value));
82 try { csw_total_2 =
static_cast<uint64_t
>(std::get<int64_t>(m.value)); }
catch(...) {}
85 if (m.name ==
"system.context_switches.per_sec") {
87 csw_rate = std::get<double>(m.value);
89 try { csw_rate =
static_cast<double>(std::get<int64_t>(m.value)); }
catch(...) {}
94 #if defined(__linux__)
96 EXPECT_GE(csw_total_2, csw_total_1);
97 #elif defined(__APPLE__)
101 EXPECT_GT(csw_total_1, 0u);
102 EXPECT_GT(csw_total_2, 0u);
103 #elif defined(_WIN32)
106 EXPECT_EQ(csw_total_2, 0u);
112 collector->collect();
115 std::this_thread::sleep_for(std::chrono::milliseconds(100));
118 auto metrics = collector->collect();
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;
127 double disk_usage_percent = 0.0;
128 double disk_total = 0.0;
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);
135 if (m.name ==
"system.disk.total_bytes") {
136 has_disk_total =
true;
137 disk_total = std::get<double>(m.value);
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;
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";
154 EXPECT_GE(disk_usage_percent, 0.0);
155 EXPECT_LE(disk_usage_percent, 100.0);
158 EXPECT_GT(disk_total, 0.0) <<
"Total disk space should be non-zero";
163 collector->collect();
166 std::this_thread::sleep_for(std::chrono::milliseconds(100));
169 auto metrics = collector->collect();
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;
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;
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";
203 auto types = collector->get_metric_types();
206 std::unordered_set<std::string> type_set(types.begin(), types.end());
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);
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);
228 collector->set_config(filter_config);
230 auto metrics = collector->collect();
232 bool has_cpu =
false;
233 bool has_disk =
false;
234 bool has_network =
false;
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;
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";
252 collector->set_config(config);
254 metrics = collector->collect();
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;
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";
298 collector->set_config(config);
299 auto retrieved = collector->get_config();
301 EXPECT_EQ(retrieved.collect_cpu, config.
collect_cpu);