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 16 #include "gtest/gtest.h" 17 #define private public 18 #define protected public 19 #include "perf_test.h" 20 #include "perf_test_strategy.h" 21 #undef private 22 23 using namespace std; 24 using namespace testing::ext; 25 using namespace OHOS; 26 using namespace OHOS::perftest; 27 28 class PerfTestTest : public testing::Test { 29 public: 30 ~PerfTestTest() override = default; 31 protected: 32 unique_ptr<PerfTest> perfTest_; SetUp()33 void SetUp() 34 { 35 set<PerfMetric> metrics; 36 metrics.insert(PerfMetric::DURATION); 37 ApiCallErr exception = ApiCallErr(NO_ERROR); 38 int32_t iterations = 10; 39 int32_t timeout = 10000; 40 unique_ptr<PerfTestStrategy> perfTestStrategy = make_unique<PerfTestStrategy>(metrics, "callback#1", "", 41 "com.unittest.test", iterations, timeout, exception); 42 unique_ptr<PerfTestCallback> perfTestCallback = make_unique<PerfTestCallback>(); 43 perfTest_ = make_unique<PerfTest>(move(perfTestStrategy), move(perfTestCallback)); 44 } 45 }; 46 47 HWTEST_F(PerfTestTest, testPerfTestStrategy, TestSize.Level1) 48 { 49 set<PerfMetric> metrics = perfTest_->perfTestStrategy_->GetPerfMetrics(); 50 ASSERT_EQ(metrics.size(), 1); 51 ASSERT_EQ(perfTest_->perfTestStrategy_->GetActionCodeRef(), "callback#1"); 52 ASSERT_EQ(perfTest_->perfTestStrategy_->GetResetCodeRef(), ""); 53 ASSERT_EQ(perfTest_->perfTestStrategy_->GetBundleName(), "com.unittest.test"); 54 ASSERT_EQ(perfTest_->perfTestStrategy_->GetIterations(), 10); 55 ASSERT_EQ(perfTest_->perfTestStrategy_->GetTimeout(), 10000); 56 } 57 58 HWTEST_F(PerfTestTest, testGetBundleNameByPid, TestSize.Level1) 59 { 60 perfTest_->perfTestStrategy_->bundleName_ = ""; 61 set<PerfMetric> metrics = perfTest_->perfTestStrategy_->GetPerfMetrics(); 62 ApiCallErr error = ApiCallErr(NO_ERROR); 63 perfTest_->perfTestStrategy_->GetBundleNameByPid(error); 64 ASSERT_EQ(error.code_, NO_ERROR); 65 ASSERT_TRUE(perfTest_->perfTestStrategy_->bundleName_.find("perftest_unittest") != string::npos); 66 } 67 68 HWTEST_F(PerfTestTest, testCreateDataCollections, TestSize.Level1) 69 { 70 set<PerfMetric> metrics; 71 metrics.insert(PerfMetric::DURATION); 72 metrics.insert(PerfMetric::CPU_LOAD); 73 metrics.insert(PerfMetric::CPU_USAGE); 74 metrics.insert(PerfMetric::MEMORY_RSS); 75 metrics.insert(PerfMetric::MEMORY_PSS); 76 metrics.insert(PerfMetric::APP_START_RESPONSE_TIME); 77 metrics.insert(PerfMetric::APP_START_COMPLETE_TIME); 78 metrics.insert(PerfMetric::PAGE_SWITCH_COMPLETE_TIME); 79 metrics.insert(PerfMetric::LIST_SWIPE_FPS); 80 perfTest_->perfTestStrategy_->perfMetrics_ = metrics; 81 perfTest_->perfTestStrategy_->CreateDataCollections(); 82 ASSERT_EQ(perfTest_->perfTestStrategy_->dataCollections_.size(), 9); 83 } 84 85 HWTEST_F(PerfTestTest, testGetPidByBundleName, TestSize.Level1) 86 { 87 int32_t currentPid = getpid(); 88 int32_t pid = perfTest_->GetPidByBundleName("perftest_unittest"); 89 ASSERT_EQ(currentPid, pid); 90 } 91 92 HWTEST_F(PerfTestTest, testRunTest, TestSize.Level1) 93 { 94 set<PerfMetric> metrics; 95 metrics.insert(PerfMetric::DURATION); 96 perfTest_->perfTestStrategy_->perfMetrics_ = metrics; 97 ApiCallErr error = ApiCallErr(NO_ERROR); 98 perfTest_->RunTest(error); 99 ASSERT_EQ(error.code_, NO_ERROR); 100 ASSERT_EQ(perfTest_->isMeasureComplete_, true); 101 ASSERT_EQ(perfTest_->IsMeasureRunning(), false); 102 } 103 104 HWTEST_F(PerfTestTest, testGetMeasureResult, TestSize.Level1) 105 { 106 set<PerfMetric> metrics; 107 perfTest_->perfTestStrategy_->perfMetrics_ = metrics; 108 ApiCallErr error1 = ApiCallErr(NO_ERROR); 109 perfTest_->GetMeasureResult(PerfMetric::DURATION, error1); 110 ASSERT_EQ(error1.code_, ERR_INVALID_INPUT); 111 metrics.insert(PerfMetric::DURATION); 112 perfTest_->perfTestStrategy_->perfMetrics_ = metrics; 113 ApiCallErr error2 = ApiCallErr(NO_ERROR); 114 perfTest_->GetMeasureResult(PerfMetric::DURATION, error2); 115 ASSERT_EQ(error2.code_, ERR_GET_RESULT_FAILED); 116 perfTest_->measureResult_[PerfMetric::DURATION].push_back(10); 117 perfTest_->measureResult_[PerfMetric::DURATION].push_back(20); 118 perfTest_->measureResult_[PerfMetric::DURATION].push_back(30); 119 perfTest_->perfTestStrategy_->iterations_ = 3; 120 perfTest_->isMeasureComplete_ = true; 121 ApiCallErr error3 = ApiCallErr(NO_ERROR); 122 nlohmann::json res = perfTest_->GetMeasureResult(PerfMetric::DURATION, error3); 123 ASSERT_EQ(error3.code_, NO_ERROR); 124 ASSERT_EQ(res["maximum"], 30); 125 ASSERT_EQ(res["minimum"], 10); 126 ASSERT_EQ(res["average"], 20); 127 } 128 129 HWTEST_F(PerfTestTest, testDestroy, TestSize.Level1) 130 { 131 ApiCallErr error = ApiCallErr(NO_ERROR); 132 perfTest_->Destroy(error); 133 ASSERT_EQ(error.code_, NO_ERROR); 134 } 135