• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <string>
17 #include <thread>
18 #include <vector>
19 #include <gtest/gtest.h>
20 
21 #include "sp_utils.h"
22 #include "RAM.h"
23 #include "GPU.h"
24 #include "CPU.h"
25 #include "FPS.h"
26 #include "Temperature.h"
27 #include "Power.h"
28 #include "Capture.h"
29 #include "Network.h"
30 #include "profiler_fps.h"
31 #include "parse_click_complete_trace.h"
32 #include "parse_click_response_trace.h"
33 #include "parse_slide_fps_trace.h"
34 using namespace testing::ext;
35 using namespace std;
36 
37 namespace OHOS {
38 namespace SmartPerf {
39 class SPdaemonTest : public testing::Test {
40 public:
SetUpTestCase()41     static void SetUpTestCase() {}
TearDownTestCase()42     static void TearDownTestCase() {}
43 
SetUp()44     void SetUp() {}
TearDown()45     void TearDown() {}
46 };
47 
48 /**
49  * @tc.name: CpuTestCase
50  * @tc.desc: Test CPU
51  * @tc.type: FUNC
52  */
53 HWTEST_F(SPdaemonTest, CpuTestCase, TestSize.Level1)
54 {
55     CPU &cpu = CPU::GetInstance();
56     std::string packName = "ohos.samples.ecg";
57 
58     std::map<std::string, std::string> cpuItemData = cpu.ItemData();
59     cpu.SetPackageName(packName);
60     std::vector<CpuFreqs> cpuFreqs = cpu.GetCpuFreq();
61     std::vector<CpuUsageInfos> getCpuUsage = cpu.GetCpuUsage();
62     std::map<std::string, std::string> getSysProcessCpuLoad = cpu.GetSysProcessCpuLoad();
63 
64     std::string cmd = "SP_daemon -N 1 -PKG ohos.samples.ecg -c";
65     std::string result = "";
66     bool flag = false;
67     auto ret = SPUtils::LoadCmd(cmd, result);
68     std::string::size_type strOne = result.find("cpu0Usage");
69     std::string::size_type strTwo = result.find("cpu0idleUsage");
70     if ((strOne != result.npos) && (strTwo != result.npos)) {
71         flag = true;
72     }
73 
74     EXPECT_EQ(ret, true);
75     EXPECT_EQ(flag, true);
76 }
77 
78 /**
79  * @tc.name: GpuTestCase
80  * @tc.desc: Test GPU
81  * @tc.type: FUNC
82  */
83 HWTEST_F(SPdaemonTest, GpuTestCase, TestSize.Level1)
84 {
85     GPU &gpu = GPU::GetInstance();
86     int getGpuFreq = 0;
87     float getGpuLoad = 0.0;
88     std::map<std::string, std::string> gpuItemData = gpu.ItemData();
89     getGpuFreq = gpu.GetGpuFreq();
90     getGpuLoad = gpu.GetGpuLoad();
91 
92     std::string cmd = "SP_daemon -N 1 -g";
93     std::string result = "";
94     bool flag = false;
95     auto ret = SPUtils::LoadCmd(cmd, result);
96     std::string::size_type strOne = result.find("gpuFrequency");
97     std::string::size_type strTwo = result.find("gpuLoad");
98     if ((strOne != result.npos) && (strTwo != result.npos)) {
99         flag = true;
100     }
101 
102     EXPECT_EQ(ret, true);
103     EXPECT_EQ(flag, true);
104 }
105 
106 /**
107  * @tc.name: FpsTestCase
108  * @tc.desc: Test FPS
109  * @tc.type: FUNC
110  */
111 HWTEST_F(SPdaemonTest, FpsTestCase, TestSize.Level1)
112 {
113     FPS &fps = FPS::GetInstance();
114     std::string packName = "ohos.samples.ecg";
115     std::string surfaceViewName;
116     FpsInfo fpsInfoResult;
117 
118     fps.SetFpsCurrentFpsTime(fpsInfoResult);
119     fps.SetPackageName(packName);
120     fps.SetLayerName(surfaceViewName);
121     fps.CalcFpsAndJitters();
122 
123     std::string cmd = "SP_daemon -N 1 -PKG ohos.samples.ecg -f";
124     std::string result = "";
125     bool flag = false;
126     auto ret = SPUtils::LoadCmd(cmd, result);
127     std::string::size_type strOne = result.find("fpsJitters");
128     std::string::size_type strTwo = result.find("timestamp");
129     if ((strOne != result.npos) && (strTwo != result.npos)) {
130         flag = true;
131     }
132 
133     EXPECT_EQ(ret, true);
134     EXPECT_EQ(flag, true);
135 }
136 
137 /**
138  * @tc.name: RamTestCase
139  * @tc.desc: Test RAM
140  * @tc.type: FUNC
141  */
142 HWTEST_F(SPdaemonTest, RamTestCase, TestSize.Level1)
143 {
144     RAM &ram = RAM::GetInstance();
145     std::string packName = "ohos.samples.ecg";
146 
147     ram.SetFirstFlag();
148     ram.SetPackageName(packName);
149     ram.ThreadGetPss();
150     ram.TriggerGetPss();
151 
152     std::string cmd = "SP_daemon -N 1 -PKG ohos.samples.ecg -r";
153     std::string result = "";
154     bool flag = false;
155     auto ret = SPUtils::LoadCmd(cmd, result);
156     std::string::size_type strOne = result.find("memAvailable");
157     std::string::size_type strTwo = result.find("memTotal");
158     if ((strOne != result.npos) && (strTwo != result.npos)) {
159         flag = true;
160     }
161 
162     EXPECT_EQ(ret, true);
163     EXPECT_EQ(flag, true);
164 }
165 
166 /**
167  * @tc.name: SnapShotTestCase
168  * @tc.desc: Test SnapShot
169  * @tc.type: FUNC
170  */
171 HWTEST_F(SPdaemonTest, SnapShotTestCase, TestSize.Level1)
172 {
173     Capture &capture = Capture::GetInstance();
174     long long catTime = 0;
175     std::string curTime = "";
176 
177     capture.SocketMessage();
178     capture.ThreadGetCatch();
179     capture.ThreadGetCatchSocket(curTime);
180     capture.TriggerGetCatch();
181     capture.TriggerGetCatchSocket(catTime);
182 
183     std::string cmd = "SP_daemon -N 1 -snapshot";
184     std::string result = "";
185     bool flag = false;
186     auto ret = SPUtils::LoadCmd(cmd, result);
187     std::string::size_type strOne = result.find("capture");
188     std::string::size_type strTwo = result.find(".png");
189     if ((strOne != result.npos) && (strTwo != result.npos)) {
190         flag = true;
191     }
192 
193     EXPECT_EQ(ret, true);
194     EXPECT_EQ(flag, true);
195 }
196 
197 /**
198  * @tc.name: NetWorkTestCase
199  * @tc.desc: Test NetWork
200  * @tc.type: FUNC
201  */
202 HWTEST_F(SPdaemonTest, NetWorkTestCase, TestSize.Level1)
203 {
204     std::string cmd = "SP_daemon -N 1 -net";
205     std::string result = "";
206     bool flag = false;
207     auto ret = SPUtils::LoadCmd(cmd, result);
208     std::string::size_type strOne = result.find("networkDown");
209     std::string::size_type strTwo = result.find("networkUp");
210     if ((strOne != result.npos) && (strTwo != result.npos)) {
211         flag = true;
212     }
213 
214     EXPECT_EQ(ret, true);
215     EXPECT_EQ(flag, true);
216 }
217 
218 /**
219  * @tc.name: StartTestCase
220  * @tc.desc: Test Start
221  * @tc.type: FUNC
222  */
223 HWTEST_F(SPdaemonTest, StartTestCase, TestSize.Level1)
224 {
225     std::string cmd = "SP_daemon -start -g";
226     std::string result = "";
227     bool flag = false;
228     auto ret = SPUtils::LoadCmd(cmd, result);
229     std::string::size_type strOne = result.find("Collection");
230     std::string::size_type strTwo = result.find("begins");
231     if ((strOne != result.npos) && (strTwo != result.npos)) {
232         flag = true;
233     }
234 
235     EXPECT_EQ(ret, true);
236     EXPECT_EQ(flag, true);
237 }
238 
239 /**
240  * @tc.name: StopTestCase
241  * @tc.desc: Test Stop
242  * @tc.type: FUNC
243  */
244 HWTEST_F(SPdaemonTest, StopTestCase, TestSize.Level1)
245 {
246     std::string cmd = "SP_daemon -stop";
247     std::string result = "";
248     bool flag = false;
249     auto ret = SPUtils::LoadCmd(cmd, result);
250     std::string::size_type strOne = result.find("Collection");
251     std::string::size_type strTwo = result.find("ended");
252     if ((strOne != result.npos) && (strTwo != result.npos)) {
253         flag = true;
254     }
255 
256     EXPECT_EQ(ret, true);
257     EXPECT_EQ(flag, true);
258 }
259 
260 /**
261  * @tc.name: ProfilerFpsTestCase
262  * @tc.desc: Test ProfilerFps
263  * @tc.type: FUNC
264  */
265 HWTEST_F(SPdaemonTest, ProfilerFpsTestCase, TestSize.Level1)
266 {
267     ProfilerFPS &profiler = ProfilerFPS::GetInstance();
268     std::string packName = "ohos.samples.ecg";
269     int sectionsNum = 10;
270     FpsInfoProfiler fpsInfoResult;
271     int nums = 10;
272     int printCount = 10;
273     long long msStartTime = 0;
274     int numb = 20;
275     long long harTime = 0;
276 
277     profiler.GetResultFPS(sectionsNum);
278     profiler.CalcFpsAndJitters();
279     profiler.GetSectionsFps(fpsInfoResult, nums);
280     profiler.GetSectionsPrint(printCount, msStartTime, numb, harTime);
281 
282     std::string cmd = "SP_daemon -profilerfps 10";
283     std::string result = "";
284     bool flag = false;
285     auto ret = SPUtils::LoadCmd(cmd, result);
286     std::string::size_type strOne = result.find("set");
287     std::string::size_type strTwo = result.find("success");
288     if ((strOne != result.npos) && (strTwo != result.npos)) {
289         flag = true;
290     }
291 
292     EXPECT_EQ(ret, true);
293     EXPECT_EQ(flag, true);
294 }
295 
296 /**
297  * @tc.name: ScreenTestCase
298  * @tc.desc: Test Screen
299  * @tc.type: FUNC
300  */
301 HWTEST_F(SPdaemonTest, ScreenTestCase, TestSize.Level1)
302 {
303     std::string cmd = "SP_daemon -screen";
304     std::string result = "";
305     bool flag = false;
306     auto ret = SPUtils::LoadCmd(cmd, result);
307     std::string::size_type strOne = result.find("activeMode");
308     std::string::size_type strTwo = result.find("refreshrate");
309     if ((strOne != result.npos) && (strTwo != result.npos)) {
310         flag = true;
311     }
312 
313     EXPECT_EQ(ret, true);
314     EXPECT_EQ(flag, true);
315 }
316 
317 /**
318  * @tc.name: FrameLossTestCase
319  * @tc.desc: Test FrameLoss
320  * @tc.type: FUNC
321  */
322 HWTEST_F(SPdaemonTest, FrameLossTestCase, TestSize.Level1)
323 {
324     std::string cmd = "SP_daemon -editor frameLoss";
325     std::string result = "";
326     bool flag = false;
327     auto ret = SPUtils::LoadCmd(cmd, result);
328     std::string::size_type strOne = result.find("BUNDLE_NAME");
329     std::string::size_type strTwo = result.find("TOTAL_APP_MISSED_FRAMES");
330     if ((strOne != result.npos) && (strTwo != result.npos)) {
331         flag = true;
332     }
333 
334     EXPECT_EQ(ret, true);
335     EXPECT_EQ(flag, true);
336 }
337 } // namespace OHOS
338 } // namespace SmartPerf