!1039 增加profiler UT 用例

Merge pull request !1039 from wenlong_12/master
This commit is contained in:
openharmony_ci 2023-08-07 02:06:15 +00:00 committed by Gitee
commit 05ab55578f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 223 additions and 9 deletions

View File

@ -21,6 +21,7 @@
#include "hook_service.h"
#include "service_entry.h"
#include "socket_context.h"
#include "unix_socket_client.h"
#include "logging.h"
using namespace testing::ext;
@ -63,13 +64,12 @@ HWTEST_F(HookSocketClientTest, ProtocolProc, TestSize.Level1)
*/
HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1)
{
uint64_t config = FILTER_SIZE;
config <<= MOBILE_BIT;
config |= SMB_SIZE;
ClientConfig config = {0};
config.shareMemroySize = SMB_SIZE;
HookSocketClient hookClient(1, &g_ClientConfigTest);
SocketContext socketContext;
auto ptr = reinterpret_cast<const int8_t*>(&config);
auto size = sizeof(uint64_t);
auto size = sizeof(ClientConfig);
ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size));
struct timespec ts = {};
@ -80,7 +80,11 @@ HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1)
HILOG_ERROR(LOG_CORE, "memcpy_s ts failed");
}
metaSize = sizeof(ts);
ASSERT_FALSE(hookClient.SendStack(buffer.get(), metaSize));
EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize));
EXPECT_FALSE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize));
hookClient.unixSocketClient_ = std::make_shared<UnixSocketClient>();
EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize));
EXPECT_TRUE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize));
}
/*

View File

@ -203,12 +203,17 @@ HWTEST_F(StackWriterTest, WriterSyncTest, TestSize.Level1)
uint8_t buffer3[] = {0xAA, 0xBB, 0xCC, 0xDD};
uint8_t buffer4[] = {0xCC, 0xDD, 0xBB, 0xEE};
EXPECT_FALSE(write->WriteWithPayloadTimeout(nullptr, 0, nullptr, 0));
EXPECT_TRUE(write->WriteWithPayloadTimeout((const void*)buffer1, sizeof(buffer1),
(const void*)buffer2, sizeof(buffer2)));
EXPECT_TRUE(CheckBuffer(buffer1, sizeof(buffer1)));
write->blocked_ = false;
EXPECT_TRUE(write->WriteWithPayloadTimeout((const void*)buffer3, sizeof(buffer3),
(const void*)buffer4, sizeof(buffer4)));
EXPECT_TRUE(CheckBuffer(buffer3, sizeof(buffer3)));
EXPECT_FALSE(write->Write(nullptr, 0));
EXPECT_FALSE(write->WriteTimeout(nullptr, 0));
EXPECT_TRUE(write->WriteTimeout((const void*)buffer1, sizeof(buffer1)));
EXPECT_TRUE(write->Clear());
}
} // namespace

View File

@ -43,6 +43,8 @@ HWTEST_F(ClientConnectionTest, RawProtocolProc, TestSize.Level1)
std::unique_ptr<ClientConnection> clientConnection = std::make_unique<ClientConnection>(0, serviceEntry);
ASSERT_EQ(clientConnection->RawProtocolProc(RAW_PROTOCOL_POINTTO_SERVICE,
(const int8_t *)serviceName.c_str(), sizeof(struct RawPointToService)), 1);
ASSERT_EQ(clientConnection->RawProtocolProc(0,
(const int8_t *)serviceName.c_str(), sizeof(struct RawPointToService)), -1);
clientConnection.reset(nullptr);
}
} // namespace

View File

@ -39,7 +39,14 @@ public:
HWTEST_F(ClientMapTest, ClientSocket, TestSize.Level1)
{
ServiceEntry serviceEntry;
ServiceEntry serviceTest;
ASSERT_EQ(ClientMap::GetInstance().PutClientSocket(1, serviceEntry), 1);
ClientMap::GetInstance().PutClientSocket(2, serviceTest);
ASSERT_EQ(ClientMap::GetInstance().PutClientSocket(2, serviceTest), -1);
ASSERT_EQ(ClientMap::GetInstance().AutoRelease(), 1);
ClientMap::GetInstance().socketClients_[2]->clientState_ = CLIENT_STAT_THREAD_EXITED;
ASSERT_EQ(ClientMap::GetInstance().AutoRelease(), 1);
ASSERT_EQ(ClientMap::GetInstance().ClearClientSocket(), 0);
ASSERT_EQ(ClientMap::GetInstance().AutoRelease(), 1);
}
} // namespace

View File

@ -38,15 +38,16 @@ public:
HWTEST_F(IpcGeneratorImplTest, NormalCase, TestSize.Level1)
{
IpcGeneratorImpl ipcGeneratorImpl;
IpcGeneratorImpl ipcGeneratorTest;
EXPECT_TRUE(ipcGeneratorTest.SetNames("test_case.proto", "") == "test_case");
ASSERT_TRUE(ipcGeneratorImpl.SetNames("test.proto", "") == "test");
ASSERT_TRUE(ipcGeneratorImpl.AddService("TestService"));
ASSERT_TRUE(!ipcGeneratorImpl.AddService("TestService"));
ASSERT_TRUE(ipcGeneratorImpl.AddServiceMethod("TestService", "DoTest", "DoTestRequest", "DoTestResponse"));
ASSERT_TRUE(ipcGeneratorImpl.AddServiceMethod("TestService", "DoTest2", "DoTest2Request", "DoTest2Response"));
ASSERT_TRUE(!ipcGeneratorImpl.AddServiceMethod("TestService", "DoTest", "DoTestRequest", "DoTestResponse"));
ipcGeneratorImpl.GenHeader();
ipcGeneratorTest.GenHeader();
ipcGeneratorImpl.GenSource();
}
} // namespace

View File

@ -51,7 +51,11 @@ HWTEST_F(SocketContextTest, RawProtocolProc, TestSize.Level1)
{
std::string s="abc";
SocketContext socketContext;
std::vector<unsigned char> buf(64); // 64: the buf size
ASSERT_EQ(socketContext.RawProtocolProc(2, (const int8_t *)s.c_str(), s.size()), -1);
EXPECT_FALSE(socketContext.ReceiveData(-1, buf.data(), buf.size()));
EXPECT_FALSE(socketContext.ReceiveData(1, nullptr, 0));
EXPECT_FALSE(socketContext.SendRaw(1, (const int8_t *)s.c_str(), s.size(), -1));
}
/**
@ -64,4 +68,16 @@ HWTEST_F(SocketContextTest, ReceiveFileDiscriptor, TestSize.Level1)
SocketContext socketContext;
ASSERT_EQ(socketContext.ReceiveFileDiscriptor(), -1);
}
/**
* @tc.name: Service
* @tc.desc: Socket send heart beat test.
* @tc.type: FUNC
*/
HWTEST_F(SocketContextTest, SendHeartBeat, TestSize.Level1)
{
SocketContext socketContext;
EXPECT_FALSE(socketContext.SendHeartBeat());
EXPECT_FALSE(socketContext.SendHookConfig(nullptr, 0));
}
} // namespace

View File

@ -22,6 +22,7 @@
#include "plugin_command_builder.h"
#include "plugin_service.ipc.h"
#include "plugin_service_impl.h"
#include "plugin_session_manager.h"
#include "profiler_data_repeater.h"
#include "profiler_service_types.pb.h"
#include "socket_context.h"
@ -48,12 +49,14 @@ public:
void SetUp()
{
pluginService_ = std::make_unique<PluginService>();
pluginService_ = std::make_shared<PluginService>();
usleep(100000); // sleep for 100000 us.
pluginClient_ = std::make_unique<PluginClientTest>();
pluginServiceImp_ = std::make_unique<PluginServiceImpl>(*pluginService_);
commandBuilder_ = std::make_unique<PluginCommandBuilder>();
pluginSessionMgr_ = std::make_shared<PluginSessionManager>(pluginService_);
pluginClient_->Connect(DEFAULT_UNIX_SOCKET_FULL_PATH);
pluginService_->SetPluginSessionManager(pluginSessionMgr_);
}
void TearDown()
@ -61,13 +64,15 @@ public:
pluginClient_ = nullptr;
pluginService_ = nullptr;
commandBuilder_ = nullptr;
pluginSessionMgr_ = nullptr;
}
public:
std::unique_ptr<PluginService> pluginService_ = nullptr;
std::shared_ptr<PluginService> pluginService_ = nullptr;
std::unique_ptr<PluginClientTest> pluginClient_ = nullptr;
std::unique_ptr<PluginServiceImpl> pluginServiceImp_ = nullptr;
std::unique_ptr<PluginCommandBuilder> commandBuilder_ = nullptr;
std::shared_ptr<PluginSessionManager> pluginSessionMgr_ = nullptr;
uint32_t pluginId_;
};
@ -140,7 +145,17 @@ HWTEST_F(UnitTestPluginService, CreatePluginSession2, TestSize.Level1)
plugin.sha256 = "ASDFAADSF";
plugin.context = &ctx;
pluginService_->AddPluginInfo(plugin);
ProfilerSessionConfig psc;
psc.set_session_mode(ProfilerSessionConfig::OFFLINE);
pluginService_->SetProfilerSessionConfig(psc);
TraceFileWriterPtr traceWriter = std::make_shared<TraceFileWriter>("/data/local/tmp/tracewrite.trace");
pluginService_->SetTraceWriter(traceWriter);
EXPECT_TRUE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
uint32_t pluginId = pluginService_->GetPluginIdByName("abc2.so");
PluginContextPtr pluginCtx = pluginService_->GetPluginContextById(pluginId);
pluginService_->ReadShareMemoryOffline(*pluginCtx);
EXPECT_TRUE(pluginService_->DestroyPluginSession("abc2.so"));
EXPECT_TRUE(pluginService_->RemovePluginSessionCtx("abc2.so"));
pluginService_->RemovePluginInfo(plugin);
}
@ -614,6 +629,11 @@ HWTEST_F(UnitTestPluginService, AddPluginInfo4, TestSize.Level1)
plugin.bufferSizeHint = 0;
plugin.sha256 = "";
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
plugin.bufferSizeHint = 10;
plugin.isStandaloneFileData = true;
plugin.outFileName = "test-file.bin";
plugin.pluginVersion = "1.02";
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
}
/**
@ -897,6 +917,11 @@ HWTEST_F(UnitTestPluginService, PluginServiceImpl_RegisterPlugin, TestSize.Level
EXPECT_TRUE(pluginServiceImp_->RegisterPlugin(ctx, request, response));
request.set_sha256("abcdsfdsfad");
EXPECT_FALSE(pluginServiceImp_->RegisterPlugin(ctx, request, response));
UnregisterPluginRequest unRequest;
UnregisterPluginResponse unResponse;
uint32_t plugId = pluginService_->GetPluginIdByName("sample2.so");
unRequest.set_plugin_id(plugId);
EXPECT_TRUE(pluginServiceImp_->UnregisterPlugin(ctx, unRequest, unResponse));
}
/**
@ -911,6 +936,14 @@ HWTEST_F(UnitTestPluginService, PluginServiceImpl_UnregisterPlugin, TestSize.Lev
SocketContext ctx;
request.set_plugin_id(1);
EXPECT_FALSE(pluginServiceImp_->UnregisterPlugin(ctx, request, response));
GetCommandRequest gcRequest;
GetCommandResponse gcResponse;
EXPECT_FALSE(pluginServiceImp_->GetCommand(ctx, gcRequest, gcResponse));
NotifyResultRequest nResRequest;
NotifyResultResponse nResResponse;
EXPECT_TRUE(pluginServiceImp_->NotifyResult(ctx, nResRequest, nResResponse));
}
/**
* @tc.name: BuildCreateSessionCmd
@ -929,5 +962,132 @@ HWTEST_F(UnitTestPluginService, pluginCommandBuilder, TestSize.Level1)
EXPECT_TRUE(commandBuilder_->BuildRefreshSessionCmd(pluginId) != nullptr);
EXPECT_TRUE(commandBuilder_->BuildStopSessionCmd(pluginId) != nullptr);
EXPECT_TRUE(commandBuilder_->BuildDestroySessionCmd(pluginId) != nullptr);
EXPECT_FALSE(commandBuilder_->GetedCommandResponse(0));
EXPECT_TRUE(commandBuilder_->GetedCommandResponse(1));
}
/**
* @tc.name: PluginSessionManager
* @tc.desc: test plugin session manager checkBufferConfig
* @tc.type: FUNC
*/
HWTEST_F(UnitTestPluginService, PluginSessionManager_Check, TestSize.Level1)
{
PluginInfo plugin;
SocketContext ctx;
plugin.name = "abc_check.so";
plugin.bufferSizeHint = 0;
plugin.sha256 = "asdfasdfasdfasfd";
plugin.context = &ctx;
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
ProfilerSessionConfig::BufferConfig bc;
bc.set_pages(1);
bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
EXPECT_TRUE(pluginSessionMgr_->CheckBufferConfig(bc));
ProfilerPluginConfig ppc;
ppc.set_name("abc_check.so");
ppc.set_plugin_sha256("asdfasdfasdfasfd");
EXPECT_TRUE(pluginSessionMgr_->CheckPluginSha256(ppc));
}
/**
* @tc.name: PluginSessionManager
* @tc.desc: test plugin session manager by creating sesssion
* @tc.type: FUNC
*/
HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSession, TestSize.Level1)
{
PluginInfo plugin;
SocketContext ctx;
plugin.name = "testsample.so";
plugin.bufferSizeHint = 0;
plugin.sha256 = "ASDFAADSF";
plugin.context = &ctx;
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
ProfilerPluginConfig ppc;
ppc.set_name("testsample.so");
ppc.set_plugin_sha256("ASDFAADSF");
ppc.set_sample_interval(SAMPLE_INTERVAL);
ProfilerSessionConfig::BufferConfig bc;
bc.set_pages(1);
bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
EXPECT_TRUE(pluginSessionMgr_->CreatePluginSession(
ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)) != nullptr);
}
/**
* @tc.name: PluginSessionManager
* @tc.desc: test plugin session manager by creating a lot of sesssions
* @tc.type: FUNC
*/
HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSessions, TestSize.Level1)
{
PluginInfo plugin;
SocketContext ctx;
plugin.name = "testsample1.so";
plugin.bufferSizeHint = 0;
plugin.sha256 = "ASDFAADSF";
plugin.context = &ctx;
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
ProfilerPluginConfig ppc;
ppc.set_name("testsample1.so");
ppc.set_plugin_sha256("ASDFAADSF");
ppc.set_sample_interval(SAMPLE_INTERVAL);
ProfilerSessionConfig::BufferConfig bc;
bc.set_pages(1);
bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
std::vector<ProfilerPluginConfig> vecConfig;
std::vector<ProfilerSessionConfig::BufferConfig> vecBuf;
vecConfig.push_back(ppc);
vecBuf.push_back(bc);
EXPECT_TRUE(pluginSessionMgr_->CreatePluginSessions(vecConfig, vecBuf,
std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
std::vector<std::string> nameList{"testsample.so", "testsample1.so"};
EXPECT_TRUE(pluginSessionMgr_->InvalidatePluginSessions(nameList));
std::vector<PluginSession::State> vecRet = pluginSessionMgr_->GetStatus(nameList);
EXPECT_TRUE(vecRet.size() != 0);
EXPECT_TRUE(pluginSessionMgr_->RefreshPluginSession());
}
/**
* @tc.name: plugin service
* @tc.desc: test createpluginsession online
* @tc.type: FUNC
*/
HWTEST_F(UnitTestPluginService, PluginService_CreatePluginSession_Online, TestSize.Level1)
{
ProfilerPluginConfig ppc;
ppc.set_name("abconline.so");
ppc.set_plugin_sha256("ASDFAADSF");
ppc.set_sample_interval(SAMPLE_INTERVAL);
ProfilerSessionConfig::BufferConfig bc;
bc.set_pages(1);
bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
PluginInfo plugin;
SocketContext ctx;
plugin.name = "abconline.so";
plugin.bufferSizeHint = 0;
plugin.sha256 = "ASDFAADSF";
plugin.context = &ctx;
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
ProfilerSessionConfig psc;
psc.set_session_mode(ProfilerSessionConfig::ONLINE);
pluginService_->SetProfilerSessionConfig(psc);
EXPECT_TRUE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
uint32_t pluginId = pluginService_->GetPluginIdByName("abconline.so");
PluginContextPtr pluginCtx = pluginService_->GetPluginContextById(pluginId);
pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
pluginService_->ReadShareMemoryOnline(*pluginCtx);
NotifyResultRequest nrr;
nrr.set_request_id(5);
nrr.set_command_id(5);
PluginResult* result = nrr.add_result();
result->set_plugin_id(pluginId);
result->set_out_file_name("/data/local/tmp/testonline.trace");
result->set_data("test plugin result!");
EXPECT_TRUE(pluginService_->AppendResult(nrr));
plugin.id = pluginId;
EXPECT_TRUE(pluginService_->RemovePluginInfo(plugin));
}
} // namespace

View File

@ -1392,6 +1392,10 @@ HWTEST_F(ProfilerServiceTest, MergeStandaloneFile, TestSize.Level1)
EXPECT_EQ(writerTarget->Write(traceData.data(), traceData.size()), sizeof(uint32_t) + traceData.size());
std::string pluginName = "destroy_session_standalone_test";
service_->MergeStandaloneFile(targetPath, pluginName, testPath, "1.02");
service_->MergeStandaloneFile(targetPath, "", testPath, "1.02");
service_->MergeStandaloneFile(targetPath, pluginName, "", "1.02");
service_->MergeStandaloneFile("", pluginName, testPath, "1.02");
service_->MergeStandaloneFile(targetPath, "hiperf-plugin", testPath, "1.02");
}
} // namespace

View File

@ -132,8 +132,23 @@ HWTEST_F(TraceFileWriterTest, SplitFileWriter, TestSize.Level1)
path = "trace-write-test.bin";
auto writer = std::make_shared<TraceFileWriter>(path, true, 0, 0);
EXPECT_NE(writer, nullptr);
writer->Path();
writer->SetStopSplitFile(false);
std::string testData = "this is a test case!";
EXPECT_EQ(writer->Write(testData.data(), testData.size()), sizeof(uint32_t) + testData.size());
std::string testStr = "test case";
writer->SetPluginConfig(testStr.data(), testStr.size());
std::string testPath = "trace-write-bin";
auto writerTestPath = std::make_shared<TraceFileWriter>(testPath, true, 0, 1);
EXPECT_NE(writerTestPath, nullptr);
writerTestPath->splitFilePaths_.push("trace-write-path-1");
writerTestPath->splitFilePaths_.push("trace-write-path-2");
writerTestPath->DeleteOldSplitFile();
EXPECT_TRUE(writerTestPath->IsSplitFile(300 * 1024 * 1024));
std::string testPathTemp = "/data/local/tmp/trace-write-path";
auto writerTemp = std::make_shared<TraceFileWriter>(testPathTemp, true, 0, 1);
EXPECT_NE(writerTemp, nullptr);
}
} // namespace