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 "utilities.h"
24
25 using namespace testing::ext;
26 using namespace std;
27 using namespace OHOS::HiviewDFX;
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 this_thread::sleep_for(1s);
70
71 ASSERT_TRUE(myHiperf.Resume());
72 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 this_thread::sleep_for(1s);
91
92 ASSERT_TRUE(myHiperf.Resume());
93 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 this_thread::sleep_for(1s);
111
112 ASSERT_TRUE(myHiperf.Resume());
113 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 this_thread::sleep_for(1s);
132
133 ASSERT_TRUE(myHiperf.Resume());
134 this_thread::sleep_for(1s);
135
136 ASSERT_TRUE(myHiperf.Stop());
137
138 stdoutRecord.Stop();
139 }
140
141 HWTEST_F(HiperfClientTest, GetCommandPath, TestSize.Level1)
142 {
143 StdoutRecord stdoutRecord;
144 stdoutRecord.Start();
145
146 HiperfClient::Client myHiperf("/data/local/tmp/");
147 ASSERT_EQ(myHiperf.GetCommandPath().empty(), false);
148
149 stdoutRecord.Stop();
150 }
151
TestCaseOption(const HiperfClient::RecordOption & opt)152 void HiperfClientTest::TestCaseOption(const HiperfClient::RecordOption &opt)
153 {
154 StdoutRecord stdoutRecord;
155 stdoutRecord.Start();
156 HiperfClient::Client myHiperf;
157 myHiperf.SetDebugMode();
158
159 ASSERT_TRUE(myHiperf.IsReady());
160 ASSERT_TRUE(myHiperf.Start(opt));
161
162 bool retPause = true;
163 bool retResume = true;
164 bool retStop = true;
165 if (!myHiperf.Pause()) {
166 retPause = false;
167 }
168 this_thread::sleep_for(1s);
169
170 if (!myHiperf.Resume()) {
171 retResume = false;
172 }
173 this_thread::sleep_for(1s);
174
175 if (!myHiperf.Stop()) {
176 retStop = false;
177 }
178
179 ASSERT_TRUE(retPause);
180 ASSERT_TRUE(retResume);
181 ASSERT_TRUE(retStop);
182
183 stdoutRecord.Stop();
184 }
185
186 HWTEST_F(HiperfClientTest, SetTargetSystemWide, TestSize.Level1)
187 {
188 HiperfClient::RecordOption opt;
189 opt.SetTargetSystemWide(true);
190
191 TestCaseOption(opt);
192 }
193
194 HWTEST_F(HiperfClientTest, SetCompressData, TestSize.Level1)
195 {
196 HiperfClient::RecordOption opt;
197 vector<pid_t> selectPids = {getpid()};
198 opt.SetSelectPids(selectPids);
199 opt.SetCompressData(true);
200 TestCaseOption(opt);
201 }
202
203 HWTEST_F(HiperfClientTest, SetSelectCpus, TestSize.Level1)
204 {
205 HiperfClient::RecordOption opt;
206 vector<pid_t> selectPids = {getpid()};
207 opt.SetSelectPids(selectPids);
208 vector<int> cpus = {0, 1};
209 opt.SetSelectCpus(cpus);
210
211 TestCaseOption(opt);
212 }
213
214 HWTEST_F(HiperfClientTest, SetTimeStopSec, TestSize.Level1)
215 {
216 HiperfClient::RecordOption opt;
217 vector<pid_t> selectPids = {getpid()};
218 opt.SetSelectPids(selectPids);
219 opt.SetTimeStopSec(40);
220
221 HiperfClient::Client myHiperf;
222 ASSERT_TRUE(myHiperf.IsReady());
223 ASSERT_TRUE(myHiperf.Start(opt));
224 }
225
226 HWTEST_F(HiperfClientTest, SetFrequency, TestSize.Level1)
227 {
228 HiperfClient::RecordOption opt;
229 vector<pid_t> selectPids = {getpid()};
230 opt.SetSelectPids(selectPids);
231 opt.SetFrequency(500);
232
233 TestCaseOption(opt);
234 }
235
236 HWTEST_F(HiperfClientTest, SetPeriod, TestSize.Level1)
237 {
238 HiperfClient::RecordOption opt;
239 vector<pid_t> selectPids = {getpid()};
240 opt.SetSelectPids(selectPids);
241 opt.SetPeriod(3);
242
243 TestCaseOption(opt);
244 }
245
246 HWTEST_F(HiperfClientTest, SetSelectEvents, TestSize.Level1)
247 {
248 HiperfClient::RecordOption opt;
249 vector<pid_t> selectPids = {getpid()};
250 opt.SetSelectPids(selectPids);
251 vector<string> selectEvents = {"sw-cpu-clock:k"};
252 opt.SetSelectEvents(selectEvents);
253
254 TestCaseOption(opt);
255 }
256
257 HWTEST_F(HiperfClientTest, SetSelectGroups, TestSize.Level1)
258 {
259 HiperfClient::RecordOption opt;
260 vector<pid_t> selectPids = {getpid()};
261 opt.SetSelectPids(selectPids);
262 vector<string> selectEvents = {"hw-cpu-cycles:u"};
263 opt.SetSelectGroups(selectEvents);
264 TestCaseOption(opt);
265 }
266
267 HWTEST_F(HiperfClientTest, SetNoInherit, TestSize.Level1)
268 {
269 HiperfClient::RecordOption opt;
270 vector<pid_t> selectPids = {getpid()};
271 opt.SetSelectPids(selectPids);
272 opt.SetNoInherit(true);
273
274 TestCaseOption(opt);
275 }
276
277 HWTEST_F(HiperfClientTest, SetSelectPids, TestSize.Level1)
278 {
279 HiperfClient::RecordOption opt;
280 vector<pid_t> selectPids = {getpid()};
281 opt.SetSelectPids(selectPids);
282
283 TestCaseOption(opt);
284 }
285
286 HWTEST_F(HiperfClientTest, SetCallStackSamplingConfigs, TestSize.Level1)
287 {
288 HiperfClient::RecordOption opt;
289 vector<pid_t> selectPids = {getpid()};
290 opt.SetSelectPids(selectPids);
291 opt.SetCallStackSamplingConfigs(1);
292
293 HiperfClient::Client myHiperf;
294 ASSERT_TRUE(myHiperf.IsReady());
295 ASSERT_TRUE(myHiperf.Start(opt));
296 }
297
298 HWTEST_F(HiperfClientTest, SetSelectTids, TestSize.Level1)
299 {
300 HiperfClient::RecordOption opt;
301 vector<pid_t> selectTids = {gettid()};
302 opt.SetSelectTids(selectTids);
303
304 TestCaseOption(opt);
305 }
306
307 HWTEST_F(HiperfClientTest, SetExcludePerf, TestSize.Level1)
308 {
309 HiperfClient::RecordOption opt;
310 opt.SetTargetSystemWide(true);
311 opt.SetExcludePerf(true);
312
313 TestCaseOption(opt);
314 }
315
316 HWTEST_F(HiperfClientTest, SetCpuPercent, TestSize.Level1)
317 {
318 HiperfClient::RecordOption opt;
319 vector<pid_t> selectPids = {getpid()};
320 opt.SetSelectPids(selectPids);
321 opt.SetCpuPercent(50);
322
323 TestCaseOption(opt);
324 }
325
326 HWTEST_F(HiperfClientTest, SetOffCPU, TestSize.Level1)
327 {
328 HiperfClient::RecordOption opt;
329 vector<pid_t> selectPids = {getpid()};
330 opt.SetSelectPids(selectPids);
331 opt.SetOffCPU(true);
332
333 TestCaseOption(opt);
334 }
335
336 HWTEST_F(HiperfClientTest, SetCallStack, TestSize.Level1)
337 {
338 HiperfClient::RecordOption opt;
339 vector<pid_t> selectPids = {getpid()};
340 opt.SetSelectPids(selectPids);
341 opt.SetCallGraph("fp");
342
343 TestCaseOption(opt);
344 }
345
346 HWTEST_F(HiperfClientTest, SetDelayUnwind, TestSize.Level1)
347 {
348 HiperfClient::RecordOption opt;
349 vector<pid_t> selectPids = {getpid()};
350 opt.SetSelectPids(selectPids);
351 opt.SetDelayUnwind(true);
352
353 TestCaseOption(opt);
354 }
355
356 HWTEST_F(HiperfClientTest, SetDisableUnwind, TestSize.Level1)
357 {
358 HiperfClient::RecordOption opt;
359 vector<pid_t> selectPids = {getpid()};
360 opt.SetSelectPids(selectPids);
361 opt.SetDisableUnwind(true);
362
363 TestCaseOption(opt);
364 }
365
366 HWTEST_F(HiperfClientTest, SetDisableCallstackMerge, TestSize.Level1)
367 {
368 HiperfClient::RecordOption opt;
369 vector<pid_t> selectPids = {getpid()};
370 opt.SetSelectPids(selectPids);
371 opt.SetDisableCallstackMerge(true);
372
373 TestCaseOption(opt);
374 }
375
376 HWTEST_F(HiperfClientTest, SetOutputFilename, TestSize.Level1)
377 {
378 HiperfClient::RecordOption opt;
379 vector<pid_t> selectPids = {getpid()};
380 opt.SetSelectPids(selectPids);
381 opt.SetOutputFilename("perf.data.ut");
382
383 TestCaseOption(opt);
384 }
385
386 HWTEST_F(HiperfClientTest, SetSymbolDir, TestSize.Level1)
387 {
388 HiperfClient::RecordOption opt;
389 vector<pid_t> selectPids = {getpid()};
390 opt.SetSelectPids(selectPids);
391 opt.SetSymbolDir("/data/local/tmp/");
392
393 TestCaseOption(opt);
394 }
395
396 HWTEST_F(HiperfClientTest, SetDataLimit, TestSize.Level1)
397 {
398 HiperfClient::RecordOption opt;
399 vector<pid_t> selectPids = {getpid()};
400 opt.SetSelectPids(selectPids);
401 opt.SetDataLimit("100M");
402
403 TestCaseOption(opt);
404 }
405
406 HWTEST_F(HiperfClientTest, SetAppPackage, TestSize.Level1)
407 {
408 HiperfClient::RecordOption opt;
409 opt.SetAppPackage("com.ohos.launcher");
410
411 TestCaseOption(opt);
412 }
413
414 HWTEST_F(HiperfClientTest, SetClockId, TestSize.Level1)
415 {
416 HiperfClient::RecordOption opt;
417 vector<pid_t> selectPids = {getpid()};
418 opt.SetSelectPids(selectPids);
419 opt.SetClockId("monotonic");
420
421 TestCaseOption(opt);
422 }
423
424 HWTEST_F(HiperfClientTest, SetMmapPages, TestSize.Level1)
425 {
426 HiperfClient::RecordOption opt;
427 vector<pid_t> selectPids = {getpid()};
428 opt.SetSelectPids(selectPids);
429 opt.SetMmapPages(64);
430
431 TestCaseOption(opt);
432 }
433
434 HWTEST_F(HiperfClientTest, SetVecBranchSampleTypes, TestSize.Level1)
435 {
436 StdoutRecord stdoutRecord;
437 stdoutRecord.Start();
438
439 HiperfClient::RecordOption opt;
440 vector<pid_t> selectPids = {getpid()};
441 opt.SetSelectPids(selectPids);
442 std::vector<std::string> vecBranchSampleTypes = {"any", "any_call", "any_ret", "ind_call", "u", "k"};
443 opt.SetVecBranchSampleTypes(vecBranchSampleTypes);
444 HiperfClient::Client myHiperf;
445 myHiperf.SetDebugMode();
446
447 ASSERT_TRUE(myHiperf.IsReady());
448 #ifdef is_ohos
449 ASSERT_EQ(myHiperf.Start(opt), false);
450 #else
451 ASSERT_TRUE(myHiperf.Start(opt));
452 ASSERT_TRUE(myHiperf.Pause());
453 this_thread::sleep_for(1s);
454
455 ASSERT_TRUE(myHiperf.Resume());
456 this_thread::sleep_for(1s);
457
458 ASSERT_TRUE(myHiperf.Stop());
459 #endif
460 stdoutRecord.Stop();
461 }
462 } // namespace HiPerf
463 } // namespace Developtools
464 } // namespace OHOS
465