diff --git a/smartperf_device/device_command/test/unittest/bytrace_test.cpp b/smartperf_device/device_command/test/unittest/bytrace_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5953d47fd9c71561397be22cc41b858699b1092d --- /dev/null +++ b/smartperf_device/device_command/test/unittest/bytrace_test.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2025 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 +#include +#include "unistd.h" +#include "sp_utils.h" +#include "ByTrace.h" +#include "common.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace SmartPerf { +class ByTraceTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + void SetUp() {} + void TearDown() {} +}; + +HWTEST_F(ByTraceTest, ThreadGetTraceTest, TestSize.Level1) +{ + ByTrace &byTrace = ByTrace::GetInstance(); + std::string result; + std::string cmdString; + if (SPUtils::IsHmKernel()) { + cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_1024); + } else { + cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_2048); + } + std::string time = std::to_string(SPUtils::GetCurTime()); + std::string traceFile = "/data/local/tmp/sptrace_" + time + ".ftrace"; + std::string traceCmdExe = cmdString + traceFile; + auto ret = SPUtils::LoadCmd(traceCmdExe, result); + byTrace.ThreadGetTrace(); + + EXPECT_EQ(ret, true); +} + +HWTEST_F(ByTraceTest, CheckFpsJittersTest, TestSize.Level1) +{ + ByTrace &byTrace = ByTrace::GetInstance(); + int times = 3; + int two = 2; + int curNum = 5; + int sum = 6; + long long currentTrigger = -1; + long long lastTriggerTime = -1; + int interval = 10000; + long long curTime = SPUtils::GetCurTime(); + std::vector jitters = {1000000, 2000000, 3000000}; + long long threshold = 22; + int cfps = 30; + int lowfps = 50; + if (curNum <= sum && currentTrigger < 0 && times > two) { + for (size_t i = 0; i < jitters.size(); i++) { + long long normalJitter = jitters[i] / 1e6; + if (normalJitter > threshold || cfps < lowfps) { + byTrace.TriggerCatch(curTime); + } + } + } + if ((curTime - lastTriggerTime) / 1e3 > interval && currentTrigger == 1) { + currentTrigger = -1; + } + TraceStatus result = byTrace.CheckFpsJitters(jitters, cfps); + + EXPECT_EQ(result, TraceStatus::TRACE_FINISH); +} + +HWTEST_F(ByTraceTest, TriggerCatchTest, TestSize.Level1) +{ + ByTrace &byTrace = ByTrace::GetInstance(); + long long lastTriggerTime = SPUtils::GetCurTime(); + usleep(2); + int curNum = 0; + int curTime = 0; + int interval = 2000; + long long currentTrigger = -1; + curTime = SPUtils::GetCurTime(); + if ((curTime - lastTriggerTime) / 1e3 > interval && !byTrace.CheckHitraceId()) { + std::thread tStart([&byTrace] { byTrace.ThreadGetTrace(); }); + currentTrigger = 1; + lastTriggerTime = curTime; + curNum++; + tStart.detach(); + } + bool haveHitraceId = byTrace.CheckHitraceId(); + + EXPECT_EQ(haveHitraceId, true); +} + +HWTEST_F(ByTraceTest, CheckHitraceIdTest, TestSize.Level1) +{ + std::string result; + bool hitraceProc = false; + std::string hitrace = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_CMD); + SPUtils::LoadCmd(hitrace, result); + if (result.empty()) { + hitraceProc = false; + } + if (result.find("-t") != std::string::npos) { + hitraceProc = true; + } + hitraceProc = false; + + ASSERT_FALSE(hitraceProc); +} +} +} \ No newline at end of file diff --git a/smartperf_device/device_command/test/unittest/control_call_cmd_test.cpp b/smartperf_device/device_command/test/unittest/control_call_cmd_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..238695dc3f70733d7df27884743da632a5496aea --- /dev/null +++ b/smartperf_device/device_command/test/unittest/control_call_cmd_test.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2025 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 "unistd.h" +#include +#include +#include +#include +#include +#include +#include +#include "control_call_cmd.h" +#include "startup_delay.h" +#include "sp_utils.h" +#include "parse_click_complete_trace.h" +#include "parse_click_response_trace.h" +#include "parse_radar.h" +#include "parse_slide_fps_trace.h" +#include "sp_log.h" +#include "stalling_rate_trace.h" +#include "common.h" + + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace SmartPerf { +class ControlCallCmdTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + void SetUp() {} + void TearDown() {} + + bool isOhTest = false; + double time = 0.0; + double noNameType = -1.0; +}; + +HWTEST_F(ControlCallCmdTest, GetResultTest01, TestSize.Level1) +{ + ControlCallCmd cmd; + time = cmd.ResponseTime(); + std::vector v = {"responseTime"}; + EXPECT_EQ("", cmd.GetResult(v)); +} + +HWTEST_F(ControlCallCmdTest, GetResultTest02, TestSize.Level1) +{ + ControlCallCmd cmd; + std::vector v = {"completeTime"}; + time = cmd.CompleteTime(); + EXPECT_EQ("", cmd.GetResult(v)); +} + +HWTEST_F(ControlCallCmdTest, GetResultTest03, TestSize.Level1) +{ + ControlCallCmd cmd; + std::vector v = {"frameLoss"}; + EXPECT_EQ("", cmd.GetResult(v)); +} + +HWTEST_F(ControlCallCmdTest, GetResultTest04, TestSize.Level1) +{ + ControlCallCmd cmd; + std::vector v = {"appStartTime"}; + EXPECT_EQ("", cmd.GetResult(v)); +} + +HWTEST_F(ControlCallCmdTest, GetResultTest05, TestSize.Level1) +{ + ControlCallCmd cmd; + std::vector v = {"slideList"}; + EXPECT_EQ("", cmd.GetResult(v)); +} + +HWTEST_F(ControlCallCmdTest, GetResultTest06, TestSize.Level1) +{ + ControlCallCmd cmd; + std::vector v = {"timeDelay"}; + EXPECT_EQ("", cmd.GetResult(v)); +} + +HWTEST_F(ControlCallCmdTest, TimeDelayTest01, TestSize.Level1) +{ + OHOS::SmartPerf::ControlCallCmd controlCallCmd; + std::string expected = "ResponseTime:-1ms\nCompleteTime:-1ms\nHitchTimeRate:-1.00ms/s "; + std::string expectedResult = expected + "\nMAX_RENDER_SEQ_MISSED_FRAMES:-1"; + EXPECT_EQ(controlCallCmd.TimeDelay(), expectedResult); +} + +HWTEST_F(ControlCallCmdTest, SlideListTest01, TestSize.Level1) +{ + OHOS::SmartPerf::ControlCallCmd controlCallCmd; + std::string expected = "FPS:-1fps\nResponseTime:-1ms\nHitchTimeRate:-1.00ms/s "; + std::string expectedResult = expected + "\nMAX_RENDER_SEQ_MISSED_FRAMES:-1"; + std::string resultStream = controlCallCmd.SlideList(); + EXPECT_EQ(resultStream, expectedResult); +} + +HWTEST_F(ControlCallCmdTest, GetFrameTest01, TestSize.Level1) +{ + OHOS::SmartPerf::ControlCallCmd controlCallCmd; + std::string result = controlCallCmd.GetFrame(); + EXPECT_EQ(result, ""); +} + +HWTEST_F(ControlCallCmdTest, ResponseTimeTest01, TestSize.Level1) +{ + OHOS::SmartPerf::ControlCallCmd controlCallCmd; + EXPECT_DOUBLE_EQ(controlCallCmd.ResponseTime(), -1); +} + +HWTEST_F(ControlCallCmdTest, CompleteTimeTest01, TestSize.Level1) +{ + OHOS::SmartPerf::ControlCallCmd cmd; + double expectedTime = -1; + double result = cmd.CompleteTime(); + EXPECT_EQ(result, expectedTime); +} + +HWTEST_F(ControlCallCmdTest, IsohTest01, TestSize.Level1) +{ + OHOS::SmartPerf::ControlCallCmd controlCallCmd; + bool isOhTest = true; + std::vector v = {"", "", "ohtest"}; + controlCallCmd.IsohTest(v); + ASSERT_TRUE(isOhTest); +} + +HWTEST_F(ControlCallCmdTest, IsohTest02, TestSize.Level1) +{ + OHOS::SmartPerf::ControlCallCmd controlCallCmd; + bool isOhTest = false; + std::vector v = {"", "", "notohtest"}; + controlCallCmd.IsohTest(v); + ASSERT_FALSE(isOhTest); +} +} +} \ No newline at end of file diff --git a/smartperf_device/device_command/test/unittest/ddr_test.cpp b/smartperf_device/device_command/test/unittest/ddr_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2bf360ecf4a0ef45fdeb665524a3d40d8816da7 --- /dev/null +++ b/smartperf_device/device_command/test/unittest/ddr_test.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2025 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 "DDR.h" +#include "sp_utils.h" +#include "common.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace SmartPerf { +class DDRTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + void SetUp() {} + void TearDown() {} +}; + +HWTEST_F(DDRTest, ItemDataTest, TestSize.Level1) +{ + bool ret = false; + DDR &ddr = DDR::GetInstance(); + std::map result = DDR::GetInstance().ItemData(); + result["ddrFrequency"] = std::to_string(ddr.GetDdrFreq()); + if (result.find("ddrFrequency") != result.end() && result["ddrFrequency"].empty()) { + result["ddrFrequency"] = "NA"; + } + if (!result.empty()) { + ret = true; + } + + EXPECT_TRUE(ret); +} +} +} \ No newline at end of file diff --git a/smartperf_device/device_command/test/unittest/dubai_test.cpp b/smartperf_device/device_command/test/unittest/dubai_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..deb8fe6fc3072fd640f7669b00b67e052d8b1900 --- /dev/null +++ b/smartperf_device/device_command/test/unittest/dubai_test.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2025 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 "sp_utils.h" +#include "parse_radar.h" +#include +#include +#include +#include +#include "Dubai.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace SmartPerf { +class DubaiTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + void SetUp() {} + void TearDown() {} +}; + +HWTEST_F(DubaiTest, DubaiBeginTest, TestSize.Level1) +{ + Dubai &dubai = Dubai::GetInstance(); + std::string result; + auto ret = OHOS::SmartPerf::SPUtils::LoadCmd("hidumper -s 1213 -a '-b'", result); + dubai.DumpDubaiBegin(); + + EXPECT_EQ(ret, true); +} + +HWTEST_F(DubaiTest, DubaiFinishTest, TestSize.Level1) +{ + Dubai &dubai = Dubai::GetInstance(); + std::string result; + auto ret = OHOS::SmartPerf::SPUtils::LoadCmd("hidumper -s 1213 -a '-f'", result); + dubai.DumpDubaiFinish(); + + EXPECT_EQ(ret, true); +} + +HWTEST_F(DubaiTest, IsFileAccessible01, TestSize.Level1) +{ + std::string existingFile = "existing_file.txt"; + std::ofstream file(existingFile); + file << "Test content"; + file.close(); + bool result = Dubai::GetInstance().IsFileAccessible(existingFile); + EXPECT_TRUE(result); + std::remove(existingFile.c_str()); +} + +HWTEST_F(DubaiTest, IsFileAccessible02, TestSize.Level1) +{ + std::string nonExistingFile = "non_existing_file.txt"; + bool result = Dubai::GetInstance().IsFileAccessible(nonExistingFile); + EXPECT_FALSE(result); +} +} +} \ No newline at end of file diff --git a/smartperf_device/device_command/test/unittest/editor_command_test.cpp b/smartperf_device/device_command/test/unittest/editor_command_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59e3b90e324042ea3f7b95835a54cef29f950701 --- /dev/null +++ b/smartperf_device/device_command/test/unittest/editor_command_test.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2025 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 "unistd.h" +#include +#include +#include +#include +#include +#include +#include "editor_command.h" +#include "control_call_cmd.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace SmartPerf { +class EditorCommandTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + void SetUp() {} + void TearDown() {} +}; + +HWTEST_F(EditorCommandTest, EditorCommandTest01, TestSize.Level1) +{ + int argc = 2; + std::vector argv = {"arg1", "arg2"}; + EditorCommand editorCommand(argc, argv); + ASSERT_TRUE(true); +} +} +} \ No newline at end of file diff --git a/smartperf_device/device_command/test/unittest/filedescriptor_test.cpp b/smartperf_device/device_command/test/unittest/filedescriptor_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5a52da6f317db6fb74f6b4a7fbb4e7cb02d88e1 --- /dev/null +++ b/smartperf_device/device_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 diff --git a/smartperf_device/device_command/test/unittest/fps_test.cpp b/smartperf_device/device_command/test/unittest/fps_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c44748542b6bcca48214c6a0d1bfec80a36ab091 --- /dev/null +++ b/smartperf_device/device_command/test/unittest/fps_test.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2025 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 +#include +#include +#include +#include +#include +#include +#include "FPS.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace SmartPerf { +class SPdaemonFpsTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +/** + * @tc.name: FPS::SetFpsCurrentFpsTime + * @tc.desc: Test SetFpsCurrentFpsTime + * @tc.type: FUNC + */ +HWTEST_F(SPdaemonFpsTest, SetFpsCurrentFpsTimeTestCase001, TestSize.Level1) +{ + bool ret = false; + FpsInfo fpsInfoResult; + FPS::GetInstance().SetFpsCurrentFpsTime(fpsInfoResult); + FpsCurrentFpsTime fcf = FPS::GetInstance().GetFpsCurrentFpsTime(); + + if (fcf.currentFpsTime == 0) { + ret = true; + } + + EXPECT_TRUE(ret); +} + +/** + * @tc.name: FPS::GetFpsCurrentFpsTime + * @tc.desc: Test GetFpsCurrentFpsTime + * @tc.type: FUNC + */ +HWTEST_F(SPdaemonFpsTest, GetFpsCurrentFpsTimeTestCase001, TestSize.Level1) +{ + bool ret = false; + FpsCurrentFpsTime fcf = FPS::GetInstance().GetFpsCurrentFpsTime(); + + if (fcf.fps == 0 && fcf.currentFpsTime == 0) { + ret = true; + } + EXPECT_TRUE(ret); +} + +} +} \ No newline at end of file