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