• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <gtest/gtest.h>
16 
17 #include "cpu_calculator.h"
18 
19 using namespace testing::ext;
20 using namespace OHOS::HiviewDFX;
21 using namespace OHOS::HiviewDFX::UCollectUtil;
22 
23 class CpuCalculatorTest : public testing::Test {
24 public:
SetUp()25     void SetUp() {};
TearDown()26     void TearDown() {};
SetUpTestCase()27     static void SetUpTestCase() {};
TearDownTestCase()28     static void TearDownTestCase() {};
29 };
30 
31 /**
32  * @tc.name: CpuCalculatorTest001
33  * @tc.desc: used to test func of CpuCalculator class
34  * @tc.type: FUNC
35 */
36 HWTEST_F(CpuCalculatorTest, CpuCalculatorTest001, TestSize.Level1)
37 {
38     auto cpuCalculator = std::make_shared<CpuCalculator>();
39     double load = cpuCalculator->CalculateCpuLoad(0, 1, 1);
40     ASSERT_EQ(load, 0);
41     load = cpuCalculator->CalculateCpuLoad(1, 0, 0);
42     ASSERT_EQ(load, 0);
43     load = cpuCalculator->CalculateCpuLoad(1000, 0, 1);
44     ASSERT_GT(load, 0);
45 }
46 
47 /**
48  * @tc.name: CpuCalculatorTest002
49  * @tc.desc: used to test func of CpuCalculator class
50  * @tc.type: FUNC
51 */
52 HWTEST_F(CpuCalculatorTest, CpuCalculatorTest002, TestSize.Level1)
53 {
54     auto cpuCalculator = std::make_shared<CpuCalculator>();
55     double usage = cpuCalculator->CalculateCpuUsage(0, 1, 1);
56     ASSERT_EQ(usage, 0);
57     usage = cpuCalculator->CalculateCpuUsage(1, 0, 0);
58     ASSERT_EQ(usage, 0);
59     usage = cpuCalculator->CalculateCpuUsage(1000, 0, 1);
60     ASSERT_GT(usage, 0);
61 }
62 
63 /**
64  * @tc.name: CpuCalculatorTest003
65  * @tc.desc: used to test func of CpuCalculator class
66  * @tc.type: FUNC
67 */
68 HWTEST_F(CpuCalculatorTest, CpuCalculatorTest003, TestSize.Level1)
69 {
70     auto cpuCalculator = std::make_shared<CpuCalculator>();
71     CpuTimeInfo curCpuTimeInfo;
72     CpuTimeInfo lastCpuTimeInfo;
73     CpuUsageInfo cpuUsageInfo = cpuCalculator->CalculateSysCpuUsageInfo(curCpuTimeInfo, lastCpuTimeInfo);
74     ASSERT_EQ(cpuUsageInfo.cpuId, "");
75     ASSERT_EQ(cpuUsageInfo.userUsage, 0);
76     ASSERT_EQ(cpuUsageInfo.niceUsage, 0);
77     ASSERT_EQ(cpuUsageInfo.systemUsage, 0);
78     ASSERT_EQ(cpuUsageInfo.idleUsage, 0);
79     curCpuTimeInfo.cpuId = "cpu0";
80     curCpuTimeInfo.userTime = 1;
81     curCpuTimeInfo.niceTime = 1;
82     curCpuTimeInfo.systemTime = 1;
83     curCpuTimeInfo.idleTime = 1;
84     curCpuTimeInfo.ioWaitTime = 1;
85     curCpuTimeInfo.irqTime = 1;
86     curCpuTimeInfo.softIrqTime = 1;
87     cpuUsageInfo = cpuCalculator->CalculateSysCpuUsageInfo(curCpuTimeInfo, lastCpuTimeInfo);
88     ASSERT_EQ(cpuUsageInfo.cpuId, "cpu0");
89     ASSERT_GT(cpuUsageInfo.userUsage, 0);
90     ASSERT_GT(cpuUsageInfo.niceUsage, 0);
91     ASSERT_GT(cpuUsageInfo.systemUsage, 0);
92     ASSERT_GT(cpuUsageInfo.idleUsage, 0);
93 }
94