#include "system_metrics.h" #include #include #include #include class SystemMetricsTest : public QObject { Q_OBJECT private slots: void countersAndMissing() { QTemporaryDir root; auto put=[&](const QString& path,const QByteArray& value) { QFile f(root.path()+path); QVERIFY(f.open(QIODevice::WriteOnly)); f.write(value); }; QDir().mkpath(root.path()+"/thermal/thermal_zone0"); put("/stat","cpu 100 0 50 800 50 0 0 0 100 0\n"); put("/meminfo","MemTotal: 1000 kB\nMemAvailable: 700 kB\n"); put("/uptime","120.5 80\n"); put("/thermal/thermal_zone0/type","soc-thermal\n"); put("/thermal/thermal_zone0/temp","44370\n"); vdi::SystemMetrics collector(root.path(),root.path()+"/thermal"); auto sample=collector.sample(); QVERIFY(sample["cpuPercent"].isNull()); QCOMPARE(sample["memoryPercent"].toDouble(),30.0); QCOMPARE(sample["socTemperatureC"].toDouble(),44.37); QCOMPARE(sample["uptimeSeconds"].toDouble(),120.5); put("/stat","cpu 120 0 60 860 60 0 0 0 500 0\n"); QCOMPARE(collector.sample()["cpuPercent"].toDouble(),30.0); // guest time is not counted twice QVERIFY(collector.sample()["cpuPercent"].isNull()); // no counter delta put("/stat","cpu 1 0 0 1 0 0 0 0\n"); QVERIFY(collector.sample()["cpuPercent"].isNull()); // reboot/reset put("/stat","broken\n"); collector.sample(); put("/stat","cpu 2 0 0 5 0 0 0 0\n"); QVERIFY(collector.sample()["cpuPercent"].isNull()); put("/meminfo","MemTotal: 100 kB\nMemAvailable: 101 kB\n"); put("/uptime","nan\n"); put("/thermal/thermal_zone0/type","gpu-thermal\n"); sample=collector.sample(); QVERIFY(sample["memoryPercent"].isNull()); QVERIFY(sample["uptimeSeconds"].isNull()); QVERIFY(sample["socTemperatureC"].isNull()); } void unavailable() { vdi::SystemMetrics collector("/nonexistent/proc","/nonexistent/thermal"); const auto sample=collector.sample(); for (auto value : sample) QVERIFY(value.isNull()); } }; QTEST_GUILESS_MAIN(SystemMetricsTest) #include "test_system_metrics.moc"