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 26using namespace testing::ext; 27using namespace std; 28using namespace OHOS::HiviewDFX; 29namespace OHOS { 30namespace Developtools { 31namespace HiPerf { 32class HiperfClientTest : public testing::Test { 33public: 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 42void HiperfClientTest::SetUpTestCase() {} 43 44void HiperfClientTest::TearDownTestCase() 45{ 46 DebugLogger::GetInstance()->Reset(); 47} 48 49void HiperfClientTest::SetUp() {} 50 51void HiperfClientTest::TearDown() 52{ 53} 54 55/** 56 * @tc.name: 57 * @tc.desc: record 58 * @tc.type: FUNC 59 */ 60HWTEST_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 80HWTEST_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 101HWTEST_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 121HWTEST_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 142HWTEST_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 161HWTEST_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 172void 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 206HWTEST_F(HiperfClientTest, SetTargetSystemWide, TestSize.Level1) 207{ 208 HiperfClient::RecordOption opt; 209 opt.SetTargetSystemWide(true); 210 211 TestCaseOption(opt); 212} 213 214HWTEST_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 223HWTEST_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 234HWTEST_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 246HWTEST_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 256HWTEST_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 266HWTEST_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 277HWTEST_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 287HWTEST_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 297HWTEST_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 306HWTEST_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 318HWTEST_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 327HWTEST_F(HiperfClientTest, SetExcludePerf, TestSize.Level1) 328{ 329 HiperfClient::RecordOption opt; 330 opt.SetTargetSystemWide(true); 331 opt.SetExcludePerf(true); 332 333 TestCaseOption(opt); 334} 335 336HWTEST_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 346HWTEST_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 356HWTEST_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 366HWTEST_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 376HWTEST_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 386HWTEST_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 396HWTEST_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 406HWTEST_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 416HWTEST_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 426HWTEST_F(HiperfClientTest, SetAppPackage, TestSize.Level1) 427{ 428 HiperfClient::RecordOption opt; 429 std::string testProcesses = "com.ohos.sceneboard"; 430 if (!CheckTestApp(testProcesses)) { 431 testProcesses = "com.ohos.launcher"; 432 } 433 opt.SetAppPackage(testProcesses); 434 435 TestCaseOption(opt); 436} 437 438HWTEST_F(HiperfClientTest, SetClockId, TestSize.Level1) 439{ 440 HiperfClient::RecordOption opt; 441 vector<pid_t> selectPids = {getpid()}; 442 opt.SetSelectPids(selectPids); 443 opt.SetClockId("monotonic"); 444 445 TestCaseOption(opt); 446} 447 448HWTEST_F(HiperfClientTest, SetMmapPages, TestSize.Level1) 449{ 450 HiperfClient::RecordOption opt; 451 vector<pid_t> selectPids = {getpid()}; 452 opt.SetSelectPids(selectPids); 453 opt.SetMmapPages(64); 454 455 TestCaseOption(opt); 456} 457 458HWTEST_F(HiperfClientTest, SetReport, TestSize.Level1) 459{ 460 HiperfClient::RecordOption opt; 461 vector<pid_t> selectPids = {getpid()}; 462 opt.SetSelectPids(selectPids); 463 opt.SetReport(true); 464 465 TestCaseOption(opt); 466} 467 468HWTEST_F(HiperfClientTest, SetVecBranchSampleTypes, TestSize.Level1) 469{ 470 StdoutRecord stdoutRecord; 471 stdoutRecord.Start(); 472 473 HiperfClient::RecordOption opt; 474 vector<pid_t> selectPids = {getpid()}; 475 opt.SetSelectPids(selectPids); 476 std::vector<std::string> vecBranchSampleTypes = {"any", "any_call", "any_ret", "ind_call", "u", "k"}; 477 opt.SetVecBranchSampleTypes(vecBranchSampleTypes); 478 HiperfClient::Client myHiperf; 479 myHiperf.SetDebugMode(); 480 481 ASSERT_TRUE(myHiperf.IsReady()); 482#ifdef is_ohos 483 ASSERT_EQ(myHiperf.Start(opt), false); 484#else 485 ASSERT_TRUE(myHiperf.Start(opt)); 486 ASSERT_TRUE(myHiperf.Pause()); 487 this_thread::sleep_for(1s); 488 489 ASSERT_TRUE(myHiperf.Resume()); 490 this_thread::sleep_for(1s); 491 492 ASSERT_TRUE(myHiperf.Stop()); 493#endif 494 stdoutRecord.Stop(); 495} 496} // namespace HiPerf 497} // namespace Developtools 498} // namespace OHOS 499