!420 新增UT测试用例,删除冗余代码

Merge pull request !420 from wenlong_12/master
This commit is contained in:
openharmony_ci 2024-01-19 10:28:50 +00:00 committed by Gitee
commit 3d11c88205
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 49 additions and 111 deletions

View File

@ -549,7 +549,6 @@ private:
void OutputStdContentDiff(ReportEventConfigItem &, ReportEventConfigItem &);
void OutputStdContentItem(const ReportItem &reportItem);
void OutputStdContentDiffOneSide(bool leftOnly, ReportItem &reportItem);
void OutputStdCallFrames(int indent, const ReportItemCallFrame &callFrames,
uint64_t totalEventCount);

View File

@ -128,7 +128,7 @@ private:
static void Report(const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &countEvents);
static void PrintPerHead();
static void GetPerKey(std::string &perKey, const PerfEvents::Summary &summary);
static void FormatComments(const std::unique_ptr<PerfEvents::ReportSum> &reportSum, std::string &commentStr);
static void MakeComments(const std::unique_ptr<PerfEvents::ReportSum> &reportSum, std::string &commentStr);
static void ReportNormal(const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &countEvents);
static void ReportDetailInfos(const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &countEvents);
static void PrintPerValue(const std::unique_ptr<PerfEvents::ReportSum> &reportSum, const float &ratio,

View File

@ -114,7 +114,6 @@ public:
{
Clean();
}
const char *HoldStringView(std::string_view view);
// only use in UT
void Clean()
{
@ -336,12 +335,9 @@ pid_t GetAppPackagePid(const std::string &appPackage, const pid_t oldPid, const
bool IsRestarted(const std::string &appPackage);
void CollectPidsByAppname(std::set<pid_t> &pids, const std::string &appPackage);
bool CheckAppIsRunning (std::vector<pid_t> &selectPids, const std::string &appPackage, int checkAppMs);
bool IsExistDebugByApp(const std::string& bundleName);
bool IsExistDebugByPid(const std::vector<pid_t> pids);
bool IsSupportNonDebuggableApp();
const std::string GetUserType();
std::string GetProcessName(int pid);
bool IsDebugableApp(const std::string& bundleName);
bool NeedAdaptSandboxPath(char *filename, int pid, u16 &headerSize);
bool NeedAdaptHMBundlePath(std::string& filename, std::string threadname);

View File

@ -606,18 +606,6 @@ void Report::OutputStdContentDiff(ReportEventConfigItem &left, ReportEventConfig
}
}
void Report::OutputStdContentDiffOneSide(bool leftOnly, ReportItem &reportItem)
{
if (reportItem.heat > option_.heatLimit_) {
if (leftOnly) {
OutputStdItemHeating(reportItem.heat, 0.0f);
} else {
OutputStdItemHeating(0.0f, reportItem.heat);
}
OutputStdContentItem(reportItem);
}
}
void Report::OutputStd(FILE *output)
{
output_ = output;

View File

@ -193,12 +193,14 @@ void SubCommandStat::PrintPerValue(const std::unique_ptr<PerfEvents::ReportSum>
std::string strEventCount = std::to_string(reportSum->eventCountSum);
for (size_t i = strEventCount.size() - 1, j = 1; i > 0; --i, ++j) {
if (j == THOUSNADS_SEPARATOR) {
strEventCount.insert(strEventCount.begin() + i, ',');
j = 0;
strEventCount.insert(strEventCount.begin() + i, ',');
}
}
std::string commentStr;
FormatComments(reportSum, commentStr);
MakeComments(reportSum, commentStr);
if (g_reportCpuFlag && g_reportThreadFlag) {
printf(" %24s %-30s | %-30s %10d %10d %10d | %-32s | (%.0lf%%)\n", strEventCount.c_str(), configName.c_str(),
reportSum->threadName.c_str(), reportSum->pid, reportSum->tid, reportSum->cpu, commentStr.c_str(),
@ -347,7 +349,7 @@ std::string SubCommandStat::GetCommentConfigName(
return commentConfigName;
}
void SubCommandStat::FormatComments(const std::unique_ptr<PerfEvents::ReportSum> &reportSum, std::string &commentStr)
void SubCommandStat::MakeComments(const std::unique_ptr<PerfEvents::ReportSum> &reportSum, std::string &commentStr)
{
if (reportSum->commentSum == 0) {
return;

View File

@ -37,27 +37,6 @@ namespace OHOS {
namespace Developtools {
namespace HiPerf {
const char *MemoryHold::HoldStringView(std::string_view view)
{
if (view.size() == 0) {
return "";
}
try {
// for null end
char *p = new (std::nothrow) char[view.size() + 1];
if (p == nullptr) {
return "";
}
p[view.size()] = '\0';
std::copy(view.data(), view.data() + view.size(), p);
holder_.emplace_back(p);
return p;
} catch (...) {
return "";
}
return "";
}
std::string CanonicalizeSpecPath(const char* src)
{
if (src == nullptr) {
@ -679,37 +658,6 @@ bool CheckAppIsRunning (std::vector<pid_t> &selectPids, const std::string &appPa
return true;
}
bool IsExistDebugByApp(const std::string& bundleName)
{
if (!IsSupportNonDebuggableApp() && !bundleName.empty() && !IsDebugableApp(bundleName)) {
HLOGE("--app option only support debug aplication.");
printf("--app option only support debug aplication\n");
return false;
}
return true;
}
bool IsExistDebugByPid(const std::vector<pid_t> pids)
{
if (pids.empty()) {
HLOGE("IsExistDebugByPid: pids is empty.");
return true;
}
for (auto pid : pids) {
if (pid <= 0) {
printf("Invalid -p value '%d', the pid should be larger than 0\n", pid);
return false;
}
std::string bundleName = GetProcessName(pid);
if (!IsSupportNonDebuggableApp() && !IsDebugableApp(bundleName)) {
HLOGE("-p option only support debug aplication for %s", bundleName.c_str());
printf("-p option only support debug aplication\n");
return false;
}
}
return true;
}
bool IsSupportNonDebuggableApp()
{
// root first
@ -793,40 +741,6 @@ std::string GetProcessName(int pid)
#endif
}
bool IsDebugableApp(const std::string& bundleName)
{
#if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE)
if (bundleName.empty()) {
printf("bundleName is empty!\n");
return false;
}
sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sam == nullptr) {
printf("GetSystemAbilityManager failed!\n");
return false;
}
sptr<IRemoteObject> remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
if (remoteObject == nullptr) {
printf("Get BundleMgr SA failed!\n");
return false;
}
sptr<BundleMgrProxy> proxy = iface_cast<BundleMgrProxy>(remoteObject);
if (proxy == nullptr) {
printf("iface_cast failed!\n");
return false;
}
int uid = proxy->GetUidByDebugBundleName(bundleName, Constants::ANY_USERID);
if (uid < 0) {
HLOGE("Get application info failed, bundleName:%s, uid is %d.", bundleName.c_str(), uid);
return false;
}
return true;
#else
return false;
#endif
}
bool NeedAdaptSandboxPath(char *filename, int pid, u16 &headerSize)
{
std::string oldFilename = filename;

View File

@ -46,7 +46,7 @@ public:
void SetUp();
void TearDown();
void TestEvents(std::string &opt, std::string &uk);
void TestEvents(std::string &opt, std::string &uk, bool isFork = true);
static void ForkAndRunTest(const std::string& cmd, bool expect = true, bool fixPid = true);
@ -60,7 +60,7 @@ public:
static constexpr size_t TEST_SIZE_F2000_DWARF_SYSTEM = 8.3E4 * 1024;
static constexpr size_t TEST_SIZE_F4000_DWARF_SYSTEM = 1.7E5 * 1024;
static constexpr size_t TEST_SIZE_F8000_DWARF_SYSTEM = 3.5E5 * 1024;
static constexpr size_t TEST_SIZE_F100_FP_SYSTEM = 8E3 * 1024;
static constexpr size_t TEST_SIZE_F100_FP_SYSTEM = 10E3 * 1024;
static constexpr size_t TEST_SIZE_F500_FP_SYSTEM = 2E4 * 1024;
static constexpr size_t TEST_SIZE_F1000_FP_SYSTEM = 3E4 * 1024;
static constexpr size_t TEST_SIZE_F2000_FP_SYSTEM = 4E4 * 1024;
@ -412,7 +412,7 @@ HWTEST_F(SubCommandRecordTest, PeriodAndFrequncyConflict, TestSize.Level1)
TestRecordCommand("-d 2 -f 2000 --period 10 ", false);
}
void SubCommandRecordTest::TestEvents(std::string &opt, std::string &uk)
void SubCommandRecordTest::TestEvents(std::string &opt, std::string &uk, bool isFork)
{
PerfEvents perfEvents;
for (auto type : TYPE_CONFIGS) {
@ -434,7 +434,11 @@ void SubCommandRecordTest::TestEvents(std::string &opt, std::string &uk)
testEventCount--;
}
cmdline.pop_back(); // remove the last ','
ForkAndRunTest(cmdline);
if (isFork) {
ForkAndRunTest(cmdline);
} else {
TestRecordCommand(cmdline);
}
TearDown();
SetUp();
}
@ -462,6 +466,13 @@ HWTEST_F(SubCommandRecordTest, SelectEventsKernel, TestSize.Level1)
TestEvents(opt, uk);
}
HWTEST_F(SubCommandRecordTest, SelectEventsKernel_2, TestSize.Level1)
{
std::string opt = "-d 2 -c 0 -e ";
std::string uk = ":k";
TestEvents(opt, uk, false);
}
HWTEST_F(SubCommandRecordTest, SelectEventsErr, TestSize.Level1)
{
ForkAndRunTest("-d 2 -c 0 -e what ", false);
@ -505,6 +516,11 @@ HWTEST_F(SubCommandRecordTest, SelectPid, TestSize.Level1)
ForkAndRunTest("-d 2 -p 1 ", true, false);
}
HWTEST_F(SubCommandRecordTest, KernelSymbols, TestSize.Level1)
{
TestRecordCommand("-d 2 -p 2 -s dwarf ", true, false);
}
HWTEST_F(SubCommandRecordTest, SelectPidMulti, TestSize.Level1)
{
ForkAndRunTest("-d 2 -p 1,2,3 ", true, false);

View File

@ -627,6 +627,29 @@ HWTEST_F(UtilitiesTest, GetSubthreadIDs, TestSize.Level1)
}
ExitThreads();
}
/**
* @tc.name: IsBeta
* @tc.desc:
* @tc.type: FUNC
*/
HWTEST_F(UtilitiesTest, IsBeta, TestSize.Level1)
{
EXPECT_EQ(IsBeta(), true);
}
HWTEST_F(UtilitiesTest, CanonicalizeSpecPath, TestSize.Level1)
{
EXPECT_EQ(CanonicalizeSpecPath(nullptr), "");
EXPECT_EQ(CanonicalizeSpecPath("/data/local/tmp/test/../test.txt"), "");
EXPECT_EQ(CanonicalizeSpecPath("/data/local/tmp/nonexistent.txt"), "/data/local/tmp/nonexistent.txt");
string largePath = "./";
for (int i = 0; i < 512; i++) { // 512: loop size
largePath += "testpath";
}
largePath += ".txt";
EXPECT_EQ(CanonicalizeSpecPath(largePath.c_str()), "");
}
} // namespace HiPerf
} // namespace Developtools
} // namespace OHOS