1 /* 2 * Copyright (c) 2023 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 <iostream> 16 17 #include "cpu_collector.h" 18 19 #include <gtest/gtest.h> 20 21 using namespace testing::ext; 22 using namespace OHOS::HiviewDFX; 23 using namespace OHOS::HiviewDFX::UCollectUtil; 24 25 class CpuCollectorTest : public testing::Test { 26 public: SetUp()27 void SetUp() {}; TearDown()28 void TearDown() {}; SetUpTestCase()29 static void SetUpTestCase() {}; TearDownTestCase()30 static void TearDownTestCase() {}; 31 }; 32 33 /** 34 * @tc.name: CpuCollectorTest001 35 * @tc.desc: used to test CpuCollector.CollectSysCpuLoad 36 * @tc.type: FUNC 37 */ 38 HWTEST_F(CpuCollectorTest, CpuCollectorTest001, TestSize.Level1) 39 { 40 std::shared_ptr<CpuCollector> collector = CpuCollector::Create(); 41 CollectResult<SysCpuLoad> data = collector->CollectSysCpuLoad(); 42 std::cout << "collect system cpu load result" << data.retCode << std::endl; 43 ASSERT_TRUE(data.retCode == UcError::SUCCESS); 44 } 45 46 /** 47 * @tc.name: CpuCollectorTest002 48 * @tc.desc: used to test CpuCollector.CollectSysCpuUsage 49 * @tc.type: FUNC 50 */ 51 HWTEST_F(CpuCollectorTest, CpuCollectorTest002, TestSize.Level1) 52 { 53 std::shared_ptr<CpuCollector> collector = CpuCollector::Create(); 54 CollectResult<SysCpuUsage> data = collector->CollectSysCpuUsage(); 55 std::cout << "collect system cpu usage result" << data.retCode << std::endl; 56 ASSERT_TRUE(data.retCode == UcError::SUCCESS); 57 } 58 59 /** 60 * @tc.name: CpuCollectorTest003 61 * @tc.desc: used to test CpuCollector.CollectProcessCpuUsage 62 * @tc.type: FUNC 63 */ 64 HWTEST_F(CpuCollectorTest, CpuCollectorTest003, TestSize.Level1) 65 { 66 std::shared_ptr<CpuCollector> collector = CpuCollector::Create(); 67 CollectResult<ProcessCpuUsage> data = collector->CollectProcessCpuUsage(1000); 68 std::cout << "collect process cpu usage result" << data.retCode << std::endl; 69 ASSERT_TRUE(data.retCode == UcError::SUCCESS); 70 } 71 72 /** 73 * @tc.name: CpuCollectorTest004 74 * @tc.desc: used to test CpuCollector.CollectProcessCpuLoad 75 * @tc.type: FUNC 76 */ 77 HWTEST_F(CpuCollectorTest, CpuCollectorTest004, TestSize.Level1) 78 { 79 std::shared_ptr<CpuCollector> collector = CpuCollector::Create(); 80 CollectResult<ProcessCpuLoad> data = collector->CollectProcessCpuLoad(1000); 81 std::cout << "collect process cpu load result" << data.retCode << std::endl; 82 ASSERT_TRUE(data.retCode == UcError::SUCCESS); 83 } 84 85 /** 86 * @tc.name: CpuCollectorTest005 87 * @tc.desc: used to test CpuCollector.CollectCpuFreqStat 88 * @tc.type: FUNC 89 */ 90 HWTEST_F(CpuCollectorTest, CpuCollectorTest005, TestSize.Level1) 91 { 92 std::shared_ptr<CpuCollector> collector = CpuCollector::Create(); 93 CollectResult<CpuFreqStat> data = collector->CollectCpuFreqStat(); 94 std::cout << "collect system cpu freq stat result" << data.retCode << std::endl; 95 ASSERT_TRUE(data.retCode == UcError::SUCCESS); 96 } 97 98 /** 99 * @tc.name: CpuCollectorTest006 100 * @tc.desc: used to test CpuCollector.CollectCpuFrequency 101 * @tc.type: FUNC 102 */ 103 HWTEST_F(CpuCollectorTest, CpuCollectorTest006, TestSize.Level1) 104 { 105 std::shared_ptr<CpuCollector> collector = CpuCollector::Create(); 106 CollectResult<std::vector<CpuFreq>> data = collector->CollectCpuFrequency(); 107 std::cout << "collect system cpu frequency result" << data.retCode << std::endl; 108 ASSERT_TRUE(data.retCode == UcError::SUCCESS); 109 }