• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <dlfcn.h>
17 #include <fcntl.h>
18 #include <grpcpp/grpcpp.h>
19 #include <grpcpp/health_check_service_interface.h>
20 #include <gtest/gtest.h>
21 #include <thread>
22 #include <ifaddrs.h>
23 #include <arpa/inet.h>
24 #include <vector>
25 
26 #include "common_types.pb.h"
27 #include "cpu_plugin_config.pb.h"
28 #include "cpu_plugin_result.pb.h"
29 #include "diskio_plugin_result.pb.h"
30 #include "hilog_plugin_config.pb.h"
31 #include "hilog_plugin_result.pb.h"
32 #include "logging.h"
33 #include "memory_plugin_common.pb.h"
34 #include "memory_plugin_config.pb.h"
35 #include "memory_plugin_result.pb.h"
36 #include "network_plugin_config.pb.h"
37 #include "network_plugin_result.pb.h"
38 #include "plugin_module_api.h"
39 #include "plugin_service_types.pb.h"
40 #include "process_plugin_config.pb.h"
41 #include "process_plugin_result.pb.h"
42 #include "profiler_service.pb.h"
43 #include "profiler_service.grpc.pb.h"
44 
45 #pragma clang optimize off
46 
47 using namespace testing::ext;
48 
49 namespace {
50 const std::string DEFAULT_HIPROFILERD_PATH("/system/bin/hiprofilerd");
51 const std::string DEFAULT_HIPROFILER_PLUGINS_PATH("/system/bin/hiprofiler_plugins");
52 const std::string WRITE_FILE = "/data/local/tmp/diskio_write_test.txt";
53 const std::string HILOG_RESULT_FILE = "/data/local/tmp/hilog.txt";
54 constexpr int ROUND_COUNT = 50;
55 constexpr int LOOP_COUNT = 10;
56 constexpr int SAMPLE_INTERVAL = 40;
57 constexpr int TIMEOUT_US = 3 * 1000 * 1000;
58 constexpr int FETCHDATA_TIMEOUT_US = 20 * 1000 * 1000;
59 constexpr int KEEP_SESSION_TIMEOUT_MS = 5 * 1000;
60 constexpr int KEEP_SESSION_TIME_US = KEEP_SESSION_TIMEOUT_MS * 1000;
61 constexpr int KEEP_SESSION_SLEEP_US = 3 * 1000 * 1000;
62 constexpr int ACTIVE_PROCESS_SLEEP_US = 10000;
63 const size_t MB_PER_BYTE = 0x100000;
64 constexpr int BLOCK_LEN = 100 * 1024;
65 constexpr int WRITE_KB = 100;
66 constexpr int DEFAULT_PAGES = 10;
67 constexpr int MEMINFO_SIZE = 2;
68 
69 class Timer {
70 public:
71     using Clock = std::chrono::steady_clock;
72     using TimePoint = Clock::time_point;
73 
Timer()74     Timer() : startTime_(Now()) {}
75 
~Timer()76     ~Timer() {}
77 
ElapsedUs()78     long ElapsedUs()
79     {
80         auto currentTime = Now();
81         return std::chrono::duration_cast<std::chrono::microseconds>(currentTime - startTime_).count();
82     }
83 
Reset()84     void Reset()
85     {
86         startTime_ = Now();
87     }
88 
89 protected:
Now()90     TimePoint Now()
91     {
92         return Clock::now();
93     }
94 
95 private:
96     TimePoint startTime_;
97 };
98 
99 class ProfilerServicePerformanceTest : public ::testing::Test {
100 protected:
SetUpTestCase()101     static void SetUpTestCase() {}
TearDownTestCase()102     static void TearDownTestCase() {}
103 
SetUp()104     void SetUp() override
105     {
106         profilerStub_ = GetProfilerServiceStub();
107     }
108 
StartServerStub(std::string name)109     void StartServerStub(std::string name)
110     {
111         if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
112             return;
113         }
114         int processNum = fork();
115         if (processNum == 0) {
116             if (DEFAULT_HIPROFILERD_PATH == name) {
117                 // start running hiprofilerd
118                 execl(name.c_str(), nullptr, nullptr);
119             } else if (DEFAULT_HIPROFILER_PLUGINS_PATH == name) {
120                 // start running hiprofiler_plugins
121                 execl(name.c_str(), "/data/local/tmp/", nullptr);
122             }
123             _exit(1);
124         } else if (DEFAULT_HIPROFILERD_PATH == name) {
125             hiprofilerdPid_ = processNum;
126         } else if (DEFAULT_HIPROFILER_PLUGINS_PATH == name) {
127             hiprofilerPluginsPid_ = processNum;
128         }
129     }
StartDependServerStub()130     void StartDependServerStub()
131     {
132 #ifdef COVERAGE_TEST
133         const int coverageSleepTime = 5; // sleep 5s
134         StartServerStub(DEFAULT_HIPROFILERD_PATH);
135         sleep(coverageSleepTime);        // UT 覆盖率版本等待5s
136         StartServerStub(DEFAULT_HIPROFILER_PLUGINS_PATH);
137         sleep(coverageSleepTime);
138 #else
139         StartServerStub(DEFAULT_HIPROFILERD_PATH);
140         sleep(1); // 睡眠1s确保hiprofilerd进程启动之后再启动hiprofiler_plugins进程
141         StartServerStub(DEFAULT_HIPROFILER_PLUGINS_PATH);
142         sleep(1); // 睡眠1s确保hiprofiler_plugins进程可以监控到插件
143 #endif
144     }
145 
StopProcessStub(int processNum)146     void StopProcessStub(int processNum)
147     {
148         std::string stopCmd = "kill " + std::to_string(processNum);
149         std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose);
150     }
151 
StopDependServerStub()152     void StopDependServerStub()
153     {
154         std::string stopCmd = "killall " + DEFAULT_HIPROFILER_PLUGINS_PATH + " " + DEFAULT_HIPROFILERD_PATH;
155         std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose);
156     }
157 
GetProfilerServiceStub()158     std::unique_ptr<IProfilerService::Stub> GetProfilerServiceStub()
159     {
160         auto grpcChannel = grpc::CreateChannel("127.0.0.1:50051", grpc::InsecureChannelCredentials());
161         if (grpcChannel == nullptr) {
162             PROFILER_LOG_INFO(LOG_CORE, "Create GRPC channel failed!");
163             return nullptr;
164         }
165         return IProfilerService::NewStub(grpcChannel);
166     }
167 
SetMemPluginConfig(CreateSessionRequest & createRequest,int requstMemoryPid,bool isFirstConfig)168     void SetMemPluginConfig(CreateSessionRequest& createRequest, int requstMemoryPid, bool isFirstConfig)
169     {
170         ProfilerPluginConfig* ppc = createRequest.add_plugin_configs();
171         ppc->set_name("memory-plugin");
172         ppc->set_sample_interval(SAMPLE_INTERVAL);
173         MemoryConfig mc;
174         mc.set_report_process_mem_info(true);
175         mc.set_report_app_mem_info(isFirstConfig);
176         mc.set_report_sysmem_mem_info(true);
177         mc.add_sys_meminfo_counters(SysMeminfoType::PMEM_MEM_TOTAL);
178         mc.add_sys_meminfo_counters(SysMeminfoType::PMEM_MEM_FREE);
179         mc.set_report_sysmem_vmem_info(true);
180         mc.add_sys_vmeminfo_counters(SysVMeminfoType::VMEMINFO_NR_FREE_PAGES);
181         mc.add_sys_vmeminfo_counters(SysVMeminfoType::VMEMINFO_NR_ALLOC_BATCH);
182         mc.add_pid(idlePid_);
183         if (isFirstConfig) {
184             mc.add_pid(requstMemoryPid);
185         }
186         std::vector<uint8_t> configData(mc.ByteSizeLong());
187         mc.SerializeToArray(configData.data(), configData.size());
188         ppc->set_config_data((const void*)configData.data(), configData.size());
189         auto sessionConfig = createRequest.mutable_session_config();
190         ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers();
191         bfs->set_pages(DEFAULT_PAGES);
192         bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE);
193         sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE);
194     }
195 
SetCpuPluginConfig(CreateSessionRequest & createRequest,int pid)196     void SetCpuPluginConfig(CreateSessionRequest& createRequest, int pid)
197     {
198         ProfilerPluginConfig* ppc = createRequest.add_plugin_configs();
199         ppc->set_name("cpu-plugin");
200         ppc->set_sample_interval(SAMPLE_INTERVAL);
201         CpuConfig cc;
202         cc.set_pid(pid);
203         std::vector<uint8_t> configData(cc.ByteSizeLong());
204         cc.SerializeToArray(configData.data(), configData.size());
205         ppc->set_config_data((const void*)configData.data(), configData.size());
206         auto sessionConfig = createRequest.mutable_session_config();
207         ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers();
208         bfs->set_pages(DEFAULT_PAGES);
209         bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE);
210         sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE);
211     }
212 
SetDiskioPluginConfig(CreateSessionRequest & createRequest)213     void SetDiskioPluginConfig(CreateSessionRequest& createRequest)
214     {
215         ProfilerPluginConfig* ppc = createRequest.add_plugin_configs();
216         ppc->set_name("diskio-plugin");
217         ppc->set_sample_interval(SAMPLE_INTERVAL);
218         auto sessionConfig = createRequest.mutable_session_config();
219         ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers();
220         bfs->set_pages(DEFAULT_PAGES);
221         bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE);
222         sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE);
223     }
224 
SetProcessPluginConfig(CreateSessionRequest & createRequest)225     void SetProcessPluginConfig(CreateSessionRequest& createRequest)
226     {
227         ProfilerPluginConfig* ppc = createRequest.add_plugin_configs();
228         ppc->set_name("process-plugin");
229         ppc->set_sample_interval(SAMPLE_INTERVAL);
230         ProcessConfig pc;
231         pc.set_report_process_tree(true);
232         std::vector<uint8_t> configData(pc.ByteSizeLong());
233         pc.SerializeToArray(configData.data(), configData.size());
234         ppc->set_config_data((const void*)configData.data(), configData.size());
235         auto sessionConfig = createRequest.mutable_session_config();
236         ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers();
237         bfs->set_pages(DEFAULT_PAGES);
238         bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE);
239         sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE);
240     }
241 
SetHilogPluginConfig(CreateSessionRequest & createRequest,bool isFirstConfig)242     void SetHilogPluginConfig(CreateSessionRequest& createRequest, bool isFirstConfig)
243     {
244         ProfilerPluginConfig* ppc = createRequest.add_plugin_configs();
245         ppc->set_name("hilog-plugin");
246         ppc->set_sample_interval(SAMPLE_INTERVAL);
247         HilogConfig hc;
248         hc.set_need_record(true);
249         hc.set_need_clear(true);
250         std::vector<uint8_t> configData(hc.ByteSizeLong());
251         hc.SerializeToArray(configData.data(), configData.size());
252         ppc->set_config_data((const void*)configData.data(), configData.size());
253         auto sessionConfig = createRequest.mutable_session_config();
254         ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers();
255         bfs->set_pages(DEFAULT_PAGES);
256         bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE);
257         if (isFirstConfig) {
258             sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE);
259         } else {
260             sessionConfig->set_session_mode(ProfilerSessionConfig::OFFLINE);
261             sessionConfig->set_result_file(HILOG_RESULT_FILE);
262         }
263     }
264 
SetNetworkPluginConfig(CreateSessionRequest & createRequest)265     void SetNetworkPluginConfig(CreateSessionRequest& createRequest)
266     {
267         ProfilerPluginConfig* ppc = createRequest.add_plugin_configs();
268         ppc->set_name("network-plugin");
269         ppc->set_sample_interval(SAMPLE_INTERVAL);
270         NetworkConfig nc;
271         nc.add_pid(1);
272         std::vector<uint8_t> configData(nc.ByteSizeLong());
273         nc.SerializeToArray(configData.data(), configData.size());
274         ppc->set_config_data((const void*)configData.data(), configData.size());
275         auto sessionConfig = createRequest.mutable_session_config();
276         ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers();
277         bfs->set_pages(DEFAULT_PAGES);
278         bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE);
279         sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE);
280     }
281 
isExistSpecifyPlugin(GetCapabilitiesResponse & capResponse,std::string pluginName)282     bool isExistSpecifyPlugin(GetCapabilitiesResponse& capResponse, std::string pluginName)
283     {
284         bool isExistPlugin = false;
285         for (int i = 0; i < capResponse.capabilities().size(); i++) {
286             ProfilerPluginCapability ppc = capResponse.capabilities()[i];
287             if (ppc.name() == pluginName) {
288                 pluginVec_.push_back(ppc.name());
289                 isExistPlugin = true;
290             }
291         }
292         return isExistPlugin;
293     }
294 
GetPluginCapabilities(int count,GetCapabilitiesResponse & capResponse)295     bool GetPluginCapabilities(int count, GetCapabilitiesResponse& capResponse)
296     {
297         grpc::Status status;
298         for (int i = 0; i < count; i++) {
299             GetCapabilitiesRequest request;
300             request.set_request_id(0);
301             grpc::ClientContext context;
302             status = profilerStub_->GetCapabilities(&context, request, &capResponse);
303             if (!status.ok()) {
304                 return false;
305             }
306         }
307         return true;
308     }
309 
CreatePluginSession(uint32_t & sessionId,int pid,bool isFirstConfig=true)310     bool CreatePluginSession(uint32_t& sessionId, int pid, bool isFirstConfig = true)
311     {
312         CreateSessionResponse createResponse;
313         grpc::ClientContext createContext;
314         CreateSessionRequest createRequest = CreateSessionRequest();
315         createRequest.set_request_id(0);
316         for (int i = 0; i < pluginVec_.size(); i++) {
317             if (pluginVec_[i] == "memory-plugin") {
318                 SetMemPluginConfig(createRequest, pid, isFirstConfig);
319             } else if (pluginVec_[i] == "cpu-plugin") {
320                 SetCpuPluginConfig(createRequest, pid);
321             } else if (pluginVec_[i] == "diskio-plugin") {
322                 SetDiskioPluginConfig(createRequest);
323             } else if (pluginVec_[i] == "process-plugin") {
324                 SetProcessPluginConfig(createRequest);
325             } else if (pluginVec_[i] == "hilog-plugin") {
326                 SetHilogPluginConfig(createRequest, isFirstConfig);
327             } else if (pluginVec_[i] == "network-plugin") {
328                 SetNetworkPluginConfig(createRequest);
329             }
330         }
331         grpc::Status createStatus = profilerStub_->CreateSession(&createContext, createRequest, &createResponse);
332         sessionId = createResponse.session_id();
333         return createStatus.ok();
334     }
335 
KeepPluginSession(int count,uint32_t sessionId)336     bool KeepPluginSession(int count, uint32_t sessionId)
337     {
338         grpc::Status status;
339         for (int i = 0; i < count; i++) {
340             KeepSessionRequest request;
341             request.set_request_id(0);
342             request.set_session_id(sessionId);
343             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
344             grpc::ClientContext context;
345             KeepSessionResponse ksResponse;
346             status = profilerStub_->KeepSession(&context, request, &ksResponse);
347             if (!status.ok()) {
348                 return false;
349             }
350         }
351         return true;
352     }
353 
StartPluginSession(uint32_t sessionId)354     bool StartPluginSession(uint32_t sessionId)
355     {
356         StartSessionResponse startResponse;
357         grpc::ClientContext startContext;
358         StartSessionRequest startRequest = StartSessionRequest();
359         startRequest.set_request_id(0);
360         startRequest.set_session_id(sessionId);
361         grpc::Status startStatus = profilerStub_->StartSession(&startContext, startRequest, &startResponse);
362         return startStatus.ok();
363     }
364 
StopPluginSession(uint32_t sessionId)365     bool StopPluginSession(uint32_t sessionId)
366     {
367         StopSessionResponse stopResponse;
368         grpc::ClientContext stopContext;
369         StopSessionRequest stopRequest = StopSessionRequest();
370         stopRequest.set_request_id(0);
371         stopRequest.set_session_id(sessionId);
372         grpc::Status stopStatus = profilerStub_->StopSession(&stopContext, stopRequest, &stopResponse);
373         return stopStatus.ok();
374     }
375 
DestroyPluginSession(uint32_t sessionId)376     bool DestroyPluginSession(uint32_t sessionId)
377     {
378         DestroySessionResponse destroyResponse;
379         grpc::ClientContext destroyContext;
380         DestroySessionRequest destroyRequest = DestroySessionRequest();
381         destroyRequest.set_request_id(0);
382         destroyRequest.set_session_id(sessionId);
383         grpc::Status destroyStatus = profilerStub_->DestroySession(&destroyContext, destroyRequest, &destroyResponse);
384         return destroyStatus.ok();
385     }
386 
StartActiveProcess(int & cpuActivePid)387     bool StartActiveProcess(int& cpuActivePid)
388     {
389         if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
390             return false;
391         }
392         int processNum = fork();
393         if (processNum == 0) {
394             // consume cpu
395             CpuConfig protoConfig;
396             void* handle = dlopen("libcpudataplugin.z.so", RTLD_LAZY);
397             if (handle == nullptr) {
398                 const int bufSize = 256;
399                 char buf[bufSize] = { 0 };
400                 strerror_r(errno, buf, bufSize);
401                 PROFILER_LOG_ERROR(LOG_CORE, "test:dlopen err, errno(%d:%s)", errno, buf);
402                 return false;
403             }
404 
405             PluginModuleStruct* cpuPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
406             if (cpuPlugin == nullptr) {
407                 PROFILER_LOG_ERROR(LOG_CORE, "test: cpuPlugin is null");
408                 return false;
409             }
410 
411             // Serialize config
412             protoConfig.set_pid(getpid());
413             int configLength = protoConfig.ByteSizeLong();
414             std::vector<uint8_t> configBuffer(configLength);
415             protoConfig.SerializeToArray(configBuffer.data(), configLength);
416 
417             // run plugin
418             std::vector<uint8_t> dataBuffer(cpuPlugin->resultBufferSizeHint);
419             cpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength);
420 
421             // 循环上报数据消耗cpu
422             while (1) {
423                 int len = cpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(),
424                     cpuPlugin->resultBufferSizeHint);
425                 if (len > 0) {
426                     CpuData cpuData;
427                     cpuData.ParseFromArray(dataBuffer.data(), len);
428                 }
429                 usleep(ACTIVE_PROCESS_SLEEP_US);
430             }
431 
432             cpuPlugin->callbacks->onPluginSessionStop();
433         } else {
434             cpuActivePid = processNum;
435         }
436         return true;
437     }
438 
StartIdleProcess()439     void StartIdleProcess()
440     {
441         if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
442             return;
443         }
444         int processNum = fork();
445         if (processNum == 0) {
446             // not to do
447             while (1) {
448                 sleep(1);
449             }
450         } else {
451             idlePid_ = processNum;
452         }
453     }
454 
StartRequestMemoryProcess(int & requestMemoryPid)455     void StartRequestMemoryProcess(int& requestMemoryPid)
456     {
457         if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
458             return;
459         }
460         int processNum = fork();
461         if (processNum == 0) {
462             auto requestBuff = std::make_unique<char[]>(MB_PER_BYTE);
463             if (requestBuff == nullptr) {
464                 const int bufSize = 256;
465                 char buf[bufSize] = { 0 };
466                 strerror_r(errno, buf, bufSize);
467                 PROFILER_LOG_ERROR(LOG_CORE, "StartRequestMemoryProcess: malloc %zu fail, errno(%d:%s)",
468                     MB_PER_BYTE, errno, buf);
469             }
470             while (1) {
471                 sleep(1);
472             }
473         } else {
474             requestMemoryPid = processNum;
475         }
476     }
477 
IoTest()478     void IoTest()
479     {
480         // 一次累加16B,直至100KB
481         std::string str = "";
482         while (str.length() < BLOCK_LEN) {
483             str += "this is IO test.";
484         }
485 
486         // 写100K数据
487         FILE* writeFp = fopen(WRITE_FILE.c_str(), "w");
488         if (writeFp == nullptr) {
489             PROFILER_LOG_ERROR(LOG_CORE, "fopen() error");
490             return;
491         }
492         size_t len = fwrite(const_cast<char*>(str.c_str()), 1, BLOCK_LEN, writeFp);
493         if (len < 0) {
494             PROFILER_LOG_ERROR(LOG_CORE, "fwrite() error");
495             if (fclose(writeFp) != 0) {
496                 PROFILER_LOG_ERROR(LOG_CORE, "fclose() error");
497             }
498             return;
499         }
500         int ret = fflush(writeFp);
501         if (ret == EOF) {
502             PROFILER_LOG_ERROR(LOG_CORE, "fflush() error");
503             if (fclose(writeFp) != 0) {
504                 PROFILER_LOG_ERROR(LOG_CORE, "fclose() error");
505             }
506             return;
507         }
508         fsync(fileno(writeFp));
509         ret = fclose(writeFp);
510         if (ret != 0) {
511             PROFILER_LOG_ERROR(LOG_CORE, "fclose() error");
512             return;
513         }
514 
515         // delete file
516         std::string command = "rm " + WRITE_FILE;
517         system(command.c_str());
518     }
StartTestDiskioProcess(int & diskioPid)519     void StartTestDiskioProcess(int& diskioPid)
520     {
521         if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
522             return;
523         }
524         int processNum = fork();
525         if (processNum == 0) {
526             IoTest();
527             while (1) {
528                 sleep(1);
529             }
530         } else {
531             diskioPid = processNum;
532         }
533     }
534 
535 private:
536     std::unique_ptr<IProfilerService::Stub> profilerStub_;
537     std::vector<std::string> pluginVec_;
538     int hiprofilerdPid_ = -1;
539     int hiprofilerPluginsPid_ = -1;
540     pid_t idlePid_ = -1;
541 };
542 
543 /**
544  * @tc.name: server
545  * @tc.desc: RPC interface performance for memory_plugin normal test.
546  * @tc.type: FUNC
547  */
548 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0010, Function | MediumTest | Level1)
549 {
550     StopDependServerStub();
551     ASSERT_TRUE(profilerStub_ != nullptr);
552     int requestMemoryPid = -1;
553     StartIdleProcess();
554     StartRequestMemoryProcess(requestMemoryPid);
555 
556     StartDependServerStub();
557     Timer timer = {};
558     GetCapabilitiesResponse capResponse;
559     EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse));
560     auto timeCost = timer.ElapsedUs();
561     printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost);
562     EXPECT_LE(timeCost, TIMEOUT_US);
563 
564     ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "memory-plugin"));
565 
566     timer.Reset();
567     uint32_t sessionId1;
568     EXPECT_TRUE(CreatePluginSession(sessionId1, requestMemoryPid));
569     timeCost = timer.ElapsedUs();
570     printf("CreateSession cost %ldus.\n", timeCost);
571     EXPECT_LE(timeCost, TIMEOUT_US);
572 
573     // KeepSession
574     timer.Reset();
575     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1));
576     timeCost = timer.ElapsedUs();
577     printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost);
578     EXPECT_LE(timeCost, TIMEOUT_US);
579 
580     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
581     bool sendHeart = true;
582     int loopCount = 0;
__anon72c0da700202() 583     std::thread keepSessionThread1([&]() {
584         while (sendHeart && loopCount < LOOP_COUNT) {
585             ++loopCount;
586             KeepSessionRequest request;
587             request.set_request_id(0);
588             request.set_session_id(sessionId1);
589             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
590             grpc::ClientContext context;
591             KeepSessionResponse ksResponse;
592             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
593             if (sendHeart) {
594                 EXPECT_TRUE(status.ok());
595             }
596             usleep(KEEP_SESSION_SLEEP_US);
597         }
598     });
599 
600     // StartSession
601     timer.Reset();
602     EXPECT_TRUE(StartPluginSession(sessionId1));
603     timeCost = timer.ElapsedUs();
604     printf("StartSession cost %ldus.\n", timeCost);
605     EXPECT_LE(timeCost, TIMEOUT_US);
606 
607     // FetchData
608     timer.Reset();
609     std::vector<CpuData> cpuDataVec1;
610     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr;
611     grpc::ClientContext fetchContext;
612     FetchDataRequest fetchRequest = FetchDataRequest();
613     fetchRequest.set_request_id(0);
614     fetchRequest.set_session_id(sessionId1);
615     fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest);
616     ASSERT_TRUE(fetchResponse != nullptr);
__anon72c0da700302() 617     std::thread fetchDataThread1([&]() {
618         int ict = 0;
619         while (1) {
620             FetchDataResponse rp;
621             fetchResponse.get()->Read(&rp);
622             for (int i = 0; i < rp.plugin_data().size(); i++) {
623                 ProfilerPluginData pd = rp.plugin_data(i);
624                 if (ict == ROUND_COUNT) {
625                     return;
626                 }
627 
628                 ASSERT_STREQ(pd.name().c_str(), "memory-plugin");
629 
630                 MemoryData md;
631                 auto& data = pd.data();
632                 md.ParseFromArray(data.data(), data.size());
633                 EXPECT_LE(md.processesinfo(0).vm_size_kb(), md.processesinfo(1).vm_size_kb());
634 
635                 EXPECT_GT(md.processesinfo(0).pid(), 0);
636                 EXPECT_GT(md.processesinfo(1).pid(), 0);
637                 EXPECT_STRNE(md.processesinfo(0).name().c_str(), "");
638                 EXPECT_STRNE(md.processesinfo(1).name().c_str(), "");
639                 EXPECT_GE(md.processesinfo(0).vm_size_kb(), 0);
640                 EXPECT_GE(md.processesinfo(1).vm_size_kb(), 0);
641                 EXPECT_GE(md.processesinfo(0).vm_rss_kb(), 0);
642                 EXPECT_GE(md.processesinfo(1).vm_rss_kb(), 0);
643                 EXPECT_GE(md.processesinfo(0).rss_anon_kb(), 0);
644                 EXPECT_GE(md.processesinfo(1).rss_anon_kb(), 0);
645                 EXPECT_GE(md.processesinfo(0).rss_file_kb(), 0);
646                 EXPECT_GE(md.processesinfo(1).rss_file_kb(), 0);
647                 EXPECT_GE(md.processesinfo(0).rss_shmem_kb(), 0);
648                 EXPECT_GE(md.processesinfo(1).rss_shmem_kb(), 0);
649                 EXPECT_GE(md.processesinfo(0).vm_swap_kb(), 0);
650                 EXPECT_GE(md.processesinfo(1).vm_swap_kb(), 0);
651                 EXPECT_GE(md.processesinfo(0).vm_locked_kb(), 0);
652                 EXPECT_GE(md.processesinfo(1).vm_locked_kb(), 0);
653                 EXPECT_GE(md.processesinfo(0).vm_hwm_kb(), 0);
654                 EXPECT_GE(md.processesinfo(1).vm_hwm_kb(), 0);
655 
656                 EXPECT_TRUE(md.processesinfo(0).has_memsummary());
657                 EXPECT_GE(md.processesinfo(0).memsummary().java_heap(), 0);
658                 EXPECT_GE(md.processesinfo(1).memsummary().java_heap(), 0);
659                 EXPECT_GE(md.processesinfo(0).memsummary().native_heap(), 0);
660                 EXPECT_GE(md.processesinfo(1).memsummary().native_heap(), 0);
661                 EXPECT_GE(md.processesinfo(0).memsummary().code(), 0);
662                 EXPECT_GE(md.processesinfo(1).memsummary().code(), 0);
663                 EXPECT_GE(md.processesinfo(0).memsummary().stack(), 0);
664                 EXPECT_GE(md.processesinfo(1).memsummary().stack(), 0);
665                 EXPECT_GE(md.processesinfo(0).memsummary().graphics(), 0);
666                 EXPECT_GE(md.processesinfo(1).memsummary().graphics(), 0);
667                 EXPECT_GE(md.processesinfo(0).memsummary().private_other(), 0);
668                 EXPECT_GE(md.processesinfo(1).memsummary().private_other(), 0);
669                 EXPECT_GE(md.processesinfo(0).memsummary().system(), 0);
670                 EXPECT_GE(md.processesinfo(1).memsummary().system(), 0);
671 
672                 EXPECT_EQ(md.meminfo().size(), MEMINFO_SIZE);
673                 EXPECT_EQ(md.meminfo(0).key(), SysMeminfoType::PMEM_MEM_TOTAL);
674                 EXPECT_GE(md.meminfo(0).value(), 0);
675                 EXPECT_EQ(md.meminfo(1).key(), SysMeminfoType::PMEM_MEM_FREE);
676                 EXPECT_GE(md.meminfo(1).value(), 0);
677 
678                 EXPECT_EQ(md.vmeminfo().size(), 1);
679                 EXPECT_EQ(md.vmeminfo(0).key(), SysVMeminfoType::VMEMINFO_NR_FREE_PAGES);
680                 EXPECT_GE(md.vmeminfo(0).value(), 0);
681 
682                 ict += 1;
683             }
684         }
685     });
686     fetchDataThread1.join();
687     timeCost = timer.ElapsedUs();
688     printf("FetchData cost %ldus.\n", timeCost);
689     EXPECT_LE(timeCost, FETCHDATA_TIMEOUT_US);
690 
691     // StopSession
692     timer.Reset();
693     EXPECT_TRUE(StopPluginSession(sessionId1));
694     timeCost = timer.ElapsedUs();
695     printf("StopSession cost %ldus.\n", timeCost);
696     EXPECT_LE(timeCost, TIMEOUT_US);
697 
698     // DestroySession
699     timer.Reset();
700     EXPECT_TRUE(DestroyPluginSession(sessionId1));
701     timeCost = timer.ElapsedUs();
702     printf("DestroySession cost %ldus.\n", timeCost);
703     EXPECT_LE(timeCost, TIMEOUT_US);
704 
705     // 销毁会话之后停止心跳发送
706     sendHeart = false;
707     keepSessionThread1.join();
708 
709     // 创建不同的memory config
710     uint32_t sessionId2;
711     EXPECT_TRUE(CreatePluginSession(sessionId2, idlePid_, false));
712     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId2));
713 
714     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
715     bool sendHeart2 = true;
716     loopCount = 0;
__anon72c0da700402() 717     std::thread keepSessionThread2([&]() {
718         while (sendHeart2 && loopCount < LOOP_COUNT) {
719             ++loopCount;
720             KeepSessionRequest request;
721             request.set_request_id(0);
722             request.set_session_id(sessionId2);
723             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
724             grpc::ClientContext context;
725             KeepSessionResponse ksResponse;
726             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
727             if (sendHeart2) {
728                 EXPECT_TRUE(status.ok());
729             }
730             usleep(KEEP_SESSION_SLEEP_US);
731         }
732     });
733 
734     EXPECT_TRUE(StartPluginSession(sessionId2));
735 
736     // FetchData
737     std::vector<CpuData> cpuDataVec2;
738     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse2 = nullptr;
739     grpc::ClientContext fetchContext2;
740     FetchDataRequest fetchRequest2 = FetchDataRequest();
741     fetchRequest2.set_request_id(0);
742     fetchRequest2.set_session_id(sessionId2);
743     fetchResponse2 = profilerStub_->FetchData(&fetchContext2, fetchRequest2);
744     ASSERT_TRUE(fetchResponse2 != nullptr);
__anon72c0da700502() 745     std::thread fetchDataThread2([&]() {
746         int ict = 0;
747         while (1) {
748             FetchDataResponse rp;
749             fetchResponse2.get()->Read(&rp);
750             for (int i = 0; i < rp.plugin_data().size(); i++) {
751                 ProfilerPluginData pd = rp.plugin_data(i);
752                 if (ict == ROUND_COUNT) {
753                     return;
754                 }
755 
756                 ASSERT_STREQ(pd.name().c_str(), "memory-plugin");
757 
758                 MemoryData md;
759                 auto& data = pd.data();
760                 md.ParseFromArray(data.data(), data.size());
761                 EXPECT_FALSE(md.processesinfo(0).has_memsummary());
762 
763                 ict += 1;
764             }
765         }
766     });
767     fetchDataThread2.join();
768     EXPECT_TRUE(StopPluginSession(sessionId2));
769     EXPECT_TRUE(DestroyPluginSession(sessionId2));
770     // 销毁会话之后停止心跳发送
771     sendHeart2 = false;
772     keepSessionThread2.join();
773 
774     StopProcessStub(hiprofilerPluginsPid_);
775     StopProcessStub(hiprofilerdPid_);
776     StopProcessStub(idlePid_);
777     StopProcessStub(requestMemoryPid);
778 
779     pluginVec_.clear();
780 }
781 
782 /**
783  * @tc.name: server
784  * @tc.desc: RPC interface performance for cpu_plugin normal test.
785  * @tc.type: FUNC
786  */
787 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0020, Function | MediumTest | Level1)
788 {
789     StopDependServerStub();
790     ASSERT_TRUE(profilerStub_ != nullptr);
791     StartIdleProcess();
792 
793     StartDependServerStub();
794     Timer timer = {};
795     GetCapabilitiesResponse capResponse;
796     EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse));
797     auto timeCost = timer.ElapsedUs();
798     printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost);
799     EXPECT_LE(timeCost, TIMEOUT_US);
800 
801     ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "cpu-plugin"));
802 
803     timer.Reset();
804     uint32_t sessionId1;
805     EXPECT_TRUE(CreatePluginSession(sessionId1, idlePid_));
806     timeCost = timer.ElapsedUs();
807     printf("CreateSession cost %ldus.\n", timeCost);
808     EXPECT_LE(timeCost, TIMEOUT_US);
809 
810     // KeepSession
811     timer.Reset();
812     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1));
813     timeCost = timer.ElapsedUs();
814     printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost);
815     EXPECT_LE(timeCost, TIMEOUT_US);
816 
817     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
818     bool sendHeart = true;
819     int loopCount = 0;
__anon72c0da700602() 820     std::thread keepSessionThread1([&]() {
821         while (sendHeart && loopCount < LOOP_COUNT) {
822             ++loopCount;
823             KeepSessionRequest request;
824             request.set_request_id(0);
825             request.set_session_id(sessionId1);
826             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
827             grpc::ClientContext context;
828             KeepSessionResponse ksResponse;
829             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
830             if (sendHeart) {
831                 EXPECT_TRUE(status.ok());
832             }
833             usleep(KEEP_SESSION_SLEEP_US);
834         }
835     });
836 
837     // StartSession
838     timer.Reset();
839     EXPECT_TRUE(StartPluginSession(sessionId1));
840     timeCost = timer.ElapsedUs();
841     printf("StartSession cost %ldus.\n", timeCost);
842     EXPECT_LE(timeCost, TIMEOUT_US);
843 
844     // FetchData
845     timer.Reset();
846     std::vector<CpuData> cpuDataVec1;
847     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr;
848     grpc::ClientContext fetchContext;
849     FetchDataRequest fetchRequest = FetchDataRequest();
850     fetchRequest.set_request_id(0);
851     fetchRequest.set_session_id(sessionId1);
852     fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest);
853     ASSERT_TRUE(fetchResponse != nullptr);
__anon72c0da700702() 854     std::thread fetchDataThread1([&]() {
855         int ict = 0;
856         while (1) {
857             FetchDataResponse rp;
858             fetchResponse.get()->Read(&rp);
859             for (int i = 0; i < rp.plugin_data().size(); i++) {
860                 ProfilerPluginData pd = rp.plugin_data(i);
861                 if (ict == ROUND_COUNT) {
862                     return;
863                 }
864 
865                 ASSERT_STREQ(pd.name().c_str(), "cpu-plugin");
866 
867                 CpuData cd;
868                 auto& data = pd.data();
869                 cd.ParseFromArray(data.data(), data.size());
870                 cpuDataVec1.push_back(cd);
871 
872                 ict += 1;
873             }
874         }
875     });
876     fetchDataThread1.join();
877     timeCost = timer.ElapsedUs();
878     printf("FetchData cost %ldus.\n", timeCost);
879     EXPECT_LE(timeCost, FETCHDATA_TIMEOUT_US);
880 
881     // StopSession
882     timer.Reset();
883     EXPECT_TRUE(StopPluginSession(sessionId1));
884     timeCost = timer.ElapsedUs();
885     printf("StopSession cost %ldus.\n", timeCost);
886     EXPECT_LE(timeCost, TIMEOUT_US);
887 
888     // DestroySession
889     timer.Reset();
890     EXPECT_TRUE(DestroyPluginSession(sessionId1));
891     timeCost = timer.ElapsedUs();
892     printf("DestroySession cost %ldus.\n", timeCost);
893     EXPECT_LE(timeCost, TIMEOUT_US);
894 
895     // 销毁会话之后停止心跳发送
896     sendHeart = false;
897     keepSessionThread1.join();
898 
899     // 开启活跃进程获取数据
900     int cpuActivePid = -1;
901     StartActiveProcess(cpuActivePid);
902     //睡眠一秒确保子进程开始工作
903     sleep(1);
904     uint32_t sessionId2;
905     EXPECT_TRUE(CreatePluginSession(sessionId2, cpuActivePid));
906     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId2));
907 
908     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
909     bool sendHeart2 = true;
910     loopCount = 0;
__anon72c0da700802() 911     std::thread keepSessionThread2([&]() {
912         while (sendHeart2 && loopCount < LOOP_COUNT) {
913             ++loopCount;
914             KeepSessionRequest request;
915             request.set_request_id(0);
916             request.set_session_id(sessionId2);
917             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
918             grpc::ClientContext context;
919             KeepSessionResponse ksResponse;
920             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
921             if (sendHeart2) {
922                 EXPECT_TRUE(status.ok());
923             }
924             usleep(KEEP_SESSION_SLEEP_US);
925         }
926     });
927 
928     EXPECT_TRUE(StartPluginSession(sessionId2));
929 
930     // FetchData
931     std::vector<CpuData> cpuDataVec2;
932     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse2 = nullptr;
933     grpc::ClientContext fetchContext2;
934     FetchDataRequest fetchRequest2 = FetchDataRequest();
935     fetchRequest2.set_request_id(0);
936     fetchRequest2.set_session_id(sessionId2);
937     fetchResponse2 = profilerStub_->FetchData(&fetchContext2, fetchRequest2);
938     ASSERT_TRUE(fetchResponse2 != nullptr);
__anon72c0da700902() 939     std::thread fetchDataThread2([&]() {
940         int ict = 0;
941         while (1) {
942             FetchDataResponse rp;
943             fetchResponse2.get()->Read(&rp);
944             for (int i = 0; i < rp.plugin_data().size(); i++) {
945                 ProfilerPluginData pd = rp.plugin_data(i);
946                 if (ict == ROUND_COUNT) {
947                     return;
948                 }
949 
950                 ASSERT_STREQ(pd.name().c_str(), "cpu-plugin");
951 
952                 CpuData cd;
953                 auto& data = pd.data();
954                 cd.ParseFromArray(data.data(), data.size());
955                 cpuDataVec2.push_back(cd);
956 
957                 ict += 1;
958             }
959         }
960     });
961     fetchDataThread2.join();
962     EXPECT_TRUE(StopPluginSession(sessionId2));
963     EXPECT_TRUE(DestroyPluginSession(sessionId2));
964     // 销毁会话之后停止心跳发送
965     sendHeart2 = false;
966     keepSessionThread2.join();
967 
968     // 校验进程的cpu占用时间
969     for (int i = 0; i < cpuDataVec1.size() && i < cpuDataVec2.size(); i++) {
970         EXPECT_LE(cpuDataVec1[i].cpu_usage_info().process_cpu_time_ms(),
971             cpuDataVec2[i].cpu_usage_info().process_cpu_time_ms());
972 
973         EXPECT_GE(cpuDataVec1[i].cpu_usage_info().process_cpu_time_ms(), 0);
974         EXPECT_GE(cpuDataVec2[i].cpu_usage_info().process_cpu_time_ms(), 0);
975         EXPECT_GT(cpuDataVec1[i].cpu_usage_info().system_cpu_time_ms(), 0);
976         EXPECT_GT(cpuDataVec2[i].cpu_usage_info().system_cpu_time_ms(), 0);
977         EXPECT_GT(cpuDataVec1[i].cpu_usage_info().system_boot_time_ms(), 0);
978         EXPECT_GT(cpuDataVec2[i].cpu_usage_info().system_boot_time_ms(), 0);
979         EXPECT_GE(cpuDataVec1[i].cpu_usage_info().cores().size(), 1);
980         EXPECT_GE(cpuDataVec2[i].cpu_usage_info().cores().size(), 1);
981         EXPECT_GE(cpuDataVec1[i].thread_info().size(), 1);
982         EXPECT_GE(cpuDataVec2[i].thread_info().size(), 1);
983         EXPECT_GT(cpuDataVec1[i].thread_info(0).tid(), 0);
984         EXPECT_GT(cpuDataVec2[i].thread_info(0).tid(), 0);
985         EXPECT_STRNE(cpuDataVec1[i].thread_info(0).thread_name().c_str(), "");
986         EXPECT_STRNE(cpuDataVec2[i].thread_info(0).thread_name().c_str(), "");
987         EXPECT_LE(cpuDataVec1[i].thread_info(0).thread_state(), ThreadState::THREAD_WAITING);
988         EXPECT_LE(cpuDataVec2[i].thread_info(0).thread_state(), ThreadState::THREAD_WAITING);
989         EXPECT_GE(cpuDataVec1[i].thread_info(0).thread_cpu_time_ms(), 0);
990         EXPECT_GE(cpuDataVec2[i].thread_info(0).thread_cpu_time_ms(), 0);
991 
992         if (i > 0) {
993             EXPECT_EQ(cpuDataVec1[i].cpu_usage_info().prev_process_cpu_time_ms(),
994                 cpuDataVec1[i-1].cpu_usage_info().process_cpu_time_ms());
995             EXPECT_EQ(cpuDataVec2[i].cpu_usage_info().prev_process_cpu_time_ms(),
996                 cpuDataVec2[i-1].cpu_usage_info().process_cpu_time_ms());
997             EXPECT_EQ(cpuDataVec1[i].cpu_usage_info().prev_system_cpu_time_ms(),
998                 cpuDataVec1[i-1].cpu_usage_info().system_cpu_time_ms());
999             EXPECT_EQ(cpuDataVec2[i].cpu_usage_info().prev_system_cpu_time_ms(),
1000                 cpuDataVec2[i-1].cpu_usage_info().system_cpu_time_ms());
1001             EXPECT_EQ(cpuDataVec1[i].cpu_usage_info().prev_system_boot_time_ms(),
1002                 cpuDataVec1[i-1].cpu_usage_info().system_boot_time_ms());
1003             EXPECT_EQ(cpuDataVec2[i].cpu_usage_info().prev_system_boot_time_ms(),
1004                 cpuDataVec2[i-1].cpu_usage_info().system_boot_time_ms());
1005             EXPECT_EQ(cpuDataVec1[i].thread_info(0).prev_thread_cpu_time_ms(),
1006                 cpuDataVec1[i-1].thread_info(0).thread_cpu_time_ms());
1007             EXPECT_EQ(cpuDataVec2[i].thread_info(0).prev_thread_cpu_time_ms(),
1008                 cpuDataVec2[i-1].thread_info(0).thread_cpu_time_ms());
1009         }
1010     }
1011 
1012     StopProcessStub(hiprofilerPluginsPid_);
1013     StopProcessStub(hiprofilerdPid_);
1014     StopProcessStub(cpuActivePid);
1015     StopProcessStub(idlePid_);
1016 
1017     pluginVec_.clear();
1018 }
1019 
1020 /**
1021  * @tc.name: server
1022  * @tc.desc: RPC interface performance for diskio_plugin normal test.
1023  * @tc.type: FUNC
1024  */
1025 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0030, Function | MediumTest | Level1)
1026 {
1027     StopDependServerStub();
1028     ASSERT_TRUE(profilerStub_ != nullptr);
1029     StartDependServerStub();
1030     Timer timer = {};
1031     GetCapabilitiesResponse capResponse;
1032     EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse));
1033     auto timeCost = timer.ElapsedUs();
1034     printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost);
1035     EXPECT_LE(timeCost, TIMEOUT_US);
1036 
1037     ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "diskio-plugin"));
1038 
1039     timer.Reset();
1040     uint32_t sessionId1;
1041     EXPECT_TRUE(CreatePluginSession(sessionId1, 0));
1042     timeCost = timer.ElapsedUs();
1043     printf("CreateSession cost %ldus.\n", timeCost);
1044     EXPECT_LE(timeCost, TIMEOUT_US);
1045 
1046     // KeepSession
1047     timer.Reset();
1048     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1));
1049     timeCost = timer.ElapsedUs();
1050     printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost);
1051     EXPECT_LE(timeCost, TIMEOUT_US);
1052 
1053     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
1054     bool sendHeart = true;
1055     int loopCount = 0;
__anon72c0da700a02() 1056     std::thread keepSessionThread([&]() {
1057         while (sendHeart && loopCount < LOOP_COUNT) {
1058             ++loopCount;
1059             KeepSessionRequest request;
1060             request.set_request_id(0);
1061             request.set_session_id(sessionId1);
1062             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
1063             grpc::ClientContext context;
1064             KeepSessionResponse ksResponse;
1065             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
1066             if (sendHeart) {
1067                 EXPECT_TRUE(status.ok());
1068             }
1069             usleep(KEEP_SESSION_SLEEP_US);
1070         }
1071     });
1072 
1073     // StartSession
1074     timer.Reset();
1075     EXPECT_TRUE(StartPluginSession(sessionId1));
1076     timeCost = timer.ElapsedUs();
1077     printf("StartSession cost %ldus.\n", timeCost);
1078     EXPECT_LE(timeCost, TIMEOUT_US);
1079 
1080     // FetchData
1081     timer.Reset();
1082     std::vector<DiskioData> diskioDataVec1;
1083     std::vector<DiskioData> diskioDataVec2;
1084     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr;
1085     grpc::ClientContext fetchContext;
1086     FetchDataRequest fetchRequest = FetchDataRequest();
1087     fetchRequest.set_request_id(0);
1088     fetchRequest.set_session_id(sessionId1);
1089     fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest);
1090     ASSERT_TRUE(fetchResponse != nullptr);
1091     int count = ROUND_COUNT + ROUND_COUNT + 1;
1092     int diskioPid = -1;
__anon72c0da700b02() 1093     std::thread fetchDataThread([&]() {
1094         int ict = 0;
1095         while (1) {
1096             FetchDataResponse rp;
1097             fetchResponse.get()->Read(&rp);
1098             for (int i = 0; i < rp.plugin_data().size(); i++) {
1099                 ProfilerPluginData pd = rp.plugin_data(i);
1100                 if (ict == count) {
1101                     return;
1102                 }
1103 
1104                 if (ict == ROUND_COUNT) { // 向磁盘写入100k的数据
1105                     StartTestDiskioProcess(diskioPid);
1106                     sleep(1);
1107                     ict += 1;
1108                     break;
1109                 }
1110 
1111                 ASSERT_STREQ(pd.name().c_str(), "diskio-plugin");
1112 
1113                 DiskioData dd;
1114                 auto& data = pd.data();
1115                 dd.ParseFromArray(data.data(), data.size());
1116                 if (ict < ROUND_COUNT) {
1117                     diskioDataVec1.push_back(dd);
1118                 } else {
1119                     diskioDataVec2.push_back(dd);
1120                 }
1121 
1122                 ict += 1;
1123             }
1124         }
1125     });
1126     fetchDataThread.join();
1127     timeCost = timer.ElapsedUs();
1128     printf("FetchData cost %ldus.\n", timeCost);
1129     EXPECT_LE(timeCost, FETCHDATA_TIMEOUT_US);
1130 
1131     // StopSession
1132     timer.Reset();
1133     EXPECT_TRUE(StopPluginSession(sessionId1));
1134     timeCost = timer.ElapsedUs();
1135     printf("StopSession cost %ldus.\n", timeCost);
1136     EXPECT_LE(timeCost, TIMEOUT_US);
1137 
1138     // DestroySession
1139     timer.Reset();
1140     EXPECT_TRUE(DestroyPluginSession(sessionId1));
1141     timeCost = timer.ElapsedUs();
1142     printf("DestroySession cost %ldus.\n", timeCost);
1143     EXPECT_LE(timeCost, TIMEOUT_US);
1144     // 销毁会话之后停止心跳发送
1145     sendHeart = false;
1146     keepSessionThread.join();
1147 
1148     // 验证diskio上报的数据指标
1149     for (int i = 0; i < diskioDataVec1.size() && i < diskioDataVec2.size(); i++) {
1150         EXPECT_LE(diskioDataVec1[i].wr_sectors_kb() + WRITE_KB, diskioDataVec2[i].wr_sectors_kb());
1151         EXPECT_GT(diskioDataVec1[i].wr_sectors_kb(), 0);
1152         EXPECT_GT(diskioDataVec2[i].wr_sectors_kb(), 0);
1153         EXPECT_GT(diskioDataVec1[i].rd_sectors_kb(), 0);
1154         EXPECT_GT(diskioDataVec2[i].rd_sectors_kb(), 0);
1155     }
1156 
1157     StopProcessStub(hiprofilerPluginsPid_);
1158     StopProcessStub(hiprofilerdPid_);
1159     StopProcessStub(diskioPid);
1160 
1161     pluginVec_.clear();
1162 }
1163 
1164 /**
1165  * @tc.name: server
1166  * @tc.desc: RPC interface performance for process_plugin normal test.
1167  * @tc.type: FUNC
1168  */
1169 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0040, Function | MediumTest | Level1)
1170 {
1171     StopDependServerStub();
1172     ASSERT_TRUE(profilerStub_ != nullptr);
1173 
1174     StartDependServerStub();
1175     Timer timer = {};
1176     GetCapabilitiesResponse capResponse;
1177     EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse));
1178     auto timeCost = timer.ElapsedUs();
1179     printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost);
1180     EXPECT_LE(timeCost, TIMEOUT_US);
1181 
1182     ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "process-plugin"));
1183 
1184     timer.Reset();
1185     uint32_t sessionId1;
1186     EXPECT_TRUE(CreatePluginSession(sessionId1, 0));
1187     timeCost = timer.ElapsedUs();
1188     printf("CreateSession cost %ldus.\n", timeCost);
1189     EXPECT_LE(timeCost, TIMEOUT_US);
1190 
1191     // KeepSession
1192     timer.Reset();
1193     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1));
1194     timeCost = timer.ElapsedUs();
1195     printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost);
1196     EXPECT_LE(timeCost, TIMEOUT_US);
1197 
1198     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
1199     bool sendHeart = true;
1200     int loopCount = 0;
__anon72c0da700c02() 1201     std::thread keepSessionThread([&]() {
1202         while (sendHeart && loopCount < LOOP_COUNT) {
1203             ++loopCount;
1204             KeepSessionRequest request;
1205             request.set_request_id(0);
1206             request.set_session_id(sessionId1);
1207             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
1208             grpc::ClientContext context;
1209             KeepSessionResponse ksResponse;
1210             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
1211             if (sendHeart) {
1212                 EXPECT_TRUE(status.ok());
1213             }
1214             usleep(KEEP_SESSION_SLEEP_US);
1215         }
1216     });
1217 
1218     // StartSession
1219     timer.Reset();
1220     EXPECT_TRUE(StartPluginSession(sessionId1));
1221     timeCost = timer.ElapsedUs();
1222     printf("StartSession cost %ldus.\n", timeCost);
1223     EXPECT_LE(timeCost, TIMEOUT_US);
1224 
1225     // FetchData
1226     timer.Reset();
1227     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr;
1228     grpc::ClientContext fetchContext;
1229     FetchDataRequest fetchRequest = FetchDataRequest();
1230     fetchRequest.set_request_id(0);
1231     fetchRequest.set_session_id(sessionId1);
1232     fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest);
1233     ASSERT_TRUE(fetchResponse != nullptr);
1234     int hiprofilerdPidCount = 0;
1235     int hiprofilerPluginsPidCount = 0;
__anon72c0da700d02() 1236     std::thread fetchDataThread([&]() {
1237         int ict = 0;
1238         while (1) {
1239             FetchDataResponse rp;
1240             fetchResponse.get()->Read(&rp);
1241             for (int i = 0; i < rp.plugin_data().size(); i++) {
1242                 ProfilerPluginData pd = rp.plugin_data(i);
1243                 if (ict == ROUND_COUNT) {
1244                     return;
1245                 }
1246 
1247                 ASSERT_STREQ(pd.name().c_str(), "process-plugin");
1248 
1249                 ProcessData processData;
1250                 auto& data = pd.data();
1251                 processData.ParseFromArray(data.data(), data.size());
1252                 EXPECT_GT(static_cast<int>(processData.processesinfo().size()), 0);
1253                 for (int j = 0; j < processData.processesinfo().size(); j++) {
1254                     ProcessInfo info = processData.processesinfo(j);
1255                     if (info.pid() == hiprofilerdPid_) {
1256                         hiprofilerdPidCount++;
1257                     } else if (info.pid() == hiprofilerPluginsPid_) {
1258                         hiprofilerPluginsPidCount++;
1259                     }
1260                 }
1261 
1262                 ict += 1;
1263             }
1264         }
1265     });
1266     fetchDataThread.join();
1267     timeCost = timer.ElapsedUs();
1268     printf("FetchData cost %ldus.\n", timeCost);
1269     EXPECT_LE(timeCost, FETCHDATA_TIMEOUT_US);
1270     EXPECT_EQ(hiprofilerdPidCount, ROUND_COUNT);
1271     EXPECT_EQ(hiprofilerPluginsPidCount, ROUND_COUNT);
1272 
1273     // StopSession
1274     timer.Reset();
1275     EXPECT_TRUE(StopPluginSession(sessionId1));
1276     timeCost = timer.ElapsedUs();
1277     printf("StopSession cost %ldus.\n", timeCost);
1278     EXPECT_LE(timeCost, TIMEOUT_US);
1279 
1280     // DestroySession
1281     timer.Reset();
1282     EXPECT_TRUE(DestroyPluginSession(sessionId1));
1283     timeCost = timer.ElapsedUs();
1284     printf("DestroySession cost %ldus.\n", timeCost);
1285     EXPECT_LE(timeCost, TIMEOUT_US);
1286     // 销毁会话之后停止心跳发送
1287     sendHeart = false;
1288     keepSessionThread.join();
1289 
1290     StopProcessStub(hiprofilerPluginsPid_);
1291     StopProcessStub(hiprofilerdPid_);
1292 
1293     pluginVec_.clear();
1294 }
1295 
1296 /**
1297  * @tc.name: server
1298  * @tc.desc: RPC interface performance for hilog_plugin normal test.
1299  * @tc.type: FUNC
1300  */
1301 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0050, Function | MediumTest | Level1)
1302 {
1303     StopDependServerStub();
1304     ASSERT_TRUE(profilerStub_ != nullptr);
1305     StartDependServerStub();
1306     Timer timer = {};
1307     GetCapabilitiesResponse capResponse;
1308     EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse));
1309     auto timeCost = timer.ElapsedUs();
1310     printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost);
1311     EXPECT_LE(timeCost, TIMEOUT_US);
1312 
1313     ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "hilog-plugin"));
1314 
1315     timer.Reset();
1316     uint32_t sessionId1;
1317     EXPECT_TRUE(CreatePluginSession(sessionId1, 0, true));
1318     timeCost = timer.ElapsedUs();
1319     printf("CreateSession cost %ldus.\n", timeCost);
1320     EXPECT_LE(timeCost, TIMEOUT_US);
1321 
1322     // KeepSession
1323     timer.Reset();
1324     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1));
1325     timeCost = timer.ElapsedUs();
1326     printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost);
1327     EXPECT_LE(timeCost, TIMEOUT_US);
1328 
1329     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
1330     bool sendHeart = true;
1331     int loopCount = 0;
__anon72c0da700e02() 1332     std::thread keepSessionThread([&]() {
1333         while (sendHeart && loopCount < LOOP_COUNT) {
1334             ++loopCount;
1335             KeepSessionRequest request;
1336             request.set_request_id(0);
1337             request.set_session_id(sessionId1);
1338             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
1339             grpc::ClientContext context;
1340             KeepSessionResponse ksResponse;
1341             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
1342             if (sendHeart) {
1343                 EXPECT_TRUE(status.ok());
1344             }
1345             usleep(KEEP_SESSION_SLEEP_US);
1346         }
1347     });
1348 
1349     // StartSession
1350     timer.Reset();
1351     EXPECT_TRUE(StartPluginSession(sessionId1));
1352     timeCost = timer.ElapsedUs();
1353     printf("StartSession cost %ldus.\n", timeCost);
1354     EXPECT_LE(timeCost, TIMEOUT_US);
1355 
1356     // FetchData
1357     timer.Reset();
1358     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr;
1359     grpc::ClientContext fetchContext;
1360     FetchDataRequest fetchRequest = FetchDataRequest();
1361     fetchRequest.set_request_id(0);
1362     fetchRequest.set_session_id(sessionId1);
1363     fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest);
1364     ASSERT_TRUE(fetchResponse != nullptr);
1365     FetchDataResponse rp;
1366     fetchResponse.get()->Read(&rp);
1367     for (int i = 0; i < rp.plugin_data().size() && i < ROUND_COUNT; i++) {
1368         ProfilerPluginData pd = rp.plugin_data(i);
1369         HilogInfo hilogInfo;
1370         auto& data = pd.data();
1371         hilogInfo.ParseFromArray(data.data(), data.size());
1372         EXPECT_GT(static_cast<int>(hilogInfo.info().size()), 0);
1373         EXPECT_TRUE(hilogInfo.info(0).has_detail());
1374         if (hilogInfo.info(0).detail().pid() > 0) {
1375             EXPECT_STRNE(hilogInfo.info(0).context().c_str(), "");
1376         }
1377     }
1378     timeCost = timer.ElapsedUs();
1379     printf("FetchData cost %ldus.\n", timeCost);
1380 
1381     // StopSession
1382     timer.Reset();
1383     EXPECT_TRUE(StopPluginSession(sessionId1));
1384     timeCost = timer.ElapsedUs();
1385     printf("StopSession cost %ldus.\n", timeCost);
1386     EXPECT_LE(timeCost, TIMEOUT_US);
1387 
1388     // DestroySession
1389     timer.Reset();
1390     EXPECT_TRUE(DestroyPluginSession(sessionId1));
1391     timeCost = timer.ElapsedUs();
1392     printf("DestroySession cost %ldus.\n", timeCost);
1393     EXPECT_LE(timeCost, TIMEOUT_US);
1394     // 销毁会话之后停止心跳发送
1395     sendHeart = false;
1396     keepSessionThread.join();
1397 
1398     // 验证hilog离线模式是否生成hilogResultFile离线文件
1399     uint32_t sessionId2;
1400     EXPECT_TRUE(CreatePluginSession(sessionId2, 0, false));
1401     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId2));
1402 
1403     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
1404     bool sendHeart2 = true;
1405     loopCount = 0;
__anon72c0da700f02() 1406     std::thread keepSessionThread2([&]() {
1407         while (sendHeart2 && loopCount < LOOP_COUNT) {
1408             ++loopCount;
1409             KeepSessionRequest request;
1410             request.set_request_id(0);
1411             request.set_session_id(sessionId2);
1412             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
1413             grpc::ClientContext context;
1414             KeepSessionResponse ksResponse;
1415             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
1416             if (sendHeart2) {
1417                 EXPECT_TRUE(status.ok());
1418             }
1419             usleep(KEEP_SESSION_SLEEP_US);
1420         }
1421     });
1422 
1423     EXPECT_TRUE(StartPluginSession(sessionId2));
1424     usleep(TIMEOUT_US);
1425     EXPECT_EQ(access(HILOG_RESULT_FILE.c_str(), F_OK), 0);
1426     EXPECT_TRUE(StopPluginSession(sessionId2));
1427     EXPECT_TRUE(DestroyPluginSession(sessionId2));
1428     // 销毁会话之后停止心跳发送
1429     sendHeart2 = false;
1430     keepSessionThread2.join();
1431 
1432     StopProcessStub(hiprofilerPluginsPid_);
1433     StopProcessStub(hiprofilerdPid_);
1434 
1435     pluginVec_.clear();
1436 }
1437 
1438 /**
1439  * @tc.name: server
1440  * @tc.desc: RPC interface performance for network_plugin normal test.
1441  * @tc.type: FUNC
1442  */
1443 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0060, Function | MediumTest | Level1)
1444 {
1445     StopDependServerStub();
1446     ASSERT_TRUE(profilerStub_ != nullptr);
1447     StartDependServerStub();
1448     Timer timer = {};
1449     GetCapabilitiesResponse capResponse;
1450     EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse));
1451     auto timeCost = timer.ElapsedUs();
1452     printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost);
1453     EXPECT_LE(timeCost, TIMEOUT_US);
1454 
1455     ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "network-plugin"));
1456 
1457     timer.Reset();
1458     uint32_t sessionId1;
1459     EXPECT_TRUE(CreatePluginSession(sessionId1, 0));
1460     timeCost = timer.ElapsedUs();
1461     printf("CreateSession cost %ldus.\n", timeCost);
1462     EXPECT_LE(timeCost, TIMEOUT_US);
1463 
1464     // KeepSession
1465     timer.Reset();
1466     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1));
1467     timeCost = timer.ElapsedUs();
1468     printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost);
1469     EXPECT_LE(timeCost, TIMEOUT_US);
1470 
1471     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
1472     bool sendHeart = true;
1473     int loopCount = 0;
__anon72c0da701002() 1474     std::thread keepSessionThread([&]() {
1475         while (sendHeart && loopCount < LOOP_COUNT) {
1476             ++loopCount;
1477             KeepSessionRequest request;
1478             request.set_request_id(0);
1479             request.set_session_id(sessionId1);
1480             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
1481             grpc::ClientContext context;
1482             KeepSessionResponse ksResponse;
1483             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
1484             if (sendHeart) {
1485                 EXPECT_TRUE(status.ok());
1486             }
1487             usleep(KEEP_SESSION_SLEEP_US);
1488         }
1489     });
1490 
1491     // StartSession
1492     timer.Reset();
1493     EXPECT_TRUE(StartPluginSession(sessionId1));
1494     timeCost = timer.ElapsedUs();
1495     printf("StartSession cost %ldus.\n", timeCost);
1496     EXPECT_LE(timeCost, TIMEOUT_US);
1497 
1498     // FetchData network_plugin在开发板上没有节点,不上报数据
1499     timer.Reset();
1500     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr;
1501     grpc::ClientContext fetchContext;
1502     FetchDataRequest fetchRequest = FetchDataRequest();
1503     fetchRequest.set_request_id(0);
1504     fetchRequest.set_session_id(sessionId1);
1505     fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest);
1506     ASSERT_TRUE(fetchResponse != nullptr);
1507     bool recvData = true;
__anon72c0da701102() 1508     std::thread fetchDataThread([&]() {
1509         while (recvData) {
1510             FetchDataResponse rp;
1511             fetchResponse.get()->Read(&rp);
1512             EXPECT_EQ(rp.plugin_data().size(), 0);
1513         }
1514     });
1515     usleep(TIMEOUT_US);
1516 
1517     // StopSession
1518     timer.Reset();
1519     EXPECT_TRUE(StopPluginSession(sessionId1));
1520     timeCost = timer.ElapsedUs();
1521     printf("StopSession cost %ldus.\n", timeCost);
1522     EXPECT_LE(timeCost, TIMEOUT_US);
1523 
1524     recvData = false;
1525     fetchDataThread.join();
1526 
1527     // DestroySession
1528     timer.Reset();
1529     EXPECT_TRUE(DestroyPluginSession(sessionId1));
1530     timeCost = timer.ElapsedUs();
1531     printf("DestroySession cost %ldus.\n", timeCost);
1532     EXPECT_LE(timeCost, TIMEOUT_US);
1533     // 销毁会话之后停止心跳发送
1534     sendHeart = false;
1535     keepSessionThread.join();
1536 
1537     StopProcessStub(hiprofilerPluginsPid_);
1538     StopProcessStub(hiprofilerdPid_);
1539 
1540     pluginVec_.clear();
1541 }
1542 
1543 /**
1544  * @tc.name: server
1545  * @tc.desc: RPC interface performance unusual test.
1546  * @tc.type: FUNC
1547  */
1548 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0070, Function | MediumTest | Level1)
1549 {
1550     StopDependServerStub();
1551     ASSERT_TRUE(profilerStub_ != nullptr);
1552     uint32_t sessionId;
1553     GetCapabilitiesResponse capResponse1;
1554     EXPECT_FALSE(GetPluginCapabilities(1, capResponse1));
1555     EXPECT_FALSE(CreatePluginSession(sessionId, 1));
1556     EXPECT_FALSE(KeepPluginSession(1, sessionId));
1557     EXPECT_FALSE(StartPluginSession(sessionId));
1558     EXPECT_FALSE(StartPluginSession(sessionId));
1559     EXPECT_FALSE(DestroyPluginSession(sessionId));
1560     StartDependServerStub();
1561     Timer timer = {};
1562     GetCapabilitiesResponse capResponse;
1563     EXPECT_TRUE(GetPluginCapabilities(1, capResponse));
1564     auto timeCost = timer.ElapsedUs();
1565     printf("UnusualTest: GetCapabilities 1 time cost %ldus.\n", timeCost);
1566     EXPECT_LE(timeCost, TIMEOUT_US);
1567 
1568     ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "cpu-plugin"));
1569 
1570     timer.Reset();
1571     EXPECT_TRUE(CreatePluginSession(sessionId, 1));
1572     timeCost = timer.ElapsedUs();
1573     printf("UnusualTest: CreateSession cost %ldus.\n", timeCost);
1574     EXPECT_LE(timeCost, TIMEOUT_US);
1575 
1576     // KeepSession,5s超时心跳
1577     timer.Reset();
1578     EXPECT_TRUE(KeepPluginSession(1, sessionId));
1579     timeCost = timer.ElapsedUs();
1580     printf("UnusualTest: KeepSession 1 time cost %ldus.\n", timeCost);
1581     EXPECT_LE(timeCost, TIMEOUT_US);
1582 
1583     // 睡眠5s,确保seesionId已失效
1584     usleep(KEEP_SESSION_TIME_US);
1585 
1586     // StartSession
1587     timer.Reset();
1588     EXPECT_FALSE(StartPluginSession(sessionId));
1589     timeCost = timer.ElapsedUs();
1590     printf("UnusualTest: StartSession cost %ldus.\n", timeCost);
1591     EXPECT_LE(timeCost, TIMEOUT_US);
1592 
1593     // FetchData
1594     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr;
1595     grpc::ClientContext fetchContext;
1596     FetchDataRequest fetchRequest = FetchDataRequest();
1597     fetchRequest.set_request_id(0);
1598     fetchRequest.set_session_id(sessionId);
1599     fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest);
1600     ASSERT_TRUE(fetchResponse != nullptr);
1601     FetchDataResponse rp;
1602     fetchResponse.get()->Read(&rp);
1603     EXPECT_EQ(rp.plugin_data().size(), 0);
1604 
1605     // StopSession
1606     timer.Reset();
1607     EXPECT_FALSE(StopPluginSession(sessionId));
1608     timeCost = timer.ElapsedUs();
1609     printf("UnusualTest: StopSession cost %ldus.\n", timeCost);
1610     EXPECT_LE(timeCost, TIMEOUT_US);
1611 
1612     // DestroySession
1613     timer.Reset();
1614     EXPECT_FALSE(DestroyPluginSession(sessionId));
1615     timeCost = timer.ElapsedUs();
1616     printf("UnusualTest: DestroySession cost %ldus.\n", timeCost);
1617     EXPECT_LE(timeCost, TIMEOUT_US);
1618 
1619     // 验证心跳正常,pid为1的cpu插件数据上报
1620     GetCapabilitiesResponse capResponse2;
1621     EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse2));
1622 
1623     uint32_t sessionId1;
1624     EXPECT_TRUE(CreatePluginSession(sessionId1, 1));
1625 
1626     // KeepSession
1627     EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1));
1628 
1629     // 开启每隔4s发一次心跳的线程,确保seesionid不会失效
1630     bool sendHeart = true;
1631     int loopCount = 0;
__anon72c0da701202() 1632     std::thread keepSessionThread1([&]() {
1633         while (sendHeart && loopCount < LOOP_COUNT) {
1634             ++loopCount;
1635             KeepSessionRequest request;
1636             request.set_request_id(0);
1637             request.set_session_id(sessionId1);
1638             request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS);
1639             grpc::ClientContext context;
1640             KeepSessionResponse ksResponse;
1641             grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse);
1642             if (sendHeart) {
1643                 EXPECT_TRUE(status.ok());
1644             }
1645             usleep(KEEP_SESSION_SLEEP_US);
1646         }
1647     });
1648 
1649     // StartSession
1650     EXPECT_TRUE(StartPluginSession(sessionId1));
1651 
1652     // FetchData
1653     std::vector<CpuData> cpuDataVec1;
1654     std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse1 = nullptr;
1655     grpc::ClientContext fetchContext2;
1656     FetchDataRequest fetchRequest1 = FetchDataRequest();
1657     fetchRequest1.set_request_id(0);
1658     fetchRequest1.set_session_id(sessionId1);
1659     fetchResponse1 = profilerStub_->FetchData(&fetchContext2, fetchRequest1);
1660     ASSERT_TRUE(fetchResponse1 != nullptr);
__anon72c0da701302() 1661     std::thread fetchDataThread1([&]() {
1662         int ict = 0;
1663         while (1) {
1664             FetchDataResponse rp;
1665             fetchResponse1.get()->Read(&rp);
1666             for (int i = 0; i < rp.plugin_data().size(); i++) {
1667                 ProfilerPluginData pd = rp.plugin_data(i);
1668                 if (ict == ROUND_COUNT) {
1669                     return;
1670                 }
1671 
1672                 ASSERT_STREQ(pd.name().c_str(), "cpu-plugin");
1673                 ict += 1;
1674             }
1675         }
1676     });
1677     fetchDataThread1.join();
1678 
1679     // StopSession
1680     EXPECT_TRUE(StopPluginSession(sessionId1));
1681 
1682     // DestroySession
1683     EXPECT_TRUE(DestroyPluginSession(sessionId1));
1684 
1685     // 销毁会话之后停止心跳发送
1686     sendHeart = false;
1687     keepSessionThread1.join();
1688 
1689     StopProcessStub(hiprofilerPluginsPid_);
1690     StopProcessStub(hiprofilerdPid_);
1691 
1692     pluginVec_.clear();
1693 }
1694 }
1695 
1696 #pragma clang optimize on