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