5#include <gtest/gtest.h>
13class GpuCollectorTest :
public ::testing::Test {
15 void SetUp()
override {
16 collector_ = std::make_unique<gpu_collector>();
17 std::unordered_map<std::string, std::string> config;
25TEST_F(GpuCollectorTest, InitializesSuccessfully) {
30TEST_F(GpuCollectorTest, ReturnsCorrectMetricTypes) {
31 auto metric_types =
collector_->get_metric_types();
34 EXPECT_FALSE(metric_types.empty());
37 auto contains = [&metric_types](
const std::string& type) {
38 return std::find(metric_types.begin(), metric_types.end(), type) != metric_types.end();
41 EXPECT_TRUE(contains(
"gpu_utilization_percent"));
42 EXPECT_TRUE(contains(
"gpu_memory_used_bytes"));
43 EXPECT_TRUE(contains(
"gpu_memory_total_bytes"));
44 EXPECT_TRUE(contains(
"gpu_memory_usage_percent"));
45 EXPECT_TRUE(contains(
"gpu_temperature_celsius"));
46 EXPECT_TRUE(contains(
"gpu_power_watts"));
47 EXPECT_TRUE(contains(
"gpu_clock_mhz"));
48 EXPECT_TRUE(contains(
"gpu_fan_speed_percent"));
52TEST_F(GpuCollectorTest, ConfigurationOptions) {
53 auto custom_collector = std::make_unique<gpu_collector>();
55 std::unordered_map<std::string, std::string> config = {
57 {
"collect_utilization",
"true"},
58 {
"collect_memory",
"true"},
59 {
"collect_temperature",
"true"},
60 {
"collect_power",
"true"},
61 {
"collect_clock",
"true"},
62 {
"collect_fan",
"true"}};
64 EXPECT_TRUE(custom_collector->initialize(config));
68TEST_F(GpuCollectorTest, CanBeDisabled) {
69 auto custom_collector = std::make_unique<gpu_collector>();
71 std::unordered_map<std::string, std::string> config = {{
"enabled",
"false"}};
73 custom_collector->initialize(config);
76 auto metrics = custom_collector->collect();
77 EXPECT_TRUE(metrics.empty());
81TEST_F(GpuCollectorTest, TracksStatistics) {
85 EXPECT_TRUE(stats.find(
"collection_count") != stats.end());
86 EXPECT_TRUE(stats.find(
"collection_errors") != stats.end());
87 EXPECT_TRUE(stats.find(
"gpus_found") != stats.end());
90 EXPECT_EQ(stats[
"collection_count"], 0.0);
91 EXPECT_EQ(stats[
"collection_errors"], 0.0);
95TEST_F(GpuCollectorTest, CollectReturnsMetrics) {
101 EXPECT_GE(stats[
"collection_count"], 1.0);
105TEST_F(GpuCollectorTest, GetLastReadings) {
107 auto last_readings =
collector_->get_last_readings();
115TEST_F(GpuCollectorTest, GpuAvailabilityCheck) {
117 bool available =
collector_->is_gpu_available();
122TEST(GpuReadingTest, DefaultInitialization) {
125 EXPECT_TRUE(reading.device.id.empty());
126 EXPECT_TRUE(reading.device.name.empty());
129 EXPECT_EQ(reading.utilization_percent, 0.0);
130 EXPECT_EQ(reading.memory_used_bytes, 0u);
131 EXPECT_EQ(reading.memory_total_bytes, 0u);
132 EXPECT_EQ(reading.temperature_celsius, 0.0);
133 EXPECT_EQ(reading.power_watts, 0.0);
134 EXPECT_EQ(reading.clock_mhz, 0.0);
135 EXPECT_EQ(reading.fan_speed_percent, 0.0);
136 EXPECT_FALSE(reading.utilization_available);
137 EXPECT_FALSE(reading.memory_available);
138 EXPECT_FALSE(reading.temperature_available);
139 EXPECT_FALSE(reading.power_available);
140 EXPECT_FALSE(reading.clock_available);
141 EXPECT_FALSE(reading.fan_available);
145TEST(GpuDeviceInfoTest, DefaultInitialization) {
146 gpu_device_info
info;
148 EXPECT_TRUE(
info.id.empty());
149 EXPECT_TRUE(
info.name.empty());
150 EXPECT_TRUE(
info.device_path.empty());
151 EXPECT_TRUE(
info.driver_version.empty());
154 EXPECT_EQ(
info.device_index, 0u);
158TEST(GpuVendorTest, VendorToStringConversion) {
168TEST(GpuTypeTest, TypeToStringConversion) {
176TEST(GpuInfoCollectorTest, BasicFunctionality) {
177 gpu_info_collector collector;
180 bool available = collector.is_gpu_available();
184 auto gpus = collector.enumerate_gpus();
189 auto gpus = collector.enumerate_gpus();
196TEST(GpuInfoCollectorTest, EnumerateGpus) {
197 gpu_info_collector collector;
199 auto gpus = collector.enumerate_gpus();
207TEST_F(GpuCollectorTest, MultipleCollectionsAreStable) {
208 for (
int i = 0; i < 5; ++i) {
215 EXPECT_GE(stats[
"collection_count"], 5.0);
216 EXPECT_EQ(stats[
"collection_errors"], 0.0);
220TEST_F(GpuCollectorTest, MetricsHaveCorrectTags) {
223 for (
const auto& m : metrics) {
225 if (!m.name.empty()) {
226 EXPECT_TRUE(m.tags.find(
"gpu_id") != m.tags.end());
227 EXPECT_TRUE(m.tags.find(
"gpu_name") != m.tags.end());
228 EXPECT_TRUE(m.tags.find(
"gpu_vendor") != m.tags.end());
229 EXPECT_TRUE(m.tags.find(
"gpu_type") != m.tags.end());
230 EXPECT_TRUE(m.tags.find(
"gpu_index") != m.tags.end());
236TEST(GpuInfoCollectorTest, ReadAllGpuMetrics) {
237 gpu_info_collector collector;
239 auto readings = collector.read_all_gpu_metrics();
243 for (
const auto& reading : readings) {
245 EXPECT_FALSE(reading.device.id.empty());
250TEST_F(GpuCollectorTest, SelectiveMetricCollection) {
251 auto custom_collector = std::make_unique<gpu_collector>();
254 std::unordered_map<std::string, std::string> config = {
256 {
"collect_utilization",
"false"},
257 {
"collect_memory",
"false"},
258 {
"collect_temperature",
"true"},
259 {
"collect_power",
"false"},
260 {
"collect_clock",
"false"},
261 {
"collect_fan",
"false"}};
263 EXPECT_TRUE(custom_collector->initialize(config));
266 auto metrics = custom_collector->collect();
271TEST_F(GpuCollectorTest, ReinitializeHandledGracefully) {
273 std::unordered_map<std::string, std::string> config1 = {{
"enabled",
"true"}};
278 std::unordered_map<std::string, std::string> config2 = {{
"enabled",
"false"}};
283 EXPECT_TRUE(metrics.empty());
GPU metrics monitoring collector.
std::string gpu_vendor_to_string(gpu_vendor vendor)
Convert gpu_vendor to string representation.
@ virtual_gpu
Virtual GPU (cloud/VM)
@ discrete
Discrete GPU (dedicated graphics card)
@ unknown
Unknown GPU type.
@ integrated
Integrated GPU (part of CPU/SoC)
std::string gpu_type_to_string(gpu_type type)
Convert gpu_type to string representation.
@ info
Informational, no action required.
@ apple
Apple (Apple Silicon GPU)
@ intel
Intel Corporation.
@ nvidia
NVIDIA Corporation.
@ amd
Advanced Micro Devices.
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< battery_collector > collector_