• 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(true);
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: TemperatureTestCase
139  * @tc.desc: Test Temperature
140  * @tc.type: FUNC
141  */
142 HWTEST_F(SPdaemonTest, TemperatureTestCase, TestSize.Level1)
143 {
144     std::string cmd = "SP_daemon -N 1 -t";
145     std::string result = "";
146     bool flag = false;
147     auto ret = SPUtils::LoadCmd(cmd, result);
148     std::string::size_type strOne = result.find("timestamp");
149     std::string::size_type strTwo = result.find("system_h");
150     std::string::size_type strThree = result.find("gpu-thermal");
151     if ((strOne != result.npos) && ((strTwo != result.npos) || (strThree != result.npos))) {
152         flag = true;
153     }
154 
155     EXPECT_EQ(ret, true);
156     EXPECT_EQ(flag, true);
157 }
158 
159 /**
160  * @tc.name: PowerTestCase
161  * @tc.desc: Test Power
162  * @tc.type: FUNC
163  */
164 HWTEST_F(SPdaemonTest, PowerTestCase, TestSize.Level1)
165 {
166     std::string cmd = "SP_daemon -N 1 -p";
167     std::string result = "";
168     bool flag = false;
169     auto ret = SPUtils::LoadCmd(cmd, result);
170     std::string::size_type strOne = result.find("currentNow");
171     std::string::size_type strTwo = result.find("voltageNow");
172     std::string::size_type strThree = result.find("timestamp");
173     if ((strOne != result.npos) && (strTwo != result.npos)) {
174         flag = true;
175     } else if ((strThree != result.npos)) {
176         flag = true;
177     }
178 
179     EXPECT_EQ(ret, true);
180     EXPECT_EQ(flag, true);
181 }
182 
183 /**
184  * @tc.name: RamTestCase
185  * @tc.desc: Test RAM
186  * @tc.type: FUNC
187  */
188 HWTEST_F(SPdaemonTest, RamTestCase, TestSize.Level1)
189 {
190     RAM &ram = RAM::GetInstance();
191     std::string packName = "ohos.samples.ecg";
192 
193     ram.SetFirstFlag();
194     ram.SetPackageName(packName);
195     ram.ThreadGetPss();
196     ram.TriggerGetPss();
197 
198     std::string cmd = "SP_daemon -N 1 -PKG ohos.samples.ecg -r";
199     std::string result = "";
200     bool flag = false;
201     auto ret = SPUtils::LoadCmd(cmd, result);
202     std::string::size_type strOne = result.find("memAvailable");
203     std::string::size_type strTwo = result.find("memTotal");
204     if ((strOne != result.npos) && (strTwo != result.npos)) {
205         flag = true;
206     }
207 
208     EXPECT_EQ(ret, true);
209     EXPECT_EQ(flag, true);
210 }
211 
212 /**
213  * @tc.name: SnapShotTestCase
214  * @tc.desc: Test SnapShot
215  * @tc.type: FUNC
216  */
217 HWTEST_F(SPdaemonTest, SnapShotTestCase, TestSize.Level1)
218 {
219     Capture &capture = Capture::GetInstance();
220     long long catTime = 0;
221     std::string curTime = "";
222 
223     capture.SocketMessage();
224     capture.ThreadGetCatch();
225     capture.ThreadGetCatchSocket(curTime);
226     capture.TriggerGetCatch();
227     capture.TriggerGetCatchSocket(catTime);
228 
229     std::string cmd = "SP_daemon -N 1 -snapshot";
230     std::string result = "";
231     bool flag = false;
232     auto ret = SPUtils::LoadCmd(cmd, result);
233     std::string::size_type strOne = result.find("capture");
234     std::string::size_type strTwo = result.find(".png");
235     if ((strOne != result.npos) && (strTwo != result.npos)) {
236         flag = true;
237     }
238 
239     EXPECT_EQ(ret, true);
240     EXPECT_EQ(flag, true);
241 }
242 
243 /**
244  * @tc.name: NetWorkTestCase
245  * @tc.desc: Test NetWork
246  * @tc.type: FUNC
247  */
248 HWTEST_F(SPdaemonTest, NetWorkTestCase, TestSize.Level1)
249 {
250     std::string cmd = "SP_daemon -N 1 -net";
251     std::string result = "";
252     bool flag = false;
253     auto ret = SPUtils::LoadCmd(cmd, result);
254     std::string::size_type strOne = result.find("networkDown");
255     std::string::size_type strTwo = result.find("networkUp");
256     if ((strOne != result.npos) && (strTwo != result.npos)) {
257         flag = true;
258     }
259 
260     EXPECT_EQ(ret, true);
261     EXPECT_EQ(flag, true);
262 }
263 
264 /**
265  * @tc.name: StartTestCase
266  * @tc.desc: Test Start
267  * @tc.type: FUNC
268  */
269 HWTEST_F(SPdaemonTest, StartTestCase, TestSize.Level1)
270 {
271     std::string cmd = "SP_daemon -start -g";
272     std::string result = "";
273     bool flag = false;
274     auto ret = SPUtils::LoadCmd(cmd, result);
275     std::string::size_type strOne = result.find("Collection");
276     std::string::size_type strTwo = result.find("begins");
277     if ((strOne != result.npos) && (strTwo != result.npos)) {
278         flag = true;
279     }
280 
281     EXPECT_EQ(ret, true);
282     EXPECT_EQ(flag, true);
283 }
284 
285 /**
286  * @tc.name: StopTestCase
287  * @tc.desc: Test Stop
288  * @tc.type: FUNC
289  */
290 HWTEST_F(SPdaemonTest, StopTestCase, TestSize.Level1)
291 {
292     std::string cmd = "SP_daemon -stop";
293     std::string result = "";
294     bool flag = false;
295     auto ret = SPUtils::LoadCmd(cmd, result);
296     std::string::size_type strOne = result.find("Collection");
297     std::string::size_type strTwo = result.find("ended");
298     if ((strOne != result.npos) && (strTwo != result.npos)) {
299         flag = true;
300     }
301 
302     EXPECT_EQ(ret, true);
303     EXPECT_EQ(flag, true);
304 }
305 
306 /**
307  * @tc.name: ScreenTestCase
308  * @tc.desc: Test Screen
309  * @tc.type: FUNC
310  */
311 HWTEST_F(SPdaemonTest, ScreenTestCase, TestSize.Level1)
312 {
313     std::string cmd = "SP_daemon -screen";
314     std::string result = "";
315     bool flag = false;
316     auto ret = SPUtils::LoadCmd(cmd, result);
317     std::string::size_type strOne = result.find("activeMode");
318     std::string::size_type strTwo = result.find("refreshrate");
319     if ((strOne != result.npos) && (strTwo != result.npos)) {
320         flag = true;
321     }
322 
323     EXPECT_EQ(ret, true);
324     EXPECT_EQ(flag, true);
325 }
326 
327 /**
328  * @tc.name: FrameLossTestCase
329  * @tc.desc: Test FrameLoss
330  * @tc.type: FUNC
331  */
332 HWTEST_F(SPdaemonTest, FrameLossTestCase, TestSize.Level1)
333 {
334     std::string cmd = "SP_daemon -editor frameLoss";
335     std::string result = "";
336     bool flag = false;
337     auto ret = SPUtils::LoadCmd(cmd, result);
338     std::string::size_type strOne = result.find("BUNDLE_NAME");
339     std::string::size_type strTwo = result.find("TOTAL_APP_MISSED_FRAMES");
340     if ((strOne != result.npos) && (strTwo != result.npos)) {
341         flag = true;
342     }
343 
344     EXPECT_EQ(ret, true);
345     EXPECT_EQ(flag, true);
346 }
347 } // namespace OHOS
348 } // namespace SmartPerf