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 #define final // disable final keyword 17 #define LOG_TAG "ProfilerServiceTest" 18 19 #include <grpcpp/grpcpp.h> 20 #include <grpcpp/health_check_service_interface.h> 21 #include <hwext/gtest-ext.h> 22 #include <hwext/gtest-tag.h> 23 #include <thread> 24 25 #include "logging.h" 26 #include "plugin_service.h" 27 #include "profiler_capability_manager.h" 28 #include "profiler_data_repeater.h" 29 #include "profiler_service.h" 30 #include "schedule_task_manager.h" 31 #include "common.h" 32 33 using namespace testing::ext; 34 35 using ProfilerServicePtr = STD_PTR(shared, ProfilerService); 36 37 namespace { 38 constexpr int ROUND_COUNT = 200; 39 constexpr int FETCH_DATA_DELAY_US = 100 * 1000; 40 constexpr int TEST_SESSION_TIMEOUT_MS = 1000; 41 constexpr int TIMEOUT_US = 3 * 1000 * 1000; 42 43 class Timer { 44 public: 45 using Clock = std::chrono::steady_clock; 46 using TimePoint = Clock::time_point; 47 Timer()48 Timer() : startTime_(Now()) {} 49 ~Timer()50 ~Timer() {} 51 ElapsedUs()52 long ElapsedUs() 53 { 54 auto currentTime = Now(); 55 return std::chrono::duration_cast<std::chrono::microseconds>(currentTime - startTime_).count(); 56 } 57 Reset()58 void Reset() 59 { 60 startTime_ = Now(); 61 } 62 63 protected: Now()64 TimePoint Now() 65 { 66 return Clock::now(); 67 } 68 69 private: 70 TimePoint startTime_; 71 }; 72 73 class ProfilerServiceTest : public ::testing::Test { 74 protected: 75 PluginServicePtr pluginService_; 76 ProfilerServicePtr service_; 77 78 PluginInfo pluginInfo; 79 std::unique_ptr<grpc::ServerContext> context_; 80 std::atomic<int> requestCounter = 0; 81 SetUpTestCase()82 static void SetUpTestCase() {} TearDownTestCase()83 static void TearDownTestCase() {} 84 SetUp()85 void SetUp() override 86 { 87 pluginInfo.name = "test_plugin"; 88 pluginService_ = std::make_shared<PluginService>(); 89 if (pluginService_) { 90 pluginService_->AddPluginInfo(pluginInfo); 91 } 92 service_ = std::make_shared<ProfilerService>(pluginService_); 93 context_ = std::make_unique<grpc::ServerContext>(); 94 } 95 TearDown()96 void TearDown() override 97 { 98 context_.reset(); 99 service_.reset(); 100 if (pluginService_) { 101 pluginService_->RemovePluginInfo(pluginInfo); 102 } 103 pluginService_.reset(); 104 ProfilerCapabilityManager::GetInstance().pluginCapabilities_.clear(); 105 } 106 AddPlugin(const std::string & pluginName)107 void AddPlugin(const std::string& pluginName) 108 { 109 PluginInfo pluginInfo; 110 pluginInfo.name = pluginName; 111 if (pluginService_) { 112 pluginService_->AddPluginInfo(pluginInfo); 113 } 114 } 115 StartSession(uint32_t sessionId)116 grpc::Status StartSession(uint32_t sessionId) 117 { 118 StartSessionRequest request; 119 StartSessionResponse response; 120 121 request.set_session_id(sessionId); 122 request.set_request_id(++requestCounter); 123 return service_->StartSession(context_.get(), &request, &response); 124 } 125 StopSession(uint32_t sessionId)126 grpc::Status StopSession(uint32_t sessionId) 127 { 128 StopSessionRequest request; 129 StopSessionResponse response; 130 131 request.set_session_id(sessionId); 132 request.set_request_id(++requestCounter); 133 return service_->StopSession(context_.get(), &request, &response); 134 } 135 DestroySession(uint32_t sessionId)136 grpc::Status DestroySession(uint32_t sessionId) 137 { 138 DestroySessionRequest request; 139 DestroySessionResponse response; 140 141 request.set_session_id(sessionId); 142 request.set_request_id(++requestCounter); 143 return service_->DestroySession(context_.get(), &request, &response); 144 } 145 CreateOnlineSession()146 uint32_t CreateOnlineSession() 147 { 148 CreateSessionRequest request; 149 CreateSessionResponse response; 150 151 auto sessionConfig = request.mutable_session_config(); 152 CHECK_NOTNULL(sessionConfig, 0, "request.mutable_session_config() return nullptr!"); 153 154 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 155 auto pluginConfig = request.add_plugin_configs(); 156 CHECK_NOTNULL(pluginConfig, 0, "request.add_plugin_configs() return nullptr!"); 157 158 pluginConfig->set_name(pluginInfo.name); 159 request.set_request_id(++requestCounter); 160 auto status = service_->CreateSession(context_.get(), &request, &response); 161 CHECK_TRUE(status.error_code() == grpc::StatusCode::OK, 0, "create session FAILED!"); 162 163 return response.session_id(); 164 } 165 CreateOfflineSession()166 uint32_t CreateOfflineSession() 167 { 168 CreateSessionRequest request; 169 CreateSessionResponse response; 170 171 auto sessionConfig = request.mutable_session_config(); 172 CHECK_NOTNULL(sessionConfig, 0, "request.mutable_session_config() return nullptr!"); 173 174 sessionConfig->set_session_mode(ProfilerSessionConfig::OFFLINE); 175 sessionConfig->set_result_file("trace.bin"); 176 auto pluginConfig = request.add_plugin_configs(); 177 CHECK_NOTNULL(pluginConfig, 0, "request.add_plugin_configs() return nullptr!"); 178 179 pluginConfig->set_name(pluginInfo.name); 180 request.set_request_id(++requestCounter); 181 auto status = service_->CreateSession(context_.get(), &request, &response); 182 CHECK_TRUE(status.error_code() == grpc::StatusCode::OK, 0, "create session FAILED!"); 183 184 return response.session_id(); 185 } 186 }; 187 188 /** 189 * @tc.name: server 190 * @tc.desc: Abnormal test. 191 * @tc.type: FUNC 192 */ 193 HWTEST_F(ProfilerServiceTest, CtorDtor, TestSize.Level1) 194 { 195 ASSERT_NE(service_, nullptr); 196 } 197 198 /** 199 * @tc.name: server 200 * @tc.desc: get plugin capabilities normal. 201 * @tc.type: FUNC 202 */ 203 HWTEST_F(ProfilerServiceTest, GetCapabilities, TestSize.Level1) 204 { 205 ASSERT_NE(service_, nullptr); 206 ASSERT_NE(context_, nullptr); 207 208 GetCapabilitiesRequest request; 209 auto response = std::make_unique<GetCapabilitiesResponse>(); 210 ASSERT_NE(response, nullptr); 211 212 ProfilerPluginCapability cap; 213 cap.set_name("cap1"); 214 215 request.set_request_id(++requestCounter); 216 auto status = service_->GetCapabilities(context_.get(), &request, response.get()); 217 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 218 EXPECT_EQ(response->capabilities_size(), ProfilerCapabilityManager::GetInstance().GetCapabilities().size()); 219 220 int capSize = response->capabilities_size(); 221 EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(cap)); 222 223 request.set_request_id(++requestCounter); 224 response = std::make_unique<GetCapabilitiesResponse>(); 225 status = service_->GetCapabilities(context_.get(), &request, response.get()); 226 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 227 EXPECT_EQ(response->capabilities_size(), ProfilerCapabilityManager::GetInstance().GetCapabilities().size()); 228 EXPECT_EQ(response->capabilities_size(), capSize + 1); 229 } 230 231 /** 232 * @tc.name: server 233 * @tc.desc: get plugin capabilities normal. 234 * @tc.type: FUNC 235 */ 236 HWTEST_F(ProfilerServiceTest, GetCapabilitiesAfterRemove, TestSize.Level1) 237 { 238 ASSERT_NE(service_, nullptr); 239 ASSERT_NE(context_, nullptr); 240 241 GetCapabilitiesRequest request; 242 auto response = std::make_unique<GetCapabilitiesResponse>(); 243 ASSERT_NE(response, nullptr); 244 245 ProfilerPluginCapability cap; 246 cap.set_name("cap1"); 247 248 request.set_request_id(++requestCounter); 249 auto status = service_->GetCapabilities(context_.get(), &request, response.get()); 250 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 251 EXPECT_EQ(response->capabilities_size(), ProfilerCapabilityManager::GetInstance().GetCapabilities().size()); 252 253 int capSize = response->capabilities_size(); 254 EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(cap)); 255 256 request.set_request_id(++requestCounter); 257 response = std::make_unique<GetCapabilitiesResponse>(); 258 status = service_->GetCapabilities(context_.get(), &request, response.get()); 259 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 260 EXPECT_EQ(response->capabilities_size(), capSize + 1); 261 262 EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().RemoveCapability(cap.name())); 263 response = std::make_unique<GetCapabilitiesResponse>(); 264 status = service_->GetCapabilities(context_.get(), &request, response.get()); 265 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 266 EXPECT_EQ(response->capabilities_size(), ProfilerCapabilityManager::GetInstance().GetCapabilities().size()); 267 EXPECT_EQ(response->capabilities_size(), capSize); 268 } 269 270 /** 271 * @tc.name: server 272 * @tc.desc: get plugin capabilities batch test. 273 * @tc.type: FUNC 274 */ 275 HWTEST_F(ProfilerServiceTest, GetCapabilitiesBatchTest, TestSize.Level1) 276 { 277 ASSERT_NE(service_, nullptr); 278 ASSERT_NE(context_, nullptr); 279 280 GetCapabilitiesRequest request; 281 auto response = std::make_unique<GetCapabilitiesResponse>(); 282 ASSERT_NE(response, nullptr); 283 284 request.set_request_id(++requestCounter); 285 auto status = service_->GetCapabilities(context_.get(), &request, response.get()); 286 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 287 EXPECT_EQ(response->capabilities_size(), ProfilerCapabilityManager::GetInstance().GetCapabilities().size()); 288 289 for (int i = 0; i < ROUND_COUNT; i++) { 290 ProfilerPluginCapability cap; 291 cap.set_name("cap_" + std::to_string(i)); 292 EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(cap)); 293 } 294 295 Timer timer = {}; 296 for (int i = 0; i < ROUND_COUNT; i++) { 297 request.set_request_id(++requestCounter); 298 response = std::make_unique<GetCapabilitiesResponse>(); 299 status = service_->GetCapabilities(context_.get(), &request, response.get()); 300 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 301 } 302 auto timeCost = timer.ElapsedUs(); 303 printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost); 304 EXPECT_LE(timeCost, TIMEOUT_US); 305 } 306 307 /** 308 * @tc.name: server 309 * @tc.desc: get plugin capabilities with invalid context. 310 * @tc.type: FUNC 311 */ 312 HWTEST_F(ProfilerServiceTest, GetCapabilitiesWithInvalidContext, TestSize.Level1) 313 { 314 ASSERT_NE(service_, nullptr); 315 ASSERT_NE(context_, nullptr); 316 317 GetCapabilitiesRequest request; 318 auto response = std::make_unique<GetCapabilitiesResponse>(); 319 ASSERT_NE(response, nullptr); 320 321 auto status = service_->GetCapabilities(nullptr, &request, response.get()); 322 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 323 } 324 325 /** 326 * @tc.name: server 327 * @tc.desc: get plugin capabilities with invalid arguments. 328 * @tc.type: FUNC 329 */ 330 HWTEST_F(ProfilerServiceTest, GetCapabilitiesWithInvalidArguments, TestSize.Level1) 331 { 332 ASSERT_NE(service_, nullptr); 333 ASSERT_NE(context_, nullptr); 334 335 GetCapabilitiesRequest request; 336 auto response = std::make_unique<GetCapabilitiesResponse>(); 337 ASSERT_NE(response, nullptr); 338 339 auto status = service_->GetCapabilities(context_.get(), nullptr, response.get()); 340 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 341 342 status = service_->GetCapabilities(context_.get(), &request, nullptr); 343 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 344 345 status = service_->GetCapabilities(context_.get(), nullptr, nullptr); 346 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 347 } 348 349 /** 350 * @tc.name: server 351 * @tc.desc: create session without plugin config. 352 * @tc.type: FUNC 353 */ 354 HWTEST_F(ProfilerServiceTest, CreateSessionNoPluginConfig, TestSize.Level1) 355 { 356 ASSERT_NE(service_, nullptr); 357 ASSERT_NE(context_, nullptr); 358 359 CreateSessionRequest request; 360 CreateSessionResponse response; 361 362 auto status = service_->CreateSession(context_.get(), &request, &response); 363 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 364 } 365 366 /** 367 * @tc.name: server 368 * @tc.desc: create session offline test. 369 * @tc.type: FUNC 370 */ 371 HWTEST_F(ProfilerServiceTest, CreateSessionOffline, TestSize.Level1) 372 { 373 ASSERT_NE(service_, nullptr); 374 ASSERT_NE(context_, nullptr); 375 376 CreateSessionRequest request; 377 CreateSessionResponse response; 378 379 auto sessionConfig = request.mutable_session_config(); 380 ASSERT_NE(sessionConfig, nullptr); 381 sessionConfig->set_session_mode(ProfilerSessionConfig::OFFLINE); 382 sessionConfig->set_result_file("trace.bin"); 383 384 auto pluginConfig = request.add_plugin_configs(); 385 ASSERT_NE(pluginConfig, nullptr); 386 pluginConfig->set_name(pluginInfo.name); 387 388 request.set_request_id(++requestCounter); 389 auto status = service_->CreateSession(context_.get(), &request, &response); 390 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 391 } 392 393 /** 394 * @tc.name: server 395 * @tc.desc: create session online test. 396 * @tc.type: FUNC 397 */ 398 HWTEST_F(ProfilerServiceTest, CreateSessionOnline, TestSize.Level1) 399 { 400 ASSERT_NE(service_, nullptr); 401 ASSERT_NE(context_, nullptr); 402 403 CreateSessionRequest request; 404 CreateSessionResponse response; 405 406 auto sessionConfig = request.mutable_session_config(); 407 ASSERT_NE(sessionConfig, nullptr); 408 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 409 sessionConfig->clear_result_file(); 410 411 auto pluginConfig = request.add_plugin_configs(); 412 ASSERT_NE(pluginConfig, nullptr); 413 pluginConfig->set_name(pluginInfo.name); 414 415 request.set_request_id(++requestCounter); 416 auto status = service_->CreateSession(context_.get(), &request, &response); 417 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 418 } 419 420 /** 421 * @tc.name: server 422 * @tc.desc: create session batch test. 423 * @tc.type: FUNC 424 */ 425 HWTEST_F(ProfilerServiceTest, CreateSessionBatchTest, TestSize.Level1) 426 { 427 ASSERT_NE(service_, nullptr); 428 ASSERT_NE(context_, nullptr); 429 430 Timer timer = {}; 431 for (int i = 0; i < ROUND_COUNT; i++) { 432 CreateSessionRequest request; 433 CreateSessionResponse response; 434 435 auto sessionConfig = request.mutable_session_config(); 436 ASSERT_NE(sessionConfig, nullptr); 437 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 438 sessionConfig->clear_result_file(); 439 440 auto pluginConfig = request.add_plugin_configs(); 441 ASSERT_NE(pluginConfig, nullptr); 442 std::string pluginName = "create_session_batch_test_" + std::to_string(i); 443 AddPlugin(pluginName); 444 pluginConfig->set_name(pluginName); 445 446 request.set_request_id(++requestCounter); 447 auto status = service_->CreateSession(context_.get(), &request, &response); 448 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 449 } 450 auto timeCost = timer.ElapsedUs(); 451 printf("CreateSession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 452 EXPECT_LE(timeCost, TIMEOUT_US); 453 } 454 455 /** 456 * @tc.name: server 457 * @tc.desc: create session with invalid arguments. 458 * @tc.type: FUNC 459 */ 460 HWTEST_F(ProfilerServiceTest, CreateSessionInvalidArguments, TestSize.Level1) 461 { 462 ASSERT_NE(service_, nullptr); 463 ASSERT_NE(context_, nullptr); 464 465 CreateSessionRequest request; 466 auto response = std::make_unique<CreateSessionResponse>(); 467 ASSERT_NE(response, nullptr); 468 469 auto status = service_->CreateSession(context_.get(), nullptr, response.get()); 470 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 471 472 status = service_->CreateSession(context_.get(), &request, nullptr); 473 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 474 475 status = service_->CreateSession(context_.get(), nullptr, nullptr); 476 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 477 } 478 479 /** 480 * @tc.name: server 481 * @tc.desc: create session with invalid context. 482 * @tc.type: FUNC 483 */ 484 HWTEST_F(ProfilerServiceTest, CreateSessionWithInvalidContext, TestSize.Level1) 485 { 486 ASSERT_NE(service_, nullptr); 487 ASSERT_NE(context_, nullptr); 488 489 CreateSessionRequest request; 490 auto response = std::make_unique<CreateSessionResponse>(); 491 ASSERT_NE(response, nullptr); 492 493 auto status = service_->CreateSession(nullptr, &request, response.get()); 494 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 495 } 496 497 /** 498 * @tc.name: server 499 * @tc.desc: destroy session with invalid session id. 500 * @tc.type: FUNC 501 */ 502 HWTEST_F(ProfilerServiceTest, DestroySessionInvalidSessionId, TestSize.Level1) 503 { 504 ASSERT_NE(service_, nullptr); 505 506 EXPECT_NE(DestroySession(0).error_code(), grpc::StatusCode::OK); 507 } 508 509 /** 510 * @tc.name: server 511 * @tc.desc: destroy session with offline session. 512 * @tc.type: FUNC 513 */ 514 HWTEST_F(ProfilerServiceTest, DestroySessionOffline, TestSize.Level1) 515 { 516 ASSERT_NE(service_, nullptr); 517 518 uint32_t sessionId = CreateOfflineSession(); 519 EXPECT_NE(sessionId, 0); 520 521 EXPECT_EQ(DestroySession(sessionId).error_code(), grpc::StatusCode::OK); 522 } 523 524 /** 525 * @tc.name: server 526 * @tc.desc: destroy session with online session. 527 * @tc.type: FUNC 528 */ 529 HWTEST_F(ProfilerServiceTest, DestroySessionOnline, TestSize.Level1) 530 { 531 ASSERT_NE(service_, nullptr); 532 533 uint32_t sessionId = CreateOnlineSession(); 534 EXPECT_NE(sessionId, 0); 535 536 EXPECT_EQ(DestroySession(sessionId).error_code(), grpc::StatusCode::OK); 537 } 538 539 /** 540 * @tc.name: server 541 * @tc.desc: destroy session batch test. 542 * @tc.type: FUNC 543 */ 544 HWTEST_F(ProfilerServiceTest, DestroySessionBatchTest, TestSize.Level1) 545 { 546 ASSERT_NE(service_, nullptr); 547 ASSERT_NE(context_, nullptr); 548 549 std::vector<uint32_t> sessionIds; 550 Timer timer = {}; 551 for (int i = 0; i < ROUND_COUNT; i++) { 552 uint32_t sessionId = 0; 553 { 554 CreateSessionRequest request; 555 CreateSessionResponse response; 556 557 auto sessionConfig = request.mutable_session_config(); 558 ASSERT_NE(sessionConfig, nullptr); 559 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 560 561 auto pluginConfig = request.add_plugin_configs(); 562 ASSERT_NE(pluginConfig, nullptr); 563 564 std::string pluginName = "create_session_batch_test_" + std::to_string(i); 565 AddPlugin(pluginName); 566 pluginConfig->set_name(pluginName); 567 568 request.set_request_id(++requestCounter); 569 auto status = service_->CreateSession(context_.get(), &request, &response); 570 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 571 sessionId = response.session_id(); 572 } 573 if (sessionId) { 574 sessionIds.push_back(sessionId); 575 } 576 } 577 auto timeCost = timer.ElapsedUs(); 578 printf("CreateSession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 579 EXPECT_LE(timeCost, TIMEOUT_US); 580 581 timer.Reset(); 582 for (auto sessionId : sessionIds) { 583 EXPECT_EQ(DestroySession(sessionId).error_code(), grpc::StatusCode::OK); 584 } 585 timeCost = timer.ElapsedUs(); 586 printf("DestroySession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 587 EXPECT_LE(timeCost, TIMEOUT_US); 588 } 589 590 /** 591 * @tc.name: server 592 * @tc.desc: destroy session with invalid arguments. 593 * @tc.type: FUNC 594 */ 595 HWTEST_F(ProfilerServiceTest, DestroySessionInvalidArguments, TestSize.Level1) 596 { 597 ASSERT_NE(service_, nullptr); 598 ASSERT_NE(context_, nullptr); 599 600 uint32_t sessionId = CreateOnlineSession(); 601 EXPECT_NE(sessionId, 0); 602 603 DestroySessionRequest request; 604 auto response = std::make_unique<DestroySessionResponse>(); 605 ASSERT_NE(response, nullptr); 606 607 auto status = service_->DestroySession(context_.get(), nullptr, response.get()); 608 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 609 610 status = service_->DestroySession(context_.get(), &request, nullptr); 611 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 612 613 status = service_->DestroySession(context_.get(), nullptr, nullptr); 614 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 615 } 616 617 /** 618 * @tc.name: server 619 * @tc.desc: destroy session with invalid context. 620 * @tc.type: FUNC 621 */ 622 HWTEST_F(ProfilerServiceTest, DestroySessionWithInvalidContext, TestSize.Level1) 623 { 624 ASSERT_NE(service_, nullptr); 625 ASSERT_NE(context_, nullptr); 626 627 uint32_t sessionId = CreateOnlineSession(); 628 EXPECT_NE(sessionId, 0); 629 630 DestroySessionRequest request; 631 auto response = std::make_unique<DestroySessionResponse>(); 632 ASSERT_NE(response, nullptr); 633 634 auto status = service_->DestroySession(nullptr, &request, response.get()); 635 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 636 } 637 638 /** 639 * @tc.name: server 640 * @tc.desc: start session with invalid session id. 641 * @tc.type: FUNC 642 */ 643 HWTEST_F(ProfilerServiceTest, StartSessionInvalidSessionId, TestSize.Level1) 644 { 645 ASSERT_NE(service_, nullptr); 646 ASSERT_NE(context_, nullptr); 647 648 auto status = StartSession(0); 649 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 650 } 651 652 /** 653 * @tc.name: server 654 * @tc.desc: start session with valid offline session. 655 * @tc.type: FUNC 656 */ 657 HWTEST_F(ProfilerServiceTest, StartSessionOffline, TestSize.Level1) 658 { 659 uint32_t sessionId = CreateOfflineSession(); 660 EXPECT_NE(sessionId, 0); 661 662 auto status = StartSession(sessionId); 663 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 664 } 665 666 /** 667 * @tc.name: server 668 * @tc.desc: start session with valid online session. 669 * @tc.type: FUNC 670 */ 671 HWTEST_F(ProfilerServiceTest, StartSessionOnline, TestSize.Level1) 672 { 673 uint32_t sessionId = CreateOnlineSession(); 674 EXPECT_NE(sessionId, 0); 675 676 auto status = StartSession(sessionId); 677 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 678 } 679 680 /** 681 * @tc.name: server 682 * @tc.desc: start session and update configs. 683 * @tc.type: FUNC 684 */ 685 HWTEST_F(ProfilerServiceTest, StartSessionUpdateConfigs, TestSize.Level1) 686 { 687 ASSERT_NE(service_, nullptr); 688 ASSERT_NE(context_, nullptr); 689 690 uint32_t sessionId = CreateOfflineSession(); 691 EXPECT_NE(sessionId, 0); 692 693 StartSessionRequest request; 694 StartSessionResponse response; 695 696 request.set_session_id(sessionId); 697 request.set_request_id(++requestCounter); 698 auto pluginConfig = request.add_update_configs(); 699 ASSERT_NE(pluginConfig, nullptr); 700 701 pluginConfig->set_name(pluginInfo.name); 702 pluginConfig->set_sample_interval(1000); 703 auto status = service_->StartSession(context_.get(), &request, &response); 704 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 705 } 706 707 /** 708 * @tc.name: server 709 * @tc.desc: start session batch test. 710 * @tc.type: FUNC 711 */ 712 HWTEST_F(ProfilerServiceTest, StartSessionBatchTest, TestSize.Level1) 713 { 714 ASSERT_NE(service_, nullptr); 715 ASSERT_NE(context_, nullptr); 716 717 std::vector<uint32_t> sessionIds; 718 Timer timer = {}; 719 for (int i = 0; i < ROUND_COUNT; i++) { 720 uint32_t sessionId = 0; 721 { 722 CreateSessionRequest request; 723 CreateSessionResponse response; 724 725 auto sessionConfig = request.mutable_session_config(); 726 ASSERT_NE(sessionConfig, nullptr); 727 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 728 729 auto pluginConfig = request.add_plugin_configs(); 730 ASSERT_NE(pluginConfig, nullptr); 731 732 std::string pluginName = "create_session_batch_test_" + std::to_string(i); 733 AddPlugin(pluginName); 734 pluginConfig->set_name(pluginName); 735 736 request.set_request_id(++requestCounter); 737 auto status = service_->CreateSession(context_.get(), &request, &response); 738 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 739 sessionId = response.session_id(); 740 } 741 if (sessionId) { 742 sessionIds.push_back(sessionId); 743 } 744 } 745 auto timeCost = timer.ElapsedUs(); 746 printf("CreateSession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 747 EXPECT_LE(timeCost, TIMEOUT_US); 748 749 timer.Reset(); 750 for (auto sessionId : sessionIds) { 751 EXPECT_EQ(StartSession(sessionId).error_code(), grpc::StatusCode::OK); 752 } 753 timeCost = timer.ElapsedUs(); 754 printf("StartSession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 755 EXPECT_LE(timeCost, TIMEOUT_US); 756 } 757 758 /** 759 * @tc.name: server 760 * @tc.desc: start session with invalid arguments. 761 * @tc.type: FUNC 762 */ 763 HWTEST_F(ProfilerServiceTest, StartSessionInvalidArguments, TestSize.Level1) 764 { 765 ASSERT_NE(service_, nullptr); 766 ASSERT_NE(context_, nullptr); 767 768 uint32_t sessionId = CreateOnlineSession(); 769 EXPECT_NE(sessionId, 0); 770 771 StartSessionRequest request; 772 auto response = std::make_unique<StartSessionResponse>(); 773 ASSERT_NE(response, nullptr); 774 775 auto status = service_->StartSession(context_.get(), nullptr, response.get()); 776 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 777 778 status = service_->StartSession(context_.get(), &request, nullptr); 779 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 780 781 status = service_->StartSession(context_.get(), nullptr, nullptr); 782 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 783 } 784 785 /** 786 * @tc.name: server 787 * @tc.desc: start session with invalid context. 788 * @tc.type: FUNC 789 */ 790 HWTEST_F(ProfilerServiceTest, StartSessionWithInvalidContext, TestSize.Level1) 791 { 792 ASSERT_NE(service_, nullptr); 793 ASSERT_NE(context_, nullptr); 794 795 uint32_t sessionId = CreateOnlineSession(); 796 EXPECT_NE(sessionId, 0); 797 798 StartSessionRequest request; 799 auto response = std::make_unique<StartSessionResponse>(); 800 ASSERT_NE(response, nullptr); 801 802 auto status = service_->StartSession(nullptr, &request, response.get()); 803 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 804 } 805 806 /** 807 * @tc.name: server 808 * @tc.desc: stop session with invalid session id. 809 * @tc.type: FUNC 810 */ 811 HWTEST_F(ProfilerServiceTest, StopSessionInvalidSessionId, TestSize.Level1) 812 { 813 ASSERT_NE(service_, nullptr); 814 ASSERT_NE(context_, nullptr); 815 816 auto status = StopSession(0); 817 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 818 } 819 820 /** 821 * @tc.name: server 822 * @tc.desc: stop session with offline session. 823 * @tc.type: FUNC 824 */ 825 HWTEST_F(ProfilerServiceTest, StopSessionOffline, TestSize.Level1) 826 { 827 uint32_t sessionId = CreateOfflineSession(); 828 EXPECT_NE(sessionId, 0); 829 830 auto status = StopSession(sessionId); 831 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 832 } 833 834 /** 835 * @tc.name: server 836 * @tc.desc: stop session with online session. 837 * @tc.type: FUNC 838 */ 839 HWTEST_F(ProfilerServiceTest, StopSessionOnline, TestSize.Level1) 840 { 841 uint32_t sessionId = CreateOnlineSession(); 842 EXPECT_NE(sessionId, 0); 843 { 844 auto status = StartSession(sessionId); 845 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 846 } 847 848 auto status = StopSession(sessionId); 849 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 850 } 851 852 /** 853 * @tc.name: server 854 * @tc.desc: stop session batch test. 855 * @tc.type: FUNC 856 */ 857 HWTEST_F(ProfilerServiceTest, StopSessionBatchTest, TestSize.Level1) 858 { 859 ASSERT_NE(service_, nullptr); 860 ASSERT_NE(context_, nullptr); 861 862 std::vector<uint32_t> sessionIds; 863 Timer timer = {}; 864 for (int i = 0; i < ROUND_COUNT; i++) { 865 uint32_t sessionId = 0; 866 { 867 CreateSessionRequest request; 868 CreateSessionResponse response; 869 870 auto sessionConfig = request.mutable_session_config(); 871 ASSERT_NE(sessionConfig, nullptr); 872 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 873 874 auto pluginConfig = request.add_plugin_configs(); 875 ASSERT_NE(pluginConfig, nullptr); 876 877 std::string pluginName = "create_session_batch_test_" + std::to_string(i); 878 AddPlugin(pluginName); 879 pluginConfig->set_name(pluginName); 880 881 request.set_request_id(++requestCounter); 882 auto status = service_->CreateSession(context_.get(), &request, &response); 883 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 884 sessionId = response.session_id(); 885 } 886 if (sessionId) { 887 sessionIds.push_back(sessionId); 888 } 889 } 890 auto timeCost = timer.ElapsedUs(); 891 printf("CreateSession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 892 EXPECT_LE(timeCost, TIMEOUT_US); 893 894 timer.Reset(); 895 for (auto sessionId : sessionIds) { 896 EXPECT_EQ(StartSession(sessionId).error_code(), grpc::StatusCode::OK); 897 } 898 timeCost = timer.ElapsedUs(); 899 printf("StartSession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 900 EXPECT_LE(timeCost, TIMEOUT_US); 901 902 timer.Reset(); 903 for (auto sessionId : sessionIds) { 904 EXPECT_EQ(StopSession(sessionId).error_code(), grpc::StatusCode::OK); 905 } 906 timeCost = timer.ElapsedUs(); 907 printf("StopSession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 908 EXPECT_LE(timeCost, TIMEOUT_US); 909 } 910 911 /** 912 * @tc.name: server 913 * @tc.desc: stop session with invalid arguments. 914 * @tc.type: FUNC 915 */ 916 HWTEST_F(ProfilerServiceTest, StopSessionInvalidArguments, TestSize.Level1) 917 { 918 ASSERT_NE(service_, nullptr); 919 ASSERT_NE(context_, nullptr); 920 921 uint32_t sessionId = CreateOnlineSession(); 922 EXPECT_NE(sessionId, 0); 923 EXPECT_EQ(StartSession(sessionId).error_code(), grpc::StatusCode::OK); 924 925 StopSessionRequest request; 926 auto response = std::make_unique<StopSessionResponse>(); 927 ASSERT_NE(response, nullptr); 928 929 auto status = service_->StopSession(context_.get(), nullptr, response.get()); 930 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 931 932 status = service_->StopSession(context_.get(), &request, nullptr); 933 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 934 935 status = service_->StopSession(context_.get(), nullptr, nullptr); 936 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 937 } 938 939 /** 940 * @tc.name: server 941 * @tc.desc: stop session with invalid context. 942 * @tc.type: FUNC 943 */ 944 HWTEST_F(ProfilerServiceTest, StopSessionWithInvalidContext, TestSize.Level1) 945 { 946 ASSERT_NE(service_, nullptr); 947 ASSERT_NE(context_, nullptr); 948 949 uint32_t sessionId = CreateOnlineSession(); 950 EXPECT_NE(sessionId, 0); 951 EXPECT_EQ(StartSession(sessionId).error_code(), grpc::StatusCode::OK); 952 953 StopSessionRequest request; 954 auto response = std::make_unique<StopSessionResponse>(); 955 ASSERT_NE(response, nullptr); 956 957 auto status = service_->StopSession(nullptr, &request, response.get()); 958 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 959 } 960 961 class FakeServerWriter : public ::grpc::ServerWriter<::FetchDataResponse> { 962 public: FakeServerWriter(::grpc::internal::Call * call,grpc::ServerContext * ctx)963 FakeServerWriter(::grpc::internal::Call* call, grpc::ServerContext* ctx) 964 : ::grpc::ServerWriter<::FetchDataResponse>(call, ctx) 965 { 966 } 967 ~FakeServerWriter() = default; 968 GetDataCount() const969 uint32_t GetDataCount() const 970 { 971 return dataCount_; 972 } 973 974 using grpc::internal::WriterInterface<::FetchDataResponse>::Write; Write(const::FetchDataResponse & msg,::grpc::WriteOptions options)975 bool Write(const ::FetchDataResponse& msg, ::grpc::WriteOptions options) override 976 { 977 if (msg.plugin_data_size() > 0) { 978 dataCount_ += msg.plugin_data_size(); 979 } 980 return true; 981 } 982 983 private: 984 std::atomic<uint32_t> dataCount_ = 0; 985 }; 986 987 /** 988 * @tc.name: server 989 * @tc.desc: fetch data with invalid session id. 990 * @tc.type: FUNC 991 */ 992 HWTEST_F(ProfilerServiceTest, FetchDataInvalidSessionId, TestSize.Level1) 993 { 994 ASSERT_NE(service_, nullptr); 995 ASSERT_NE(context_, nullptr); 996 997 FetchDataRequest request; 998 auto writer = std::make_unique<FakeServerWriter>(nullptr, context_.get()); 999 1000 request.set_session_id(0); // invalid session id 1001 request.set_request_id(++requestCounter); 1002 auto status = service_->FetchData(context_.get(), &request, writer.get()); 1003 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1004 } 1005 1006 /** 1007 * @tc.name: server 1008 * @tc.desc: fetch data with offline. 1009 * @tc.type: FUNC 1010 */ 1011 HWTEST_F(ProfilerServiceTest, FetchDataOffline, TestSize.Level1) 1012 { 1013 ASSERT_NE(service_, nullptr); 1014 ASSERT_NE(context_, nullptr); 1015 1016 uint32_t sessionId = CreateOfflineSession(); 1017 EXPECT_NE(sessionId, 0); 1018 { 1019 auto status = StartSession(sessionId); 1020 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 1021 } 1022 1023 FetchDataRequest request; 1024 request.set_session_id(sessionId); 1025 request.set_request_id(++requestCounter); 1026 auto writer = std::make_unique<FakeServerWriter>(nullptr, context_.get()); 1027 auto status = service_->FetchData(context_.get(), &request, writer.get()); 1028 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 1029 1030 EXPECT_EQ(StopSession(sessionId).error_code(), grpc::StatusCode::OK); 1031 EXPECT_EQ(DestroySession(sessionId).error_code(), grpc::StatusCode::OK); 1032 } 1033 1034 /** 1035 * @tc.name: server 1036 * @tc.desc: fetch data online. 1037 * @tc.type: FUNC 1038 */ 1039 HWTEST_F(ProfilerServiceTest, FetchDataOnline, TestSize.Level1) 1040 { 1041 ASSERT_NE(service_, nullptr); 1042 ASSERT_NE(context_, nullptr); 1043 1044 uint32_t sessionId = CreateOnlineSession(); 1045 StartSession(sessionId); 1046 1047 auto sessionCtx = service_->GetSessionContext(sessionId); 1048 ASSERT_NE(sessionCtx->dataRepeater, nullptr); 1049 { 1050 auto data = std::make_shared<ProfilerPluginData>(); 1051 ASSERT_NE(data, nullptr); 1052 data->set_name(pluginInfo.name); 1053 sessionCtx->dataRepeater->PutPluginData(data); 1054 } 1055 1056 FetchDataRequest request; 1057 auto writer = std::make_unique<FakeServerWriter>(nullptr, context_.get()); 1058 request.set_session_id(sessionId); 1059 request.set_request_id(++requestCounter); __anonb09dd57f0202null1060 std::thread bgThread([&] { 1061 auto status = service_->FetchData(context_.get(), &request, writer.get()); 1062 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 1063 }); 1064 1065 usleep(FETCH_DATA_DELAY_US); 1066 sessionCtx->dataRepeater->Close(); 1067 bgThread.join(); 1068 1069 EXPECT_EQ(StopSession(sessionId).error_code(), grpc::StatusCode::OK); 1070 EXPECT_EQ(DestroySession(sessionId).error_code(), grpc::StatusCode::OK); 1071 1072 EXPECT_EQ(writer->GetDataCount(), 1); 1073 } 1074 1075 /** 1076 * @tc.name: server 1077 * @tc.desc: fetch data online batch test. 1078 * @tc.type: FUNC 1079 */ 1080 HWTEST_F(ProfilerServiceTest, FetchDataBatchTest, TestSize.Level1) 1081 { 1082 ASSERT_NE(service_, nullptr); 1083 ASSERT_NE(context_, nullptr); 1084 1085 auto writer = std::make_unique<FakeServerWriter>(nullptr, context_.get()); 1086 1087 uint32_t sessionId = CreateOnlineSession(); 1088 StartSession(sessionId); 1089 1090 auto sessionCtx = service_->GetSessionContext(sessionId); 1091 ASSERT_NE(sessionCtx->dataRepeater, nullptr); 1092 1093 // 只能存入小于maxsize的数据才能确保dataProducer线程正常退出 1094 int size = ProfilerService::DEFAULT_REPEATER_BUFFER_SIZE - 1; __anonb09dd57f0302() 1095 std::thread dataProducer([&]() { 1096 for (int i = 0; i < size; i++) { 1097 auto data = std::make_shared<ProfilerPluginData>(); 1098 ASSERT_NE(data, nullptr); 1099 data->set_name(pluginInfo.name); 1100 data->set_status(i); 1101 sessionCtx->dataRepeater->PutPluginData(data); 1102 } 1103 }); 1104 dataProducer.join(); 1105 __anonb09dd57f0402() 1106 std::thread closeDataRepeater([&]() { 1107 while (1) { 1108 if (sessionCtx->dataRepeater->Size() == 0) { 1109 sessionCtx->dataRepeater->Close(); 1110 break; 1111 } 1112 } 1113 }); 1114 1115 Timer timer = {}; __anonb09dd57f0502() 1116 std::thread dataConsumer([&]() { 1117 FetchDataRequest request; 1118 request.set_session_id(sessionId); 1119 request.set_request_id(++requestCounter); 1120 auto status = service_->FetchData(context_.get(), &request, writer.get()); 1121 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 1122 }); 1123 1124 dataConsumer.join(); 1125 auto timeCost = timer.ElapsedUs(); 1126 printf("FetchData %d time, cost %ldus.\n", size, timeCost); 1127 EXPECT_LE(timeCost, TIMEOUT_US); 1128 1129 EXPECT_EQ(StopSession(sessionId).error_code(), grpc::StatusCode::OK); 1130 EXPECT_EQ(DestroySession(sessionId).error_code(), grpc::StatusCode::OK); 1131 1132 EXPECT_EQ(writer->GetDataCount(), size); 1133 closeDataRepeater.join(); 1134 } 1135 1136 /** 1137 * @tc.name: server 1138 * @tc.desc: fetch data with invalid arguments. 1139 * @tc.type: FUNC 1140 */ 1141 HWTEST_F(ProfilerServiceTest, FetchDataInvalidArguments, TestSize.Level1) 1142 { 1143 ASSERT_NE(service_, nullptr); 1144 ASSERT_NE(context_, nullptr); 1145 1146 FetchDataRequest request; 1147 auto writer = std::make_unique<FakeServerWriter>(nullptr, context_.get()); 1148 request.set_request_id(++requestCounter); 1149 auto status = service_->FetchData(context_.get(), &request, writer.get()); 1150 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1151 1152 uint32_t sessionId = CreateOnlineSession(); 1153 1154 auto sessionCtx = service_->GetSessionContext(sessionId); 1155 ASSERT_NE(sessionCtx->dataRepeater, nullptr); 1156 { 1157 auto data = std::make_shared<ProfilerPluginData>(); 1158 ASSERT_NE(data, nullptr); 1159 data->set_name(pluginInfo.name); 1160 sessionCtx->dataRepeater->PutPluginData(data); 1161 } 1162 1163 status = service_->FetchData(context_.get(), nullptr, writer.get()); 1164 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1165 1166 status = service_->FetchData(context_.get(), &request, nullptr); 1167 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1168 1169 status = service_->FetchData(context_.get(), nullptr, nullptr); 1170 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1171 } 1172 1173 /** 1174 * @tc.name: server 1175 * @tc.desc: fetch data with invalid context. 1176 * @tc.type: FUNC 1177 */ 1178 HWTEST_F(ProfilerServiceTest, FetchDataInvalidContext, TestSize.Level1) 1179 { 1180 ASSERT_NE(service_, nullptr); 1181 ASSERT_NE(context_, nullptr); 1182 1183 FetchDataRequest request; 1184 auto writer = std::make_unique<FakeServerWriter>(nullptr, context_.get()); 1185 request.set_request_id(++requestCounter); 1186 auto status = service_->FetchData(context_.get(), &request, writer.get()); 1187 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1188 1189 uint32_t sessionId = CreateOnlineSession(); 1190 1191 auto sessionCtx = service_->GetSessionContext(sessionId); 1192 ASSERT_NE(sessionCtx->dataRepeater, nullptr); 1193 { 1194 auto data = std::make_shared<ProfilerPluginData>(); 1195 ASSERT_NE(data, nullptr); 1196 data->set_name(pluginInfo.name); 1197 sessionCtx->dataRepeater->PutPluginData(data); 1198 } 1199 1200 status = service_->FetchData(nullptr, &request, writer.get()); 1201 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1202 } 1203 1204 /** 1205 * @tc.name: server 1206 * @tc.desc: keep session with invalid session id. 1207 * @tc.type: FUNC 1208 */ 1209 HWTEST_F(ProfilerServiceTest, KeepSessionInvalidSessionId, TestSize.Level1) 1210 { 1211 ASSERT_NE(service_, nullptr); 1212 ASSERT_NE(context_, nullptr); 1213 1214 KeepSessionRequest request; 1215 KeepSessionResponse response; 1216 1217 request.set_session_id(0); 1218 request.set_request_id(++requestCounter); 1219 auto status = service_->KeepSession(context_.get(), &request, &response); 1220 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1221 } 1222 1223 /** 1224 * @tc.name: server 1225 * @tc.desc: keep session with offline session id. 1226 * @tc.type: FUNC 1227 */ 1228 HWTEST_F(ProfilerServiceTest, KeepSessionWithoutAliveTime, TestSize.Level1) 1229 { 1230 ASSERT_NE(service_, nullptr); 1231 ASSERT_NE(context_, nullptr); 1232 1233 uint32_t sessionId = CreateOfflineSession(); 1234 EXPECT_NE(sessionId, 0); 1235 1236 KeepSessionRequest request; 1237 KeepSessionResponse response; 1238 1239 request.set_session_id(sessionId); 1240 request.set_request_id(++requestCounter); 1241 auto status = service_->KeepSession(context_.get(), &request, &response); 1242 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 1243 } 1244 1245 /** 1246 * @tc.name: server 1247 * @tc.desc: keep session with offline session id. 1248 * @tc.type: FUNC 1249 */ 1250 HWTEST_F(ProfilerServiceTest, KeepSessionOffline, TestSize.Level1) 1251 { 1252 ASSERT_NE(service_, nullptr); 1253 ASSERT_NE(context_, nullptr); 1254 1255 uint32_t sessionId = CreateOfflineSession(); 1256 EXPECT_NE(sessionId, 0); 1257 1258 KeepSessionRequest request; 1259 KeepSessionResponse response; 1260 1261 request.set_session_id(sessionId); 1262 request.set_request_id(++requestCounter); 1263 request.set_keep_alive_time(TEST_SESSION_TIMEOUT_MS); 1264 auto status = service_->KeepSession(context_.get(), &request, &response); 1265 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 1266 1267 // make sure session expired by backgroud task 1268 std::this_thread::sleep_for(std::chrono::milliseconds(TEST_SESSION_TIMEOUT_MS)); 1269 std::this_thread::sleep_for(std::chrono::milliseconds(TEST_SESSION_TIMEOUT_MS)); 1270 1271 status = DestroySession(sessionId); 1272 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1273 } 1274 1275 /** 1276 * @tc.name: server 1277 * @tc.desc: keep session with batch test. 1278 * @tc.type: FUNC 1279 */ 1280 HWTEST_F(ProfilerServiceTest, KeepSessionBatchTest, TestSize.Level1) 1281 { 1282 ASSERT_NE(service_, nullptr); 1283 ASSERT_NE(context_, nullptr); 1284 1285 uint32_t sessionId = CreateOfflineSession(); 1286 EXPECT_NE(sessionId, 0); 1287 1288 Timer timer = {}; 1289 for (int i = 0; i < ROUND_COUNT; i++) { 1290 KeepSessionRequest request; 1291 KeepSessionResponse response; 1292 1293 request.set_session_id(sessionId); 1294 request.set_request_id(++requestCounter); 1295 request.set_keep_alive_time(TEST_SESSION_TIMEOUT_MS); 1296 auto status = service_->KeepSession(context_.get(), &request, &response); 1297 EXPECT_EQ(status.error_code(), grpc::StatusCode::OK); 1298 } 1299 auto timeCost = timer.ElapsedUs(); 1300 printf("KeepSession %d time, cost %ldus.\n", ROUND_COUNT, timeCost); 1301 EXPECT_LE(timeCost, TIMEOUT_US); 1302 1303 std::this_thread::sleep_for(std::chrono::milliseconds(TEST_SESSION_TIMEOUT_MS)); 1304 std::this_thread::sleep_for(std::chrono::milliseconds(TEST_SESSION_TIMEOUT_MS)); 1305 auto status = DestroySession(sessionId); 1306 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1307 } 1308 1309 /** 1310 * @tc.name: server 1311 * @tc.desc: keep session with invalid arguments. 1312 * @tc.type: FUNC 1313 */ 1314 HWTEST_F(ProfilerServiceTest, KeepSessionInvalidArgs, TestSize.Level1) 1315 { 1316 ASSERT_NE(service_, nullptr); 1317 ASSERT_NE(context_, nullptr); 1318 1319 uint32_t sessionId = CreateOfflineSession(); 1320 EXPECT_NE(sessionId, 0); 1321 1322 KeepSessionRequest request; 1323 KeepSessionResponse response; 1324 1325 request.set_session_id(sessionId); 1326 request.set_request_id(++requestCounter); 1327 request.set_keep_alive_time(TEST_SESSION_TIMEOUT_MS); 1328 auto status = service_->KeepSession(context_.get(), nullptr, &response); 1329 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1330 1331 status = service_->KeepSession(context_.get(), &request, nullptr); 1332 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1333 } 1334 1335 /** 1336 * @tc.name: server 1337 * @tc.desc: keep session with invalid context. 1338 * @tc.type: FUNC 1339 */ 1340 HWTEST_F(ProfilerServiceTest, KeepSessionInvalidContext, TestSize.Level1) 1341 { 1342 ASSERT_NE(service_, nullptr); 1343 ASSERT_NE(context_, nullptr); 1344 1345 uint32_t sessionId = CreateOfflineSession(); 1346 EXPECT_NE(sessionId, 0); 1347 1348 KeepSessionRequest request; 1349 KeepSessionResponse response; 1350 1351 request.set_session_id(sessionId); 1352 request.set_request_id(++requestCounter); 1353 request.set_keep_alive_time(TEST_SESSION_TIMEOUT_MS); 1354 auto status = service_->KeepSession(nullptr, &request, &response); 1355 EXPECT_NE(status.error_code(), grpc::StatusCode::OK); 1356 } 1357 1358 /** 1359 * @tc.name: server 1360 * @tc.desc: start service. 1361 * @tc.type: FUNC 1362 */ 1363 HWTEST_F(ProfilerServiceTest, StartService, TestSize.Level1) 1364 { 1365 auto service = service_; 1366 EXPECT_NE(service, nullptr); 1367 EXPECT_FALSE(service->StartService("")); 1368 std::string listenUri = std::string("0.0.0.0:") + std::to_string(COMMON::GetServicePort()); 1369 EXPECT_TRUE(service->StartService(listenUri)); 1370 std::thread waiterThread(&ProfilerService::WaitServiceDone, service.get()); 1371 sleep(1); 1372 service->StopService(); 1373 waiterThread.join(); 1374 } 1375 1376 /** 1377 * @tc.name: server 1378 * @tc.desc: MergeStandaloneFile test. 1379 * @tc.type: FUNC 1380 */ 1381 HWTEST_F(ProfilerServiceTest, MergeStandaloneFile, TestSize.Level1) 1382 { 1383 std::string testPath = "testFile.bin"; 1384 std::string targetPath = "target.trace.bin"; 1385 auto writer = std::make_shared<TraceFileWriter>(testPath, false, 0, 0); 1386 auto writerTarget = std::make_shared<TraceFileWriter>(targetPath, false, 0, 0); 1387 EXPECT_NE(writer, nullptr); 1388 EXPECT_NE(writerTarget, nullptr); 1389 std::string testData = "this is a test case!this is a test case!"; 1390 std::string traceData = "test"; 1391 EXPECT_EQ(writer->Write(testData.data(), testData.size()), sizeof(uint32_t) + testData.size()); 1392 EXPECT_EQ(writerTarget->Write(traceData.data(), traceData.size()), sizeof(uint32_t) + traceData.size()); 1393 std::string pluginName = "destroy_session_standalone_test"; 1394 service_->MergeStandaloneFile(targetPath, pluginName, testPath, "1.02"); 1395 service_->MergeStandaloneFile(targetPath, "", testPath, "1.02"); 1396 service_->MergeStandaloneFile(targetPath, pluginName, "", "1.02"); 1397 service_->MergeStandaloneFile("", pluginName, testPath, "1.02"); 1398 service_->MergeStandaloneFile(targetPath, "hiperf-plugin", testPath, "1.02"); 1399 } 1400 1401 } // namespace 1402