• 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 
16 #include <unistd.h>
17 #include <thread>
18 #include <chrono>
19 #include "gtest/gtest.h"
20 #define private public
21 #define protected public
22 #include "duration_collection.h"
23 #include "cpu_collection.h"
24 #include "memory_collection.h"
25 #include "list_swipe_fps_collection.h"
26 #include "app_start_time_collection.h"
27 #include "page_switch_time_collection.h"
28 #undef private
29 
30 using namespace std;
31 using namespace testing::ext;
32 using namespace OHOS;
33 using namespace OHOS::perftest;
34 
35 class DataCollectionTest : public testing::Test {
36 public:
37     ~DataCollectionTest() override = default;
38 protected:
39     shared_ptr<DataCollection> dataCollection_ = make_shared<DurationCollection>(PerfMetric::DURATION);
40 };
41 
42 HWTEST_F(DataCollectionTest, testGetPidByBundleName, TestSize.Level1)
43 {
44     string bundleName = "com.unittest.test";
45     EXPECT_EQ(dataCollection_->GetPidByBundleName(bundleName), -1);
46 }
47 
48 HWTEST_F(DataCollectionTest, testIsProcessExist, TestSize.Level1)
49 {
50     int32_t currentPid = getpid();
51     EXPECT_EQ(dataCollection_->IsProcessExist(currentPid), true);
52     int32_t errorPid = 100000;
53     EXPECT_EQ(dataCollection_->IsProcessExist(errorPid), false);
54 }
55 
56 HWTEST_F(DataCollectionTest, testExecuteCommand, TestSize.Level1)
57 {
58     string command = "ls";
59     string result;
60     EXPECT_EQ(dataCollection_->ExecuteCommand(command, result), true);
61     EXPECT_NE(result, "");
62 }
63 
64 HWTEST_F(DataCollectionTest, testStartCollection, TestSize.Level1)
65 {
66     ApiCallErr error = ApiCallErr(NO_ERROR);
67     dataCollection_->StartCollection(error);
68     EXPECT_EQ(error.code_, NO_ERROR);
69 }
70 
71 HWTEST_F(DataCollectionTest, testStopCollectionAndGetResult, TestSize.Level1)
72 {
73     ApiCallErr error = ApiCallErr(NO_ERROR);
74     dataCollection_->StartCollection(error);
75     EXPECT_EQ(error.code_, NO_ERROR);
76     this_thread::sleep_for(chrono::milliseconds(1000));
77     double duration = dataCollection_->StopCollectionAndGetResult(error);
78     EXPECT_EQ(error.code_, NO_ERROR);
79     EXPECT_GT(duration, 0);
80 }
81 
82 HWTEST_F(DataCollectionTest, testStartCollectionWithWrongBundleName, TestSize.Level1)
83 {
84     shared_ptr<DataCollection> cpuCollection = make_shared<CpuCollection>(PerfMetric::CPU_USAGE);
85     ApiCallErr error1 = ApiCallErr(NO_ERROR);
86     cpuCollection->StartCollection(error1);
87     EXPECT_EQ(error1.code_, ERR_DATA_COLLECTION_FAILED);
88     shared_ptr<DataCollection> memoryCollection = make_shared<MemoryCollection>(PerfMetric::MEMORY_RSS);
89     ApiCallErr error2 = ApiCallErr(NO_ERROR);
90     cpuCollection->StartCollection(error2);
91     EXPECT_EQ(error2.code_, ERR_DATA_COLLECTION_FAILED);
92 }
93 
94 HWTEST_F(DataCollectionTest, testCpuStopCollectionAndGetResult, TestSize.Level1)
95 {
96     shared_ptr<CpuCollection> cpuCollection1 = make_shared<CpuCollection>(PerfMetric::CPU_LOAD);
97     shared_ptr<CpuCollection> cpuCollection2 = make_shared<CpuCollection>(PerfMetric::CPU_USAGE);
98     shared_ptr<CpuCollection> cpuCollection3 = make_shared<CpuCollection>(PerfMetric::DURATION);
99     cpuCollection1->cpuLoad_ = 1.00;
100     cpuCollection1->cpuUsage_ = 2.00;
101     ApiCallErr error = ApiCallErr(NO_ERROR);
102     EXPECT_EQ(cpuCollection1->StopCollectionAndGetResult(error), 1.00);
103     EXPECT_EQ(cpuCollection2->StopCollectionAndGetResult(error), 2.00);
104     EXPECT_EQ(cpuCollection3->StopCollectionAndGetResult(error), INVALID_VALUE);
105 }
106 
107 HWTEST_F(DataCollectionTest, testMemoryStopCollectionAndGetResult, TestSize.Level1)
108 {
109     shared_ptr<MemoryCollection> memoryCollection1 = make_shared<MemoryCollection>(PerfMetric::MEMORY_RSS);
110     shared_ptr<MemoryCollection> memoryCollection2 = make_shared<MemoryCollection>(PerfMetric::MEMORY_PSS);
111     shared_ptr<MemoryCollection> memoryCollection3 = make_shared<MemoryCollection>(PerfMetric::DURATION);
112     memoryCollection1->memoryRss_ = 1.00;
113     memoryCollection1->memoryPss_ = 2.00;
114     ApiCallErr error = ApiCallErr(NO_ERROR);
115     EXPECT_EQ(memoryCollection1->StopCollectionAndGetResult(error), 1.00);
116     EXPECT_EQ(memoryCollection2->StopCollectionAndGetResult(error), 2.00);
117     EXPECT_EQ(memoryCollection3->StopCollectionAndGetResult(error), INVALID_VALUE);
118 }
119 
120 HWTEST_F(DataCollectionTest, testGetSwipeState, TestSize.Level1)
121 {
122     shared_ptr<ListSwipeFpsCollection> fpsCollection = make_shared<ListSwipeFpsCollection>(PerfMetric::LIST_SWIPE_FPS);
123     fpsCollection->pid_ = 0;
124     string line1 = "bundlename-0 ( 0000) [000] .....  123.45678: trace_tag: S|0|H:APP_LIST_FLING|0|M0000";
125     string traceTag = "H:APP_LIST_FLING";
126     const vector<string> tokens1 = fpsCollection->SplitString(line1, ' ');
127     int32_t stateIndex1 = 0;
128     EXPECT_EQ(fpsCollection->GetSwipeState(tokens1, traceTag, stateIndex1), "SWIPE_START");
129     EXPECT_EQ(stateIndex1, 7);
130     EXPECT_EQ(fpsCollection->GetTime(tokens1, stateIndex1 - 2), 123.45678);
131     string line2 = "bundlename-0 (0000) [000] .....  123.45678: trace_tag: F|0|H:APP_LIST_FLING|0|M0000";
132     const vector<string> tokens2 = fpsCollection->SplitString(line2, ' ');
133     int32_t stateIndex2 = 0;
134     EXPECT_EQ(fpsCollection->GetSwipeState(tokens2, traceTag, stateIndex2), "SWIPE_FINISH");
135     EXPECT_EQ(stateIndex2, 6);
136     EXPECT_EQ(fpsCollection->GetTime(tokens2, stateIndex2 - 2), 123.45678);
137 }
138 
139 HWTEST_F(DataCollectionTest, testGetTime, TestSize.Level1)
140 {
141     shared_ptr<ListSwipeFpsCollection> fpsCollection = make_shared<ListSwipeFpsCollection>(PerfMetric::LIST_SWIPE_FPS);
142     string line = "bundlename-pid ( 0000) [000] .....  123.45678: trace_tag: S|1111|H:APP_LIST_FLING|0|M0000";
143     const vector<string> tokens = fpsCollection->SplitString(line, ' ');
144     EXPECT_EQ(fpsCollection->GetTime(tokens, 5), 123.45678);
145 }
146 
147 HWTEST_F(DataCollectionTest, testSplitString, TestSize.Level1)
148 {
149     shared_ptr<ListSwipeFpsCollection> fpsCollection = make_shared<ListSwipeFpsCollection>(PerfMetric::LIST_SWIPE_FPS);
150     string str1 = "aaa bbb ccc";
151     vector<string> tokens1 = fpsCollection->SplitString(str1, ' ');
152     EXPECT_EQ(tokens1[0], "aaa");
153     EXPECT_EQ(tokens1[1], "bbb");
154     EXPECT_EQ(tokens1[2], "ccc");
155     string str2 = "aaa,,bbb,,,ccc";
156     vector<string> tokens2 = fpsCollection->SplitString(str2, ',');
157     EXPECT_EQ(tokens2[0], "aaa");
158     EXPECT_EQ(tokens2[1], "bbb");
159     EXPECT_EQ(tokens2[2], "ccc");
160 }
161 
162 HWTEST_F(DataCollectionTest, testAppStartCollectionAndGetResult, TestSize.Level1)
163 {
164     nlohmann::json recordJson;
165     recordJson["BUNDLE_NAME"] = "com.unittest.test";
166     recordJson["RESPONSE_LATENCY"] = 50;
167     recordJson["E2E_LATENCY"] = 100;
168     shared_ptr<HiSysEventRecord> record = make_shared<HiSysEventRecord>(recordJson.dump());
169     shared_ptr<AppStartTimeCollection> appStartCollection1 =
170                                             make_shared<AppStartTimeCollection>(PerfMetric::APP_START_RESPONSE_TIME);
171     shared_ptr<AppStartTimeCollection> appStartCollection2 =
172                                             make_shared<AppStartTimeCollection>(PerfMetric::APP_START_COMPLETE_TIME);
173     ApiCallErr error = ApiCallErr(NO_ERROR);
174     appStartCollection1->bundleName_ = "com.unittest.test";
175     appStartCollection1->appStartListener_ = std::make_shared<TimeListener>(appStartCollection1->bundleName_);
176     appStartCollection1->appStartListener_->OnEvent(record);
177     EXPECT_NE(appStartCollection1->appStartListener_->GetEvent(), nullptr);
178     appStartCollection1->isCollecting_ = true;
179     EXPECT_EQ(appStartCollection1->StopCollectionAndGetResult(error), 50);
180     EXPECT_EQ(appStartCollection2->StopCollectionAndGetResult(error), 100);
181 }
182 
183 HWTEST_F(DataCollectionTest, testPageSwitchCollectionAndGetResult, TestSize.Level1)
184 {
185     nlohmann::json recordJson;
186     recordJson["BUNDLE_NAME"] = "com.unittest.test";
187     recordJson["E2E_LATENCY"] = 100;
188     shared_ptr<HiSysEventRecord> record = make_shared<HiSysEventRecord>(recordJson.dump());
189     shared_ptr<PageSwitchTimeCollection> pageSwitchCollection =
190                                         make_shared<PageSwitchTimeCollection>(PerfMetric::PAGE_SWITCH_COMPLETE_TIME);
191     ApiCallErr error = ApiCallErr(NO_ERROR);
192     pageSwitchCollection->bundleName_ = "com.unittest.test";
193     pageSwitchCollection->pageSwitchListener_ = std::make_shared<TimeListener>(pageSwitchCollection->bundleName_);
194     pageSwitchCollection->pageSwitchListener_->OnEvent(record);
195     EXPECT_NE(pageSwitchCollection->pageSwitchListener_->GetEvent(), nullptr);
196     EXPECT_EQ(pageSwitchCollection->StopCollectionAndGetResult(error), 100);
197 }