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 "cpu_usage_test.h"
17 using namespace std;
18 using namespace testing::ext;
19 using namespace std::chrono;
20 namespace OHOS {
21 namespace Developtools {
22 namespace Hiperf {
23 class CpuUsageTest : public testing::Test {
24 public:
25 static void SetUpTestCase(void);
26 static void TearDownTestCase(void);
27 void SetUp();
28 void TearDown();
29
30 pid_t GetPidByProcessName(std::string procName);
31
32 int GetVmRSSLine(pid_t pid);
33
34 const char* GetItems(const char* buffer, unsigned int item);
35
36 unsigned long GetCpuTotalUsage();
37
38 unsigned long GetCpuProcUsage(int pid);
39
40 float GetCpuUsageRatio(int pid);
41
42 float GetAverageCpuUsage(pid_t pid, uint64_t timeOut);
43 };
44
SetUpTestCase()45 void CpuUsageTest::SetUpTestCase() {}
46
TearDownTestCase()47 void CpuUsageTest::TearDownTestCase() {}
48
SetUp()49 void CpuUsageTest::SetUp() {}
50
TearDown()51 void CpuUsageTest::TearDown() {}
52
GetPidByProcessName(std::string procName)53 pid_t CpuUsageTest::GetPidByProcessName(std::string procName)
54 {
55 FILE *fp = nullptr;
56 char buf[100];
57 pid_t pid = -1;
58 std::string cmd = "pidof " + procName;
59 if ((fp = popen(cmd.c_str(), "r")) != nullptr) {
60 if (fgets(buf, sizeof(buf), fp) != nullptr) {
61 pid = atoi(buf);
62 }
63 }
64 if (fp != nullptr) {
65 pclose(fp);
66 }
67 return pid;
68 }
69
GetVmRSSLine(pid_t pid)70 int CpuUsageTest::GetVmRSSLine(pid_t pid)
71 {
72 int line = 0;
73 std::string fileName = "/proc" + std::to_string(pid) + "/stat";
74 std::ifstream in(fileName, std::ios::in);
75 std::string tmp;
76 if (in.fail()) {
77 return 0;
78 } else {
79 while (getline(in, tmp)) {
80 line++;
81 if (tmp.find("VmRSS")) {
82 return line;
83 }
84 }
85 }
86 in.close();
87 return -1;
88 }
89
GetItems(const char * buffer,unsigned int item)90 const char* CpuUsageTest::GetItems(const char* buffer, unsigned int item)
91 {
92 // read from buffer by offset
93 const char* p = buffer;
94 int len = strlen(buffer);
95 int count = 0;
96 for (int i = 0; i < len; ++i) {
97 if (*p == ' ') {
98 count++;
99 if (count == item - 1) {
100 p++;
101 break;
102 }
103 }
104 p++;
105 }
106 return p;
107 }
108
GetCpuTotalUsage()109 unsigned long CpuUsageTest::GetCpuTotalUsage()
110 {
111 // get total cpu usage time from /proc/stat
112
113 // different mode cpu usage time
114 unsigned long userTime;
115 unsigned long niceTime;
116 unsigned long systemTime;
117 unsigned long idleTime;
118
119 FILE *fd = nullptr;
120 char buff[1024] = {0};
121 std::string fileName = "/proc/stat";
122 fd = fopen(fileName.c_str(), "r");
123 if (fd == nullptr) {
124 return 0;
125 }
126 fgets (buff, sizeof(buff), fd);
127 char name[64] = {0};
128 // get first line cpu time data
129 stringstream stream;
130 stream << buff;
131 stream >> name >> userTime >> niceTime >> systemTime >> idleTime;
132 fclose(fd);
133 stream.clear();
134 return (userTime + niceTime + systemTime + idleTime);
135 }
136
GetCpuProcUsage(int pid)137 unsigned long CpuUsageTest::GetCpuProcUsage(int pid)
138 {
139 // get cpu usage of specific pid
140
141 unsigned int tmpPid;
142 unsigned long utime; // user time
143 unsigned long stime; // kernel time
144 unsigned long cutime; // all usertime
145 unsigned long cstime; // all dead time
146
147 FILE *fd = nullptr;
148 char lineBuff[1024] = {0};
149 std::string fileName = "/proc" + std::to_string(pid) + "/stat";
150 fd = fopen(fileName.c_str(), "r");
151 if (fd == nullptr) {
152 return 0;
153 }
154 fgets(lineBuff, sizeof(lineBuff), fd);
155 stringstream stream;
156 stream << lineBuff;
157 stream >> tmpPid;
158 const char* q = GetItems(lineBuff, PROCESS_ITEM);
159 stream.clear();
160 stream << q;
161 stream >> utime >> stime >> cutime >> cstime;
162 fclose(fd);
163
164 return (utime + stime + cutime + cstime);
165 }
166
GetCpuUsageRatio(int pid)167 float CpuUsageTest::GetCpuUsageRatio(int pid)
168 {
169 unsigned long totalCpuTimepPrev;
170 unsigned long totalcputimeCur;
171 unsigned long procCpuTimepPrev;
172 unsigned long proccputimeCur;
173
174 totalCpuTimepPrev = GetCpuTotalUsage();
175 procCpuTimepPrev = GetCpuProcUsage(pid);
176
177 // sleep 200ms to get two point cpu usage snapshots
178 int timeInterval = 200000;
179 usleep(timeInterval);
180
181 totalcputimeCur = GetCpuTotalUsage();
182 proccputimeCur = GetCpuProcUsage(pid);
183
184 float pcpu = 0.0;
185 if (totalcputimeCur - totalCpuTimepPrev != 0) {
186 pcpu = (proccputimeCur - procCpuTimepPrev) / float(totalcputimeCur - totalCpuTimepPrev);
187 }
188
189 int cpuNum = get_nprocs();
190 // multi cpu machine should multiply cpu number
191 pcpu *= cpuNum;
192 return pcpu;
193 }
194
GetAverageCpuUsage(pid_t pid,uint64_t timeOut)195 float CpuUsageTest::GetAverageCpuUsage(pid_t pid, uint64_t timeOut)
196 {
197 float cpuUsage = 0.0;
198 int count = 0;
199 auto startTime = std::chrono::steady_clock::now();
200 while (true) {
201 ++count;
202 cpuUsage += GetCpuUsageRatio(pid);
203 auto thisTime = std::chrono::steady_clock::now();
204 if ((uint64_t)duration_cast<milliseconds>(thisTime - startTime).count()
205 > timeOut) {
206 break;
207 }
208 }
209 cpuUsage = HUNDRED * cpuUsage / count;
210 return cpuUsage;
211 }
212
213 /**
214 * @tc.name: recordCpuUsageF100_FP_SYSTEM
215 * @tc.desc: test hiperf record system wide cpu usage within required limit
216 * @tc.type: FUNC
217 */
218 HWTEST_F(CpuUsageTest, recordCpuUsageF100_FP_SYSTEM, TestSize.Level1)
219 {
220 std::string cmd = "hiperf record -a -f 100 -s fp -d 10";
221 std::thread perf(system, cmd.c_str());
222 perf.detach();
223 pid_t pid = GetPidByProcessName("hiperf");
224 uint64_t timeOut = 10000;
225 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
226 EXPECT_LE(cpuUsage, F100_FP_CPU_LIMIT_SYSTEM);
227 }
228
229 /**
230 * @tc.name: recordCpuUsageF500_FP_SYSTEM
231 * @tc.desc: test hiperf record system wide cpu usage within required limit
232 * @tc.type: FUNC
233 */
234 HWTEST_F(CpuUsageTest, recordCpuUsageF500_FP_SYSTEM, TestSize.Level1)
235 {
236 std::string cmd = "hiperf record -a -f 500 -s fp -d 10";
237 std::thread perf(system, cmd.c_str());
238 perf.detach();
239 pid_t pid = GetPidByProcessName("hiperf");
240 uint64_t timeOut = 10000;
241 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
242 EXPECT_LE(cpuUsage, F500_FP_CPU_LIMIT_SYSTEM);
243 }
244
245 /**
246 * @tc.name: recordCpuUsageF1000_FP_SYSTEM
247 * @tc.desc: test hiperf record system wide cpu usage within required limit
248 * @tc.type: FUNC
249 */
250 HWTEST_F(CpuUsageTest, recordCpuUsageF1000_FP_SYSTEM, TestSize.Level1)
251 {
252 std::string cmd = "hiperf record -a -f 1000 -s fp -d 10";
253 std::thread perf(system, cmd.c_str());
254 perf.detach();
255 pid_t pid = GetPidByProcessName("hiperf");
256 uint64_t timeOut = 10000;
257 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
258 EXPECT_LE(cpuUsage, F1000_FP_CPU_LIMIT_SYSTEM);
259 }
260
261 /**
262 * @tc.name: recordCpuUsageF2000_FP_SYSTEM
263 * @tc.desc: test hiperf record system wide cpu usage within required limit
264 * @tc.type: FUNC
265 */
266 HWTEST_F(CpuUsageTest, recordCpuUsageF2000_FP_SYSTEM, TestSize.Level1)
267 {
268 std::string cmd = "hiperf record -a -f 2000 -s fp -d 10";
269 std::thread perf(system, cmd.c_str());
270 perf.detach();
271 pid_t pid = GetPidByProcessName("hiperf");
272 uint64_t timeOut = 10000;
273 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
274 EXPECT_LE(cpuUsage, F2000_FP_CPU_LIMIT_SYSTEM);
275 }
276
277 /**
278 * @tc.name: recordCpuUsageF4000_FP_SYSTEM
279 * @tc.desc: test hiperf record system wide cpu usage within required limit
280 * @tc.type: FUNC
281 */
282 HWTEST_F(CpuUsageTest, recordCpuUsageF4000_FP_SYSTEM, TestSize.Level1)
283 {
284 std::string cmd = "hiperf record -a -f 4000 -s fp -d 10";
285 std::thread perf(system, cmd.c_str());
286 perf.detach();
287 pid_t pid = GetPidByProcessName("hiperf");
288 uint64_t timeOut = 10000;
289 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
290 EXPECT_LE(cpuUsage, F4000_FP_CPU_LIMIT_SYSTEM);
291 }
292
293 /**
294 * @tc.name: recordCpuUsageF8000_FP_SYSTEM
295 * @tc.desc: test hiperf record system wide cpu usage within required limit
296 * @tc.type: FUNC
297 */
298 HWTEST_F(CpuUsageTest, recordCpuUsageF8000_FP_SYSTEM, TestSize.Level1)
299 {
300 std::string cmd = "hiperf record -a -f 8000 -s fp -d 10";
301 std::thread perf(system, cmd.c_str());
302 perf.detach();
303 pid_t pid = GetPidByProcessName("hiperf");
304 uint64_t timeOut = 10000;
305 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
306 EXPECT_LE(cpuUsage, F8000_FP_CPU_LIMIT_SYSTEM);
307 }
308
309 /**
310 * @tc.name: recordCpuUsageF100_DWARF_SYSTEM
311 * @tc.desc: test hiperf record system wide cpu usage within required limit
312 * @tc.type: FUNC
313 */
314 HWTEST_F(CpuUsageTest, recordCpuUsageF100_DWARF_SYSTEM, TestSize.Level1)
315 {
316 std::string cmd = "hiperf record -a -f 100 -s dwarf -d 10";
317 std::thread perf(system, cmd.c_str());
318 perf.detach();
319 pid_t pid = GetPidByProcessName("hiperf");
320 uint64_t timeOut = 10000;
321 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
322 EXPECT_LE(cpuUsage, F100_DWARF_CPU_LIMIT_SYSTEM);
323 }
324
325 /**
326 * @tc.name: recordCpuUsageF500_DWARF_SYSTEM
327 * @tc.desc: test hiperf record system wide cpu usage within required limit
328 * @tc.type: FUNC
329 */
330 HWTEST_F(CpuUsageTest, recordCpuUsageF500_DWARF_SYSTEM, TestSize.Level1)
331 {
332 std::string cmd = "hiperf record -a -f 500 -s dwarf -d 10";
333 std::thread perf(system, cmd.c_str());
334 perf.detach();
335 pid_t pid = GetPidByProcessName("hiperf");
336 uint64_t timeOut = 10000;
337 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
338 EXPECT_LE(cpuUsage, F500_DWARF_CPU_LIMIT_SYSTEM);
339 }
340
341 /**
342 * @tc.name: recordCpuUsageF1000_DWARF_SYSTEM
343 * @tc.desc: test hiperf record system wide cpu usage within required limit
344 * @tc.type: FUNC
345 */
346 HWTEST_F(CpuUsageTest, recordCpuUsageF1000_DWARF_SYSTEM, TestSize.Level1)
347 {
348 std::string cmd = "hiperf record -a -f 1000 -s dwarf -d 10";
349 std::thread perf(system, cmd.c_str());
350 perf.detach();
351 pid_t pid = GetPidByProcessName("hiperf");
352 uint64_t timeOut = 10000;
353 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
354 EXPECT_LE(cpuUsage, F1000_DWARF_CPU_LIMIT_SYSTEM);
355 }
356
357 /**
358 * @tc.name: recordCpuUsageF2000_DWARF_SYSTEM
359 * @tc.desc: test hiperf record system wide cpu usage within required limit
360 * @tc.type: FUNC
361 */
362 HWTEST_F(CpuUsageTest, recordCpuUsageF2000_DWARF_SYSTEM, TestSize.Level1)
363 {
364 std::string cmd = "hiperf record -a -f 2000 -s dwarf -d 10";
365 std::thread perf(system, cmd.c_str());
366 perf.detach();
367 pid_t pid = GetPidByProcessName("hiperf");
368 uint64_t timeOut = 10000;
369 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
370 EXPECT_LE(cpuUsage, F2000_DWARF_CPU_LIMIT_SYSTEM);
371 }
372
373 /**
374 * @tc.name: recordCpuUsageF4000_DWARF_SYSTEM
375 * @tc.desc: test hiperf record system wide cpu usage within required limit
376 * @tc.type: FUNC
377 */
378 HWTEST_F(CpuUsageTest, recordCpuUsageF4000_DWARF_SYSTEM, TestSize.Level1)
379 {
380 std::string cmd = "hiperf record -a -f 4000 -s dwarf -d 10";
381 std::thread perf(system, cmd.c_str());
382 perf.detach();
383 pid_t pid = GetPidByProcessName("hiperf");
384 uint64_t timeOut = 10000;
385 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
386 EXPECT_LE(cpuUsage, F4000_DWARF_CPU_LIMIT_SYSTEM);
387 }
388
389 /**
390 * @tc.name: recordCpuUsageF8000_DWARF_SYSTEM
391 * @tc.desc: test hiperf record system wide cpu usage within required limit
392 * @tc.type: FUNC
393 */
394 HWTEST_F(CpuUsageTest, recordCpuUsageF8000_DWARF_SYSTEM, TestSize.Level1)
395 {
396 std::string cmd = "hiperf record -a -f 8000 -s dwarf -d 10";
397 std::thread perf(system, cmd.c_str());
398 perf.detach();
399 pid_t pid = GetPidByProcessName("hiperf");
400 uint64_t timeOut = 10000;
401 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
402 EXPECT_LE(cpuUsage, F8000_DWARF_CPU_LIMIT_SYSTEM);
403 }
404
405 /**
406 * @tc.name: recordCpuUsageF100_FP_PROCESS
407 * @tc.desc: test hiperf record one process cpu usage within required limit
408 * @tc.type: FUNC
409 */
410 HWTEST_F(CpuUsageTest, recordCpuUsageF100_FP_PROCESS, TestSize.Level1)
411 {
412 std::string cmd = "hiperf record --app com.ohos.systemui -f 100 -s fp -d 10";
413 std::thread perf(system, cmd.c_str());
414 perf.detach();
415 pid_t pid = GetPidByProcessName("hiperf");
416 uint64_t timeOut = 10000;
417 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
418 EXPECT_LE(cpuUsage, F100_FP_CPU_LIMIT_PROCESS);
419 }
420
421 /**
422 * @tc.name: recordCpuUsageF500_FP_PROCESS
423 * @tc.desc: test hiperf record one process cpu usage within required limit
424 * @tc.type: FUNC
425 */
426 HWTEST_F(CpuUsageTest, recordCpuUsageF500_FP_PROCESS, TestSize.Level1)
427 {
428 std::string cmd = "hiperf record --app com.ohos.systemui -f 500 -s fp -d 10";
429 std::thread perf(system, cmd.c_str());
430 perf.detach();
431 pid_t pid = GetPidByProcessName("hiperf");
432 uint64_t timeOut = 10000;
433 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
434 EXPECT_LE(cpuUsage, F500_FP_CPU_LIMIT_PROCESS);
435 }
436
437 /**
438 * @tc.name: recordCpuUsageF1000_FP_PROCESS
439 * @tc.desc: test hiperf record one process cpu usage within required limit
440 * @tc.type: FUNC
441 */
442 HWTEST_F(CpuUsageTest, recordCpuUsageF1000_FP_PROCESS, TestSize.Level1)
443 {
444 std::string cmd = "hiperf record --app com.ohos.systemui -f 1000 -s fp -d 10";
445 std::thread perf(system, cmd.c_str());
446 perf.detach();
447 pid_t pid = GetPidByProcessName("hiperf");
448 uint64_t timeOut = 10000;
449 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
450 EXPECT_LE(cpuUsage, F1000_FP_CPU_LIMIT_PROCESS);
451 }
452
453 /**
454 * @tc.name: recordCpuUsageF2000_FP_PROCESS
455 * @tc.desc: test hiperf record one process cpu usage within required limit
456 * @tc.type: FUNC
457 */
458 HWTEST_F(CpuUsageTest, recordCpuUsageF2000_FP_PROCESS, TestSize.Level1)
459 {
460 std::string cmd = "hiperf record --app com.ohos.systemui -f 2000 -s fp -d 10";
461 std::thread perf(system, cmd.c_str());
462 perf.detach();
463 pid_t pid = GetPidByProcessName("hiperf");
464 uint64_t timeOut = 10000;
465 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
466 EXPECT_LE(cpuUsage, F2000_FP_CPU_LIMIT_PROCESS);
467 }
468
469 /**
470 * @tc.name: recordCpuUsageF4000_FP_PROCESS
471 * @tc.desc: test hiperf record one process cpu usage within required limit
472 * @tc.type: FUNC
473 */
474 HWTEST_F(CpuUsageTest, recordCpuUsageF4000_FP_PROCESS, TestSize.Level1)
475 {
476 std::string cmd = "hiperf record --app com.ohos.systemui -f 4000 -s fp -d 10";
477 std::thread perf(system, cmd.c_str());
478 perf.detach();
479 pid_t pid = GetPidByProcessName("hiperf");
480 uint64_t timeOut = 10000;
481 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
482 EXPECT_LE(cpuUsage, F4000_FP_CPU_LIMIT_PROCESS);
483 }
484
485 /**
486 * @tc.name: recordCpuUsageF8000_FP_PROCESS
487 * @tc.desc: test hiperf record one process cpu usage within required limit
488 * @tc.type: FUNC
489 */
490 HWTEST_F(CpuUsageTest, recordCpuUsageF8000_FP_PROCESS, TestSize.Level1)
491 {
492 std::string cmd = "hiperf record --app com.ohos.systemui -f 8000 -s fp -d 10";
493 std::thread perf(system, cmd.c_str());
494 perf.detach();
495 pid_t pid = GetPidByProcessName("hiperf");
496 uint64_t timeOut = 10000;
497 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
498 EXPECT_LE(cpuUsage, F8000_FP_CPU_LIMIT_PROCESS);
499 }
500
501 /**
502 * @tc.name: recordCpuUsageF100_DWARF_PROCESS
503 * @tc.desc: test hiperf record one process cpu usage within required limit
504 * @tc.type: FUNC
505 */
506 HWTEST_F(CpuUsageTest, recordCpuUsageF100_DWARF_PROCESS, TestSize.Level1)
507 {
508 std::string cmd = "hiperf record --app com.ohos.systemui -f 100 -s dwarf -d 10";
509 std::thread perf(system, cmd.c_str());
510 perf.detach();
511 pid_t pid = GetPidByProcessName("hiperf");
512 uint64_t timeOut = 10000;
513 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
514 EXPECT_LE(cpuUsage, F100_DWARF_CPU_LIMIT_PROCESS);
515 }
516
517 /**
518 * @tc.name: recordCpuUsageF500_DWARF_PROCESS
519 * @tc.desc: test hiperf record one process cpu usage within required limit
520 * @tc.type: FUNC
521 */
522 HWTEST_F(CpuUsageTest, recordCpuUsageF500_DWARF_PROCESS, TestSize.Level1)
523 {
524 std::string cmd = "hiperf record --app com.ohos.systemui -f 500 -s dwarf -d 10";
525 std::thread perf(system, cmd.c_str());
526 perf.detach();
527 pid_t pid = GetPidByProcessName("hiperf");
528 uint64_t timeOut = 10000;
529 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
530 EXPECT_LE(cpuUsage, F500_DWARF_CPU_LIMIT_PROCESS);
531 }
532
533 /**
534 * @tc.name: recordCpuUsageF1000_DWARF_PROCESS
535 * @tc.desc: test hiperf record one process cpu usage within required limit
536 * @tc.type: FUNC
537 */
538 HWTEST_F(CpuUsageTest, recordCpuUsageF1000_DWARF_PROCESS, TestSize.Level1)
539 {
540 std::string cmd = "hiperf record --app com.ohos.systemui -f 1000 -s dwarf -d 10";
541 std::thread perf(system, cmd.c_str());
542 perf.detach();
543 pid_t pid = GetPidByProcessName("hiperf");
544 uint64_t timeOut = 10000;
545 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
546 EXPECT_LE(cpuUsage, F1000_DWARF_CPU_LIMIT_PROCESS);
547 }
548
549 /**
550 * @tc.name: recordCpuUsageF2000_DWARF_PROCESS
551 * @tc.desc: test hiperf record one process cpu usage within required limit
552 * @tc.type: FUNC
553 */
554 HWTEST_F(CpuUsageTest, recordCpuUsageF2000_DWARF_PROCESS, TestSize.Level1)
555 {
556 std::string cmd = "hiperf record --app com.ohos.systemui -f 2000 -s dwarf -d 10";
557 std::thread perf(system, cmd.c_str());
558 perf.detach();
559 pid_t pid = GetPidByProcessName("hiperf");
560 uint64_t timeOut = 10000;
561 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
562 EXPECT_LE(cpuUsage, F2000_DWARF_CPU_LIMIT_PROCESS);
563 }
564
565 /**
566 * @tc.name: recordCpuUsageF4000_DWARF_PROCESS
567 * @tc.desc: test hiperf record one process cpu usage within required limit
568 * @tc.type: FUNC
569 */
570 HWTEST_F(CpuUsageTest, recordCpuUsageF4000_DWARF_PROCESS, TestSize.Level1)
571 {
572 std::string cmd = "hiperf record --app com.ohos.systemui -f 4000 -s dwarf -d 10";
573 std::thread perf(system, cmd.c_str());
574 perf.detach();
575 pid_t pid = GetPidByProcessName("hiperf");
576 uint64_t timeOut = 10000;
577 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
578 EXPECT_LE(cpuUsage, F4000_DWARF_CPU_LIMIT_PROCESS);
579 }
580
581 /**
582 * @tc.name: recordCpuUsageF8000_DWARF_PROCESS
583 * @tc.desc: test hiperf record one process cpu usage within required limit
584 * @tc.type: FUNC
585 */
586 HWTEST_F(CpuUsageTest, recordCpuUsageF8000_DWARF_PROCESS, TestSize.Level1)
587 {
588 std::string cmd = "hiperf record --app com.ohos.systemui -f 8000 -s dwarf -d 10";
589 std::thread perf(system, cmd.c_str());
590 perf.detach();
591 pid_t pid = GetPidByProcessName("hiperf");
592 uint64_t timeOut = 10000;
593 float cpuUsage = GetAverageCpuUsage(pid, timeOut);
594 EXPECT_LE(cpuUsage, F8000_DWARF_CPU_LIMIT_PROCESS);
595 }
596 } // namespace HiPerf
597 } // namespace Developtools
598 } // namespace OHOS
599