File indexing completed on 2024-11-24 04:55:59

0001 /*
0002     SPDX-FileCopyrightText: 2023 David Redondo <kde@david-redondo.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include <QSignalSpy>
0008 #include <QTest>
0009 
0010 #include "../NvidiaSmiProcess.h"
0011 
0012 class NvidiaParserTest : public QObject
0013 {
0014     Q_OBJECT
0015 private Q_SLOTS:
0016     void testDmonParsing_data();
0017     void testDmonParsing();
0018 
0019 private:
0020 };
0021 
0022 void NvidiaParserTest::testDmonParsing_data()
0023 {
0024     QTest::addColumn<QString>("data");
0025     QTest::addColumn<NvidiaSmiProcess::GpuData>("expected");
0026     QTest::newRow("520")
0027         << QFINDTESTDATA("fixtures/520.txt")
0028         << NvidiaSmiProcess::GpuData{.index = 0, .power = 11, .temperature = 46, .usage = 3, .memoryUsed = 434, .coreFrequency = 139, .memoryFrequency = 405};
0029     QTest::newRow("525")
0030         << QFINDTESTDATA("fixtures/525.txt")
0031         << NvidiaSmiProcess::GpuData{.index = 0, .power = 29, .temperature = 49, .usage = 1, .memoryUsed = 403, .coreFrequency = 974, .memoryFrequency = 4006};
0032     QTest::newRow("530")
0033         << QFINDTESTDATA("fixtures/530.txt")
0034         << NvidiaSmiProcess::GpuData{.index = 0, .power = 28, .temperature = 44, .usage = 4, .memoryUsed = 902, .coreFrequency = 210, .memoryFrequency = 405};
0035 }
0036 
0037 void NvidiaParserTest::testDmonParsing()
0038 {
0039     QFETCH(QString, data);
0040     QFETCH(NvidiaSmiProcess::GpuData, expected);
0041 
0042     NvidiaSmiProcess smi;
0043     QSignalSpy dataSpy(&smi, &NvidiaSmiProcess::dataReceived);
0044 
0045     QFile dataFile(data);
0046     QVERIFY(dataFile.open(QIODevice::ReadOnly));
0047     int lines = -2; // subtract two headers
0048     while (!dataFile.atEnd()) {
0049         ++lines;
0050         smi.readStatisticsData(dataFile.readLine());
0051     }
0052 
0053     QCOMPARE(dataSpy.count(), lines);
0054     for (const auto &emission : dataSpy) {
0055         const auto gpuData = emission[0].value<NvidiaSmiProcess::GpuData>();
0056         QCOMPARE(gpuData.index, expected.index);
0057         QCOMPARE(gpuData.power, expected.power);
0058         QCOMPARE(gpuData.temperature, expected.temperature);
0059         QCOMPARE(gpuData.usage, expected.usage);
0060         QCOMPARE(gpuData.memoryUsed, expected.memoryUsed);
0061         QCOMPARE(gpuData.coreFrequency, expected.coreFrequency);
0062         QCOMPARE(gpuData.memoryFrequency, expected.memoryFrequency);
0063     }
0064 }
0065 
0066 QTEST_MAIN(NvidiaParserTest)
0067 
0068 #include "nvidia.moc"