diff --git a/host/smartperf/client/client_command/cpu_info.cpp b/host/smartperf/client/client_command/cpu_info.cpp index f4d8d32352f61fffdeb4adc4979e6369beb90b09..45091f591a7e10287245e7afcf29e3c3e597e631 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 428ce835a70873a3cac777cc2180d2333dfc50bc..53e9c378a74003a923d2f4d2d4e0eb81271134d8 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 4cc849b961ccac032be9250463e3d23a20cd813e..370e943a4843f145178131b9f142eff6c46b3d17 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 0000000000000000000000000000000000000000..e5a52da6f317db6fb74f6b4a7fbb4e7cb02d88e1 --- /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