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 "hiperf_client_test.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <cinttypes>
21 #include <thread>
22
23 #include "test_utilities.h"
24 #include "utilities.h"
25
26 using namespace testing::ext;
27 using namespace std;
28 namespace OHOS {
29 namespace Developtools {
30 namespace HiPerf {
31 class HiperfClientTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37
38 static void TestCaseOption(const HiperfClient::RecordOption &opt);
39 };
40
SetUpTestCase()41 void HiperfClientTest::SetUpTestCase() {}
42
TearDownTestCase()43 void HiperfClientTest::TearDownTestCase()
44 {
45 DebugLogger::GetInstance()->Reset();
46 }
47
SetUp()48 void HiperfClientTest::SetUp() {}
49
TearDown()50 void HiperfClientTest::TearDown()
51 {
52 }
53
54 /**
55 * @tc.name:
56 * @tc.desc: record
57 * @tc.type: FUNC
58 */
59 HWTEST_F(HiperfClientTest, NoPara, TestSize.Level1)
60 {
61 StdoutRecord stdoutRecord;
62 stdoutRecord.Start();
63
64 HiperfClient::Client myHiperf;
65 myHiperf.SetDebugMode();
66 ASSERT_TRUE(myHiperf.Start());
67
68 ASSERT_TRUE(myHiperf.Pause());
69 std::this_thread::sleep_for(1s);
70
71 ASSERT_TRUE(myHiperf.Resume());
72 std::this_thread::sleep_for(1s);
73
74 ASSERT_TRUE(myHiperf.Stop());
75
76 stdoutRecord.Stop();
77 }
78
79 HWTEST_F(HiperfClientTest, OutDir, TestSize.Level1)
80 {
81 StdoutRecord stdoutRecord;
82 stdoutRecord.Start();
83
84 HiperfClient::Client myHiperf("/data/local/tmp/");
85 ASSERT_EQ(myHiperf.GetOutputDir(), "/data/local/tmp/");
86 myHiperf.SetDebugMode();
87 ASSERT_TRUE(myHiperf.Start());
88
89 ASSERT_TRUE(myHiperf.Pause());
90 std::this_thread::sleep_for(1s);
91
92 ASSERT_TRUE(myHiperf.Resume());
93 std::this_thread::sleep_for(1s);
94
95 ASSERT_TRUE(myHiperf.Stop());
96
97 stdoutRecord.Stop();
98 }
99
100 HWTEST_F(HiperfClientTest, DebugMuchMode, TestSize.Level1)
101 {
102 StdoutRecord stdoutRecord;
103 stdoutRecord.Start();
104
105 HiperfClient::Client myHiperf;
106 myHiperf.SetDebugMuchMode();
107 ASSERT_TRUE(myHiperf.Start());
108
109 ASSERT_TRUE(myHiperf.Pause());
110 std::this_thread::sleep_for(1s);
111
112 ASSERT_TRUE(myHiperf.Resume());
113 std::this_thread::sleep_for(1s);
114
115 ASSERT_TRUE(myHiperf.Stop());
116
117 stdoutRecord.Stop();
118 }
119
120 HWTEST_F(HiperfClientTest, EnableHilog, TestSize.Level1)
121 {
122 StdoutRecord stdoutRecord;
123 stdoutRecord.Start();
124
125 HiperfClient::Client myHiperf;
126 myHiperf.SetDebugMode();
127 myHiperf.EnableHilog();
128 ASSERT_TRUE(myHiperf.Start());
129
130 ASSERT_TRUE(myHiperf.Pause());
131 std::this_thread::sleep_for(1s);
132
133 ASSERT_TRUE(myHiperf.Resume());
134 std::this_thread::sleep_for(1s);
135
136 ASSERT_TRUE(myHiperf.Stop());
137
138 stdoutRecord.Stop();
139 }
140
141 HWTEST_F(HiperfClientTest, Prepare, TestSize.Level1)
142 {
143 StdoutRecord stdoutRecord;
144 stdoutRecord.Start();
145 HiperfClient::RecordOption opt;
146 opt.SetTargetSystemWide(true);
147
148 HiperfClient::Client myHiperf("/data/local/tmp/");
149 ASSERT_TRUE(myHiperf.PrePare(opt));
150 std::this_thread::sleep_for(1s);
151
152 ASSERT_TRUE(myHiperf.StartRun());
153 std::this_thread::sleep_for(1s);
154
155 ASSERT_TRUE(myHiperf.Stop());
156
157 stdoutRecord.Stop();
158 }
159
160 HWTEST_F(HiperfClientTest, GetCommandPath, TestSize.Level1)
161 {
162 StdoutRecord stdoutRecord;
163 stdoutRecord.Start();
164
165 HiperfClient::Client myHiperf("/data/local/tmp/");
166 ASSERT_EQ(myHiperf.GetCommandPath().empty(), false);
167
168 stdoutRecord.Stop();
169 }
170
TestCaseOption(const HiperfClient::RecordOption & opt)171 void HiperfClientTest::TestCaseOption(const HiperfClient::RecordOption &opt)
172 {
173 StdoutRecord stdoutRecord;
174 stdoutRecord.Start();
175 HiperfClient::Client myHiperf;
176 myHiperf.SetDebugMode();
177
178 ASSERT_TRUE(myHiperf.IsReady());
179 ASSERT_TRUE(myHiperf.Start(opt));
180
181 bool retPause = true;
182 bool retResume = true;
183 bool retStop = true;
184 if (!myHiperf.Pause()) {
185 retPause = false;
186 }
187 std::this_thread::sleep_for(1s);
188
189 if (!myHiperf.Resume()) {
190 retResume = false;
191 }
192 std::this_thread::sleep_for(1s);
193
194 if (!myHiperf.Stop()) {
195 retStop = false;
196 }
197
198 ASSERT_TRUE(retPause);
199 ASSERT_TRUE(retResume);
200 ASSERT_TRUE(retStop);
201
202 stdoutRecord.Stop();
203 }
204
205 HWTEST_F(HiperfClientTest, SetTargetSystemWide, TestSize.Level1)
206 {
207 HiperfClient::RecordOption opt;
208 opt.SetTargetSystemWide(true);
209
210 TestCaseOption(opt);
211 }
212
213 HWTEST_F(HiperfClientTest, SetCompressData, TestSize.Level1)
214 {
215 HiperfClient::RecordOption opt;
216 std::vector<pid_t> selectPids = {getpid()};
217 opt.SetSelectPids(selectPids);
218 opt.SetCompressData(true);
219 TestCaseOption(opt);
220 }
221
222 HWTEST_F(HiperfClientTest, SetSelectCpus, TestSize.Level1)
223 {
224 HiperfClient::RecordOption opt;
225 std::vector<pid_t> selectPids = {getpid()};
226 opt.SetSelectPids(selectPids);
227 std::vector<int> cpus = {0, 1};
228 opt.SetSelectCpus(cpus);
229
230 TestCaseOption(opt);
231 }
232
233 HWTEST_F(HiperfClientTest, SetTimeStopSec, TestSize.Level1)
234 {
235 HiperfClient::RecordOption opt;
236 std::vector<pid_t> selectPids = {getpid()};
237 opt.SetSelectPids(selectPids);
238 opt.SetTimeStopSec(40);
239
240 HiperfClient::Client myHiperf;
241 ASSERT_TRUE(myHiperf.IsReady());
242 ASSERT_TRUE(myHiperf.Start(opt));
243 }
244
245 HWTEST_F(HiperfClientTest, SetFrequency, TestSize.Level1)
246 {
247 HiperfClient::RecordOption opt;
248 std::vector<pid_t> selectPids = {getpid()};
249 opt.SetSelectPids(selectPids);
250 opt.SetFrequency(500);
251
252 TestCaseOption(opt);
253 }
254
255 HWTEST_F(HiperfClientTest, SetPeriod, TestSize.Level1)
256 {
257 HiperfClient::RecordOption opt;
258 std::vector<pid_t> selectPids = {getpid()};
259 opt.SetSelectPids(selectPids);
260 opt.SetPeriod(3);
261
262 TestCaseOption(opt);
263 }
264
265 HWTEST_F(HiperfClientTest, SetSelectEvents, TestSize.Level1)
266 {
267 HiperfClient::RecordOption opt;
268 std::vector<pid_t> selectPids = {getpid()};
269 opt.SetSelectPids(selectPids);
270 std::vector<std::string> selectEvents = {"hw-cpu-cycles:k"};
271 opt.SetSelectEvents(selectEvents);
272
273 TestCaseOption(opt);
274 }
275
276 HWTEST_F(HiperfClientTest, SetSelectGroups, TestSize.Level1)
277 {
278 HiperfClient::RecordOption opt;
279 std::vector<pid_t> selectPids = {getpid()};
280 opt.SetSelectPids(selectPids);
281 std::vector<std::string> selectEvents = {"hw-cpu-cycles:u"};
282 opt.SetSelectGroups(selectEvents);
283 TestCaseOption(opt);
284 }
285
286 HWTEST_F(HiperfClientTest, SetNoInherit, TestSize.Level1)
287 {
288 HiperfClient::RecordOption opt;
289 std::vector<pid_t> selectPids = {getpid()};
290 opt.SetSelectPids(selectPids);
291 opt.SetNoInherit(true);
292
293 TestCaseOption(opt);
294 }
295
296 HWTEST_F(HiperfClientTest, SetSelectPids, TestSize.Level1)
297 {
298 HiperfClient::RecordOption opt;
299 std::vector<pid_t> selectPids = {getpid()};
300 opt.SetSelectPids(selectPids);
301
302 TestCaseOption(opt);
303 }
304
305 HWTEST_F(HiperfClientTest, SetCallStackSamplingConfigs, TestSize.Level1)
306 {
307 HiperfClient::RecordOption opt;
308 std::vector<pid_t> selectPids = {getpid()};
309 opt.SetSelectPids(selectPids);
310 opt.SetCallStackSamplingConfigs(1);
311
312 HiperfClient::Client myHiperf;
313 ASSERT_TRUE(myHiperf.IsReady());
314 ASSERT_TRUE(myHiperf.Start(opt));
315 }
316
317 HWTEST_F(HiperfClientTest, SetSelectTids, TestSize.Level1)
318 {
319 HiperfClient::RecordOption opt;
320 std::vector<pid_t> selectTids = {gettid()};
321 opt.SetSelectTids(selectTids);
322
323 TestCaseOption(opt);
324 }
325
326 HWTEST_F(HiperfClientTest, SetExcludePerf, TestSize.Level1)
327 {
328 HiperfClient::RecordOption opt;
329 opt.SetTargetSystemWide(true);
330 opt.SetExcludePerf(true);
331
332 TestCaseOption(opt);
333 }
334
335 HWTEST_F(HiperfClientTest, SetCpuPercent, TestSize.Level1)
336 {
337 HiperfClient::RecordOption opt;
338 std::vector<pid_t> selectPids = {getpid()};
339 opt.SetSelectPids(selectPids);
340 opt.SetCpuPercent(50);
341
342 TestCaseOption(opt);
343 }
344
345 HWTEST_F(HiperfClientTest, SetOffCPU, TestSize.Level1)
346 {
347 HiperfClient::RecordOption opt;
348 std::vector<pid_t> selectPids = {getpid()};
349 opt.SetSelectPids(selectPids);
350 opt.SetOffCPU(true);
351
352 TestCaseOption(opt);
353 }
354
355 HWTEST_F(HiperfClientTest, SetCallStack, TestSize.Level1)
356 {
357 HiperfClient::RecordOption opt;
358 std::vector<pid_t> selectPids = {getpid()};
359 opt.SetSelectPids(selectPids);
360 opt.SetCallGraph("fp");
361
362 TestCaseOption(opt);
363 }
364
365 HWTEST_F(HiperfClientTest, SetDelayUnwind, TestSize.Level1)
366 {
367 HiperfClient::RecordOption opt;
368 std::vector<pid_t> selectPids = {getpid()};
369 opt.SetSelectPids(selectPids);
370 opt.SetDelayUnwind(true);
371
372 TestCaseOption(opt);
373 }
374
375 HWTEST_F(HiperfClientTest, SetDisableUnwind, TestSize.Level1)
376 {
377 HiperfClient::RecordOption opt;
378 std::vector<pid_t> selectPids = {getpid()};
379 opt.SetSelectPids(selectPids);
380 opt.SetDisableUnwind(true);
381
382 TestCaseOption(opt);
383 }
384
385 HWTEST_F(HiperfClientTest, SetDisableCallstackMerge, TestSize.Level1)
386 {
387 HiperfClient::RecordOption opt;
388 std::vector<pid_t> selectPids = {getpid()};
389 opt.SetSelectPids(selectPids);
390 opt.SetDisableCallstackMerge(true);
391
392 TestCaseOption(opt);
393 }
394
395 HWTEST_F(HiperfClientTest, SetOutputFilename, TestSize.Level1)
396 {
397 HiperfClient::RecordOption opt;
398 std::vector<pid_t> selectPids = {getpid()};
399 opt.SetSelectPids(selectPids);
400 opt.SetOutputFilename("perf.data.ut");
401
402 TestCaseOption(opt);
403 }
404
405 HWTEST_F(HiperfClientTest, SetSymbolDir, TestSize.Level1)
406 {
407 HiperfClient::RecordOption opt;
408 std::vector<pid_t> selectPids = {getpid()};
409 opt.SetSelectPids(selectPids);
410 opt.SetSymbolDir("/data/local/tmp/");
411
412 TestCaseOption(opt);
413 }
414
415 HWTEST_F(HiperfClientTest, SetDataLimit, TestSize.Level1)
416 {
417 HiperfClient::RecordOption opt;
418 std::vector<pid_t> selectPids = {getpid()};
419 opt.SetSelectPids(selectPids);
420 opt.SetDataLimit("100M");
421
422 TestCaseOption(opt);
423 }
424
425 HWTEST_F(HiperfClientTest, SetAppPackage, TestSize.Level1)
426 {
427 HiperfClient::RecordOption opt;
428 std::string testProcesses = "com.ohos.sceneboard";
429 if (!CheckTestApp(testProcesses)) {
430 testProcesses = "com.ohos.launcher";
431 }
432 opt.SetAppPackage(testProcesses);
433
434 TestCaseOption(opt);
435 }
436
437 HWTEST_F(HiperfClientTest, SetClockId, TestSize.Level1)
438 {
439 HiperfClient::RecordOption opt;
440 std::vector<pid_t> selectPids = {getpid()};
441 opt.SetSelectPids(selectPids);
442 opt.SetClockId("monotonic");
443
444 TestCaseOption(opt);
445 }
446
447 HWTEST_F(HiperfClientTest, SetMmapPages, TestSize.Level1)
448 {
449 HiperfClient::RecordOption opt;
450 std::vector<pid_t> selectPids = {getpid()};
451 opt.SetSelectPids(selectPids);
452 opt.SetMmapPages(64);
453
454 TestCaseOption(opt);
455 }
456
457 HWTEST_F(HiperfClientTest, SetReport, TestSize.Level1)
458 {
459 HiperfClient::RecordOption opt;
460 std::vector<pid_t> selectPids = {getpid()};
461 opt.SetSelectPids(selectPids);
462 opt.SetReport(true);
463
464 TestCaseOption(opt);
465 }
466
467 HWTEST_F(HiperfClientTest, SetVecBranchSampleTypes, TestSize.Level1)
468 {
469 HiperfClient::RecordOption opt;
470 std::vector<pid_t> selectPids = {getpid()};
471 opt.SetSelectPids(selectPids);
472 opt.SetCallStackSamplingConfigs(1);
473 std::vector<std::string> branchSampleTypes = {"any"};
474 opt.SetVecBranchSampleTypes(branchSampleTypes);
475 HiperfClient::Client myHiperf;
476 ASSERT_TRUE(myHiperf.IsReady());
477 }
478
479 HWTEST_F(HiperfClientTest, Output, TestSize.Level1)
480 {
481 HiperfClient::RecordOption opt;
482 std::vector<std::string> process = {"hilogd"};
483 opt.SetTargetSystemWide(true);
484 opt.SetBackTrack(true);
485 opt.SetBackTrackSec(10); // 10 : 10s
486 opt.SetExcludeProcess(process);
487
488 HiperfClient::Client myHiperf("/data/local/tmp/");
489 EXPECT_TRUE(myHiperf.PrePare(opt));
490 std::this_thread::sleep_for(1s);
491 EXPECT_FALSE(myHiperf.Output());
492 std::this_thread::sleep_for(1s);
493 EXPECT_TRUE(myHiperf.Stop());
494 }
495 } // namespace HiPerf
496 } // namespace Developtools
497 } // namespace OHOS
498