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