Debian Codegraph @

tests/src/test_cpuutils.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include <catch2/catch_all.hpp>
5
6 #include "core/components/cpuutils.h"
7
8 namespace Tests::Utils::CPU {
9
10 TEST_CASE("CPU utils tests", "[Utils][CPU]")
11 {
12 SECTION("parseProcCpuInfo")
13 {
14 // clang-format off
15 std::vector<std::string> input{"processor : 0",
16 "target : data"};
17 // clang-format on
18
19 SECTION("Returns target data")
20 {
21 auto data = ::Utils::CPU::parseProcCpuInfo(input, 0, "target");
22 REQUIRE(data.has_value());
23 REQUIRE(*data == "data");
24 }
25
26 SECTION("Returns nothing when input doesn't have the requested processor")
27 {
28 auto data = ::Utils::CPU::parseProcCpuInfo(input, 10, "target");
29 REQUIRE_FALSE(data.has_value());
30 }
31
32 SECTION("Returns nothing when input doesn't have the requested target on "
33 "the processor")
34 {
35 auto data = ::Utils::CPU::parseProcCpuInfo(input, 0, "wrong_target");
36 REQUIRE_FALSE(data.has_value());
37 }
38 }
39
40 SECTION("parseProcStat")
41 {
42 SECTION("Returns the CPU aggregate stats from /proc/stat info")
43 {
44 std::vector<std::string> input{
45 "cpu 141434 264 40474 2021588 12439 5752 2750 0 0 0"};
46 auto stat = ::Utils::CPU::parseProcStat(input);
47 REQUIRE(stat.has_value());
48 REQUIRE(stat->user == 141434);
49 REQUIRE(stat->nice == 264);
50 REQUIRE(stat->system == 40474);
51 REQUIRE(stat->idle == 2021588);
52 REQUIRE(stat->ioWait == 12439);
53 REQUIRE(stat->irq == 5752);
54 REQUIRE(stat->softIrq == 2750);
55 REQUIRE(stat->steal == 0);
56 REQUIRE(stat->total ==
57 141434 + 264 + 40474 + 2021588 + 12439 + 5752 + 2750 + 0);
58 }
59
60 SECTION("Returns nothing when some stat values are missing from the cpu "
61 "aggregate line")
62 {
63 std::vector<std::string> input{"cpu 141434 264 40474 2021588 0 0 0"};
64 auto stat = ::Utils::CPU::parseProcStat(input);
65 REQUIRE_FALSE(stat.has_value());
66 }
67
68 SECTION("Returns nothing when the cpu aggregate line is missing")
69 {
70 std::vector<std::string> input{"other data"};
71 auto stat = ::Utils::CPU::parseProcStat(input);
72 REQUIRE_FALSE(stat.has_value());
73 }
74 }
75
76 SECTION("computeCPUUsage")
77 {
78 SECTION("Returns CPU usage")
79 {
80 std::uint64_t total = 723998 + 5703 + 159187 + 9095956 + 22936 + 23633 +
81 10115 + 0;
82 auto statT0 = ::Utils::CPU::Stat{723998, 5703, 159187, 9095956, 22936,
83 23633, 10115, 0, total};
84 total = 725077 + 5703 + 159248 + 9095956 + 22936 + 23638 + 10116 + 0;
85 auto statT1 = ::Utils::CPU::Stat{725077, 5703, 159248, 9095956, 22936,
86 23638, 10116, 0, total};
87 auto usage = ::Utils::CPU::computeCPUUsage(statT0, statT1);
88 REQUIRE(usage == 100);
89
90 total = 664215 + 2940 + 147770 + 6752849 + 19569 + 21752 + 9330 + 0;
91 statT0 = ::Utils::CPU::Stat{664215, 2940, 147770, 6752849, 19569,
92 21752, 9330, 0, total};
93 total = 664215 + 2940 + 147770 + 6754574 + 19583 + 21752 + 9330 + 0;
94 statT1 = ::Utils::CPU::Stat{664215, 2940, 147770, 6754574, 19583,
95 21752, 9330, 0, total};
96 usage = ::Utils::CPU::computeCPUUsage(statT0, statT1);
97 REQUIRE(usage == 0);
98 }
99 }
100 }
101
102 } // namespace Tests::Utils::CPU