From 3f684831d0929b16054ff7a9cfca3836b08828c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=9B=E5=9B=BD=E4=BA=AE?= Date: Thu, 11 Sep 2025 11:03:47 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=93=9D=E9=BB=84?= =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 牛国亮 --- .../client/client_command/cpu_info.cpp | 6 +- .../client/client_command/include/cpu_info.h | 2 +- .../client/client_command/test/BUILD.gn | 1 + .../test/unittest/filedescriptor_test.cpp | 103 ++++++++++++++++++ 4 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 host/smartperf/client/client_command/test/unittest/filedescriptor_test.cpp diff --git a/host/smartperf/client/client_command/cpu_info.cpp b/host/smartperf/client/client_command/cpu_info.cpp index f4d8d3235..bd2e26020 100644 --- a/host/smartperf/client/client_command/cpu_info.cpp +++ b/host/smartperf/client/client_command/cpu_info.cpp @@ -64,7 +64,7 @@ std::map CPUInfo::ItemData() return result; } -void CPUInfo::CalculateCPIInfo(const std::string line, double &cpiTotal, size_t &cpiCount) +void CPUInfo::CalculateCPIInfo(const std::string line, double &cpi_Total, size_t &cpi_Count) { auto trim = [](std::string& s) { s.erase(0, s.find_first_not_of(" \t")); @@ -78,8 +78,8 @@ void CPUInfo::CalculateCPIInfo(const std::string line, double &cpiTotal, size_t if (cpi_pos != std::string::npos) { size_t number_end = comment.find(" ", 0); std::string cpi_str = comment.substr(0, number_end); - cpiTotal += SPUtilesTye::StringToSometype(cpi_str); - ++cpiCount; + cpi_Total += SPUtilesTye::StringToSometype(cpi_str); + ++cpi_Count; } } } diff --git a/host/smartperf/client/client_command/include/cpu_info.h b/host/smartperf/client/client_command/include/cpu_info.h index 428ce835a..95212e5bf 100644 --- a/host/smartperf/client/client_command/include/cpu_info.h +++ b/host/smartperf/client/client_command/include/cpu_info.h @@ -41,7 +41,7 @@ public: void Start(); void Stop(); private: - void CalculateCPIInfo(const std::string line, double &cpiTotal, size_t &cpiCount); + void CalculateCPIInfo(const std::string line, double &cpi_Total, size_t &cpi_Count); std::vector pids_; std::thread th_; std::string hiperfCmd_ = "/bin/hiperf stat -e " + SPUtils::GetProductName() + "-instructions," + diff --git a/host/smartperf/client/client_command/test/BUILD.gn b/host/smartperf/client/client_command/test/BUILD.gn index 4cc849b96..370e943a4 100644 --- a/host/smartperf/client/client_command/test/BUILD.gn +++ b/host/smartperf/client/client_command/test/BUILD.gn @@ -67,6 +67,7 @@ ohos_unittest("sp_daemon_ut") { "unittest/ddr_test.cpp", "unittest/dubai_test.cpp", "unittest/editor_command_test.cpp", + "unittest/filedescriptor_test.cpp", "unittest/fps_test.cpp", "unittest/gpuCounter_test.cpp", "unittest/parse_click_complete_trace_test.cpp", diff --git a/host/smartperf/client/client_command/test/unittest/filedescriptor_test.cpp b/host/smartperf/client/client_command/test/unittest/filedescriptor_test.cpp new file mode 100644 index 000000000..e5a52da6f --- /dev/null +++ b/host/smartperf/client/client_command/test/unittest/filedescriptor_test.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "FileDescriptor.h" +#include "sp_utils.h" +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace SmartPerf { +class SPdaemonFdsTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + void SetUp() {} + void TearDown() {} +}; + +static bool VerifResult(const std::string &result) +{ + int fdTotalInt = 0; + std::regex fdTotalRegex(R"(fdTotal=(\d+))"); + std::smatch match; + std::string::const_iterator searchStart(result.cbegin()); + while (std::regex_search(searchStart, result.cend(), match, fdTotalRegex)) { + std::cout << "Match found: " << match.str(0) << std::endl; + std::string fdTotalStr = match.str(1); + fdTotalInt = std::stoi(fdTotalStr); + std::cout << "fdTotalInt as integer: " << fdTotalInt << std::endl; + searchStart = match.suffix().first; + } + return fdTotalInt > 0; +} + +/** + * @tc.name: FdsTestCase01 + * @tc.desc: Test Fds by packagename + * @tc.type: FUNC + */ +HWTEST_F(SPdaemonFdsTest, FdsTestCase01, TestSize.Level1) +{ + std::string cmd = "SP_daemon -N 1 -PKG ohos.samples.ecg -fds"; + std::string result = ""; + bool flag = false; + bool ret = SPUtils::LoadCmd(cmd, result); + std::string::size_type strOne = result.find("command exec finished!"); + if ((strOne != result.npos)) { + flag = true; + } + EXPECT_EQ(ret, true); + EXPECT_TRUE(flag); +} + +/** + * @tc.name: FdsTestCase03 + * @tc.desc: Test Fds by pid not exit + * @tc.type: FUNC + */ +HWTEST_F(SPdaemonFdsTest, FdsTestCase03, TestSize.Level1) +{ + std::string cmd = "SP_daemon -N 1 -fds -PID 88888888"; // 88888888 is not exit + std::string result = ""; + bool ret = SPUtils::LoadCmd(cmd, result); + EXPECT_EQ(ret, true); + ret = VerifResult(result); + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: FdsTestCase04 + * @tc.desc: Test Fds + * @tc.type: FUNC + */ +HWTEST_F(SPdaemonFdsTest, FdsTestCase04, TestSize.Level1) +{ + FileDescriptor &fdsInstance = FileDescriptor::GetInstance(); + std::string packName = "init"; + fdsInstance.SetPackageName(packName); + fdsInstance.SetProcessId("1"); + std::map fdsItemData = fdsInstance.ItemData(); + std::string fdTotal = fdsItemData["fdTotal"]; + std::string fds = fdsItemData["fds"]; + EXPECT_EQ(fdTotal.empty(), false); + EXPECT_EQ(fds.empty(), false); +} +} // namespace OHOS +} // namespace SmartPerf \ No newline at end of file -- Gitee From 5d1050f4d3a2153258b9be2e0d2d7eac98b1e886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=9B=E5=9B=BD=E4=BA=AE?= Date: Thu, 11 Sep 2025 11:16:51 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=93=9D=E9=BB=84?= =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 牛国亮 --- host/smartperf/client/client_command/cpu_info.cpp | 2 +- host/smartperf/client/client_command/include/cpu_info.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/host/smartperf/client/client_command/cpu_info.cpp b/host/smartperf/client/client_command/cpu_info.cpp index bd2e26020..18624927b 100644 --- a/host/smartperf/client/client_command/cpu_info.cpp +++ b/host/smartperf/client/client_command/cpu_info.cpp @@ -64,7 +64,7 @@ std::map CPUInfo::ItemData() return result; } -void CPUInfo::CalculateCPIInfo(const std::string line, double &cpi_Total, size_t &cpi_Count) +void CPUInfo::CalculateCPIInfo(const std::string line, double &cpi_total, size_t &cpi_count) { auto trim = [](std::string& s) { s.erase(0, s.find_first_not_of(" \t")); diff --git a/host/smartperf/client/client_command/include/cpu_info.h b/host/smartperf/client/client_command/include/cpu_info.h index 95212e5bf..53e9c378a 100644 --- a/host/smartperf/client/client_command/include/cpu_info.h +++ b/host/smartperf/client/client_command/include/cpu_info.h @@ -41,7 +41,7 @@ public: void Start(); void Stop(); private: - void CalculateCPIInfo(const std::string line, double &cpi_Total, size_t &cpi_Count); + void CalculateCPIInfo(const std::string line, double &cpi_total, size_t &cpi_count); std::vector pids_; std::thread th_; std::string hiperfCmd_ = "/bin/hiperf stat -e " + SPUtils::GetProductName() + "-instructions," + -- Gitee From 116776deeee99077dc5d282faa9d4497b6970ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=9B=E5=9B=BD=E4=BA=AE?= Date: Thu, 11 Sep 2025 13:40:27 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=93=9D=E9=BB=84?= =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 牛国亮 --- host/smartperf/client/client_command/cpu_info.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/host/smartperf/client/client_command/cpu_info.cpp b/host/smartperf/client/client_command/cpu_info.cpp index 18624927b..45091f591 100644 --- a/host/smartperf/client/client_command/cpu_info.cpp +++ b/host/smartperf/client/client_command/cpu_info.cpp @@ -78,8 +78,8 @@ void CPUInfo::CalculateCPIInfo(const std::string line, double &cpi_total, size_t if (cpi_pos != std::string::npos) { size_t number_end = comment.find(" ", 0); std::string cpi_str = comment.substr(0, number_end); - cpi_Total += SPUtilesTye::StringToSometype(cpi_str); - ++cpi_Count; + cpi_total += SPUtilesTye::StringToSometype(cpi_str); + ++cpi_count; } } } -- Gitee