• 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 
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     TestCaseOption(opt);
222 }
223 
224 HWTEST_F(HiperfClientTest, SetFrequency, TestSize.Level1)
225 {
226     HiperfClient::RecordOption opt;
227     vector<pid_t> selectPids = {getpid()};
228     opt.SetSelectPids(selectPids);
229     opt.SetFrequency(500);
230 
231     TestCaseOption(opt);
232 }
233 
234 HWTEST_F(HiperfClientTest, SetPeriod, TestSize.Level1)
235 {
236     HiperfClient::RecordOption opt;
237     vector<pid_t> selectPids = {getpid()};
238     opt.SetSelectPids(selectPids);
239     opt.SetPeriod(3);
240 
241     TestCaseOption(opt);
242 }
243 
244 HWTEST_F(HiperfClientTest, SetSelectEvents, TestSize.Level1)
245 {
246     HiperfClient::RecordOption opt;
247     vector<pid_t> selectPids = {getpid()};
248     opt.SetSelectPids(selectPids);
249     vector<string> selectEvents = {"sw-cpu-clock:k"};
250     opt.SetSelectEvents(selectEvents);
251 
252     TestCaseOption(opt);
253 }
254 
255 HWTEST_F(HiperfClientTest, SetSelectGroups, TestSize.Level1)
256 {
257     HiperfClient::RecordOption opt;
258     vector<pid_t> selectPids = {getpid()};
259     opt.SetSelectPids(selectPids);
260     vector<string> selectEvents = {"hw-cpu-cycles:u"};
261     opt.SetSelectGroups(selectEvents);
262     TestCaseOption(opt);
263 }
264 
265 HWTEST_F(HiperfClientTest, SetNoInherit, TestSize.Level1)
266 {
267     HiperfClient::RecordOption opt;
268     vector<pid_t> selectPids = {getpid()};
269     opt.SetSelectPids(selectPids);
270     opt.SetNoInherit(true);
271 
272     TestCaseOption(opt);
273 }
274 
275 HWTEST_F(HiperfClientTest, SetSelectPids, TestSize.Level1)
276 {
277     HiperfClient::RecordOption opt;
278     vector<pid_t> selectPids = {getpid()};
279     opt.SetSelectPids(selectPids);
280 
281     TestCaseOption(opt);
282 }
283 
284 HWTEST_F(HiperfClientTest, SetSelectTids, TestSize.Level1)
285 {
286     HiperfClient::RecordOption opt;
287     vector<pid_t> selectTids = {gettid()};
288     opt.SetSelectTids(selectTids);
289 
290     TestCaseOption(opt);
291 }
292 
293 HWTEST_F(HiperfClientTest, SetExcludePerf, TestSize.Level1)
294 {
295     HiperfClient::RecordOption opt;
296     opt.SetTargetSystemWide(true);
297     opt.SetExcludePerf(true);
298 
299     TestCaseOption(opt);
300 }
301 
302 HWTEST_F(HiperfClientTest, SetCpuPercent, TestSize.Level1)
303 {
304     HiperfClient::RecordOption opt;
305     vector<pid_t> selectPids = {getpid()};
306     opt.SetSelectPids(selectPids);
307     opt.SetCpuPercent(50);
308 
309     TestCaseOption(opt);
310 }
311 
312 HWTEST_F(HiperfClientTest, SetOffCPU, TestSize.Level1)
313 {
314     HiperfClient::RecordOption opt;
315     vector<pid_t> selectPids = {getpid()};
316     opt.SetSelectPids(selectPids);
317     opt.SetOffCPU(true);
318 
319     TestCaseOption(opt);
320 }
321 
322 HWTEST_F(HiperfClientTest, SetCallStack, TestSize.Level1)
323 {
324     HiperfClient::RecordOption opt;
325     vector<pid_t> selectPids = {getpid()};
326     opt.SetSelectPids(selectPids);
327     opt.SetCallGraph("fp");
328 
329     TestCaseOption(opt);
330 }
331 
332 HWTEST_F(HiperfClientTest, SetDelayUnwind, TestSize.Level1)
333 {
334     HiperfClient::RecordOption opt;
335     vector<pid_t> selectPids = {getpid()};
336     opt.SetSelectPids(selectPids);
337     opt.SetDelayUnwind(true);
338 
339     TestCaseOption(opt);
340 }
341 
342 HWTEST_F(HiperfClientTest, SetDisableUnwind, TestSize.Level1)
343 {
344     HiperfClient::RecordOption opt;
345     vector<pid_t> selectPids = {getpid()};
346     opt.SetSelectPids(selectPids);
347     opt.SetDisableUnwind(true);
348 
349     TestCaseOption(opt);
350 }
351 
352 HWTEST_F(HiperfClientTest, SetDisableCallstackMerge, TestSize.Level1)
353 {
354     HiperfClient::RecordOption opt;
355     vector<pid_t> selectPids = {getpid()};
356     opt.SetSelectPids(selectPids);
357     opt.SetDisableCallstackMerge(true);
358 
359     TestCaseOption(opt);
360 }
361 
362 HWTEST_F(HiperfClientTest, SetOutputFilename, TestSize.Level1)
363 {
364     HiperfClient::RecordOption opt;
365     vector<pid_t> selectPids = {getpid()};
366     opt.SetSelectPids(selectPids);
367     opt.SetOutputFilename("perf.data.ut");
368 
369     TestCaseOption(opt);
370 }
371 
372 HWTEST_F(HiperfClientTest, SetSymbolDir, TestSize.Level1)
373 {
374     HiperfClient::RecordOption opt;
375     vector<pid_t> selectPids = {getpid()};
376     opt.SetSelectPids(selectPids);
377     opt.SetSymbolDir("/data/local/tmp/");
378 
379     TestCaseOption(opt);
380 }
381 
382 HWTEST_F(HiperfClientTest, SetDataLimit, TestSize.Level1)
383 {
384     HiperfClient::RecordOption opt;
385     vector<pid_t> selectPids = {getpid()};
386     opt.SetSelectPids(selectPids);
387     opt.SetDataLimit("100M");
388 
389     TestCaseOption(opt);
390 }
391 
392 HWTEST_F(HiperfClientTest, SetAppPackage, TestSize.Level1)
393 {
394     HiperfClient::RecordOption opt;
395     opt.SetAppPackage("com.ohos.launcher");
396 
397     TestCaseOption(opt);
398 }
399 
400 HWTEST_F(HiperfClientTest, SetClockId, TestSize.Level1)
401 {
402     HiperfClient::RecordOption opt;
403     vector<pid_t> selectPids = {getpid()};
404     opt.SetSelectPids(selectPids);
405     opt.SetClockId("monotonic");
406 
407     TestCaseOption(opt);
408 }
409 
410 HWTEST_F(HiperfClientTest, SetMmapPages, TestSize.Level1)
411 {
412     HiperfClient::RecordOption opt;
413     vector<pid_t> selectPids = {getpid()};
414     opt.SetSelectPids(selectPids);
415     opt.SetMmapPages(64);
416 
417     TestCaseOption(opt);
418 }
419 
420 HWTEST_F(HiperfClientTest, SetVecBranchSampleTypes, TestSize.Level1)
421 {
422     StdoutRecord stdoutRecord;
423     stdoutRecord.Start();
424 
425     HiperfClient::RecordOption opt;
426     vector<pid_t> selectPids = {getpid()};
427     opt.SetSelectPids(selectPids);
428     std::vector<std::string> vecBranchSampleTypes = {"any", "any_call", "any_ret", "ind_call", "u", "k"};
429     opt.SetVecBranchSampleTypes(vecBranchSampleTypes);
430     HiperfClient::Client myHiperf;
431     myHiperf.SetDebugMode();
432 
433     ASSERT_TRUE(myHiperf.IsReady());
434 #ifdef is_ohos
435     ASSERT_EQ(myHiperf.Start(opt), false);
436 #else
437     ASSERT_TRUE(myHiperf.Start(opt));
438     ASSERT_TRUE(myHiperf.Pause());
439     this_thread::sleep_for(1s);
440 
441     ASSERT_TRUE(myHiperf.Resume());
442     this_thread::sleep_for(1s);
443 
444     ASSERT_TRUE(myHiperf.Stop());
445 #endif
446     stdoutRecord.Stop();
447 }
448 } // namespace HiPerf
449 } // namespace Developtools
450 } // namespace OHOS
451