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 myHiperf.SetDebugMode();
86 ASSERT_TRUE(myHiperf.Start());
87
88 ASSERT_TRUE(myHiperf.Pause());
89 this_thread::sleep_for(1s);
90
91 ASSERT_TRUE(myHiperf.Resume());
92 this_thread::sleep_for(1s);
93
94 ASSERT_TRUE(myHiperf.Stop());
95
96 stdoutRecord.Stop();
97 }
98
TestCaseOption(const HiperfClient::RecordOption & opt)99 void HiperfClientTest::TestCaseOption(const HiperfClient::RecordOption &opt)
100 {
101 StdoutRecord stdoutRecord;
102 stdoutRecord.Start();
103 HiperfClient::Client myHiperf;
104 myHiperf.SetDebugMode();
105
106 ASSERT_TRUE(myHiperf.IsReady());
107 ASSERT_TRUE(myHiperf.Start(opt));
108
109 bool retPause = true;
110 bool retResume = true;
111 bool retStop = true;
112 if (!myHiperf.Pause()) {
113 retPause = false;
114 }
115 this_thread::sleep_for(1s);
116
117 if (!myHiperf.Resume()) {
118 retResume = false;
119 }
120 this_thread::sleep_for(1s);
121
122 if (!myHiperf.Stop()) {
123 retStop = false;
124 }
125
126 ASSERT_TRUE(retPause);
127 ASSERT_TRUE(retResume);
128 ASSERT_TRUE(retStop);
129
130 stdoutRecord.Stop();
131 }
132
133 HWTEST_F(HiperfClientTest, SetTargetSystemWide, TestSize.Level1)
134 {
135 HiperfClient::RecordOption opt;
136 opt.SetTargetSystemWide(true);
137
138 TestCaseOption(opt);
139 }
140
141 HWTEST_F(HiperfClientTest, SetCompressData, TestSize.Level1)
142 {
143 HiperfClient::RecordOption opt;
144 vector<pid_t> selectPids = {getpid()};
145 opt.SetSelectPids(selectPids);
146 opt.SetCompressData(true);
147 TestCaseOption(opt);
148 }
149
150 HWTEST_F(HiperfClientTest, SetSelectCpus, TestSize.Level1)
151 {
152 HiperfClient::RecordOption opt;
153 vector<pid_t> selectPids = {getpid()};
154 opt.SetSelectPids(selectPids);
155 vector<int> cpus = {0, 1};
156 opt.SetSelectCpus(cpus);
157
158 TestCaseOption(opt);
159 }
160
161 HWTEST_F(HiperfClientTest, SetTimeStopSec, TestSize.Level1)
162 {
163 HiperfClient::RecordOption opt;
164 vector<pid_t> selectPids = {getpid()};
165 opt.SetSelectPids(selectPids);
166 opt.SetTimeStopSec(40);
167
168 TestCaseOption(opt);
169 }
170
171 HWTEST_F(HiperfClientTest, SetFrequency, TestSize.Level1)
172 {
173 HiperfClient::RecordOption opt;
174 vector<pid_t> selectPids = {getpid()};
175 opt.SetSelectPids(selectPids);
176 opt.SetFrequency(500);
177
178 TestCaseOption(opt);
179 }
180
181 HWTEST_F(HiperfClientTest, SetPeriod, TestSize.Level1)
182 {
183 HiperfClient::RecordOption opt;
184 vector<pid_t> selectPids = {getpid()};
185 opt.SetSelectPids(selectPids);
186 opt.SetPeriod(3);
187
188 TestCaseOption(opt);
189 }
190
191 HWTEST_F(HiperfClientTest, SetSelectEvents, TestSize.Level1)
192 {
193 HiperfClient::RecordOption opt;
194 vector<pid_t> selectPids = {getpid()};
195 opt.SetSelectPids(selectPids);
196 vector<string> selectEvents = {"sw-cpu-clock:k"};
197 opt.SetSelectEvents(selectEvents);
198
199 TestCaseOption(opt);
200 }
201
202 HWTEST_F(HiperfClientTest, SetSelectGroups, TestSize.Level1)
203 {
204 HiperfClient::RecordOption opt;
205 vector<pid_t> selectPids = {getpid()};
206 opt.SetSelectPids(selectPids);
207 vector<string> selectEvents = {"hw-cpu-cycles:u"};
208 opt.SetSelectGroups(selectEvents);
209 TestCaseOption(opt);
210 }
211
212 HWTEST_F(HiperfClientTest, SetNoInherit, TestSize.Level1)
213 {
214 HiperfClient::RecordOption opt;
215 vector<pid_t> selectPids = {getpid()};
216 opt.SetSelectPids(selectPids);
217 opt.SetNoInherit(true);
218
219 TestCaseOption(opt);
220 }
221
222 HWTEST_F(HiperfClientTest, SetSelectPids, TestSize.Level1)
223 {
224 HiperfClient::RecordOption opt;
225 vector<pid_t> selectPids = {getpid()};
226 opt.SetSelectPids(selectPids);
227
228 TestCaseOption(opt);
229 }
230
231 HWTEST_F(HiperfClientTest, SetSelectTids, TestSize.Level1)
232 {
233 HiperfClient::RecordOption opt;
234 vector<pid_t> selectTids = {gettid()};
235 opt.SetSelectTids(selectTids);
236
237 TestCaseOption(opt);
238 }
239
240 HWTEST_F(HiperfClientTest, SetExcludePerf, TestSize.Level1)
241 {
242 HiperfClient::RecordOption opt;
243 opt.SetTargetSystemWide(true);
244 opt.SetExcludePerf(true);
245
246 TestCaseOption(opt);
247 }
248
249 HWTEST_F(HiperfClientTest, SetCpuPercent, TestSize.Level1)
250 {
251 HiperfClient::RecordOption opt;
252 vector<pid_t> selectPids = {getpid()};
253 opt.SetSelectPids(selectPids);
254 opt.SetCpuPercent(50);
255
256 TestCaseOption(opt);
257 }
258
259 HWTEST_F(HiperfClientTest, SetOffCPU, TestSize.Level1)
260 {
261 HiperfClient::RecordOption opt;
262 vector<pid_t> selectPids = {getpid()};
263 opt.SetSelectPids(selectPids);
264 opt.SetOffCPU(true);
265
266 TestCaseOption(opt);
267 }
268
269 HWTEST_F(HiperfClientTest, SetCallStack, TestSize.Level1)
270 {
271 HiperfClient::RecordOption opt;
272 vector<pid_t> selectPids = {getpid()};
273 opt.SetSelectPids(selectPids);
274 opt.SetCallGraph("fp");
275
276 TestCaseOption(opt);
277 }
278
279 HWTEST_F(HiperfClientTest, SetDelayUnwind, TestSize.Level1)
280 {
281 HiperfClient::RecordOption opt;
282 vector<pid_t> selectPids = {getpid()};
283 opt.SetSelectPids(selectPids);
284 opt.SetDelayUnwind(true);
285
286 TestCaseOption(opt);
287 }
288
289 HWTEST_F(HiperfClientTest, SetDisableUnwind, TestSize.Level1)
290 {
291 HiperfClient::RecordOption opt;
292 vector<pid_t> selectPids = {getpid()};
293 opt.SetSelectPids(selectPids);
294 opt.SetDisableUnwind(true);
295
296 TestCaseOption(opt);
297 }
298
299 HWTEST_F(HiperfClientTest, SetDisableCallstackMerge, TestSize.Level1)
300 {
301 HiperfClient::RecordOption opt;
302 vector<pid_t> selectPids = {getpid()};
303 opt.SetSelectPids(selectPids);
304 opt.SetDisableCallstackMerge(true);
305
306 TestCaseOption(opt);
307 }
308
309 HWTEST_F(HiperfClientTest, SetOutputFilename, TestSize.Level1)
310 {
311 HiperfClient::RecordOption opt;
312 vector<pid_t> selectPids = {getpid()};
313 opt.SetSelectPids(selectPids);
314 opt.SetOutputFilename("perf.data.ut");
315
316 TestCaseOption(opt);
317 }
318
319 HWTEST_F(HiperfClientTest, SetSymbolDir, TestSize.Level1)
320 {
321 HiperfClient::RecordOption opt;
322 vector<pid_t> selectPids = {getpid()};
323 opt.SetSelectPids(selectPids);
324 opt.SetSymbolDir("/data/local/tmp/");
325
326 TestCaseOption(opt);
327 }
328
329 HWTEST_F(HiperfClientTest, SetDataLimit, TestSize.Level1)
330 {
331 HiperfClient::RecordOption opt;
332 vector<pid_t> selectPids = {getpid()};
333 opt.SetSelectPids(selectPids);
334 opt.SetDataLimit("100M");
335
336 TestCaseOption(opt);
337 }
338
339 HWTEST_F(HiperfClientTest, SetAppPackage, TestSize.Level1)
340 {
341 HiperfClient::RecordOption opt;
342 opt.SetAppPackage("com.ohos.launcher");
343
344 TestCaseOption(opt);
345 }
346
347 HWTEST_F(HiperfClientTest, SetClockId, TestSize.Level1)
348 {
349 HiperfClient::RecordOption opt;
350 vector<pid_t> selectPids = {getpid()};
351 opt.SetSelectPids(selectPids);
352 opt.SetClockId("monotonic");
353
354 TestCaseOption(opt);
355 }
356
357 HWTEST_F(HiperfClientTest, SetMmapPages, TestSize.Level1)
358 {
359 HiperfClient::RecordOption opt;
360 vector<pid_t> selectPids = {getpid()};
361 opt.SetSelectPids(selectPids);
362 opt.SetMmapPages(64);
363
364 TestCaseOption(opt);
365 }
366 } // namespace HiPerf
367 } // namespace Developtools
368 } // namespace OHOS
369