1 /* 2 * Copyright (C) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <gtest/gtest.h> 16 #include <iostream> 17 #include <sstream> 18 #include <thread> 19 #include <chrono> 20 #include "unistd.h" 21 #include "sp_utils.h" 22 #include "ByTrace.h" 23 #include "common.h" 24 25 using namespace testing::ext; 26 using namespace std; 27 28 namespace OHOS { 29 namespace SmartPerf { 30 class ByTraceTest : public testing::Test { 31 public: SetUpTestCase()32 static void SetUpTestCase() {} TearDownTestCase()33 static void TearDownTestCase() {} 34 SetUp()35 void SetUp() {} TearDown()36 void TearDown() {} 37 }; 38 39 HWTEST_F(ByTraceTest, ThreadGetTraceTest, TestSize.Level1) 40 { 41 ByTrace &byTrace = ByTrace::GetInstance(); 42 std::string result; 43 std::string cmdString; 44 if (SPUtils::IsHmKernel()) { 45 cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_1024); 46 } else { 47 cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_2048); 48 } 49 std::string time = std::to_string(SPUtils::GetCurTime()); 50 std::string traceFile = "/data/local/tmp/sptrace_" + time + ".ftrace"; 51 std::string traceCmdExe = cmdString + traceFile; 52 auto ret = SPUtils::LoadCmd(traceCmdExe, result); 53 byTrace.ThreadGetTrace(); 54 55 EXPECT_EQ(ret, true); 56 } 57 58 HWTEST_F(ByTraceTest, CheckFpsJittersTest, TestSize.Level1) 59 { 60 ByTrace &byTrace = ByTrace::GetInstance(); 61 int times = 3; 62 int two = 2; 63 int curNum = 5; 64 int sum = 6; 65 long long currentTrigger = -1; 66 long long lastTriggerTime = -1; 67 int interval = 10000; 68 long long curTime = SPUtils::GetCurTime(); 69 std::vector<long long> jitters = {1000000, 2000000, 3000000}; 70 long long threshold = 22; 71 int cfps = 30; 72 int lowfps = 50; 73 if (curNum <= sum && currentTrigger < 0 && times > two) { 74 for (size_t i = 0; i < jitters.size(); i++) { 75 long long normalJitter = jitters[i] / 1e6; 76 if (normalJitter > threshold || cfps < lowfps) { 77 byTrace.TriggerCatch(curTime); 78 } 79 } 80 } 81 if ((curTime - lastTriggerTime) / 1e3 > interval && currentTrigger == 1) { 82 currentTrigger = -1; 83 } 84 TraceStatus result = byTrace.CheckFpsJitters(jitters, cfps); 85 86 EXPECT_EQ(result, TraceStatus::TRACE_FINISH); 87 } 88 89 HWTEST_F(ByTraceTest, TriggerCatchTest, TestSize.Level1) 90 { 91 ByTrace &byTrace = ByTrace::GetInstance(); 92 long long lastTriggerTime = SPUtils::GetCurTime(); 93 usleep(2); 94 int curNum = 0; 95 int curTime = 0; 96 int interval = 2000; 97 long long currentTrigger = -1; 98 curTime = SPUtils::GetCurTime(); 99 if ((curTime - lastTriggerTime) / 1e3 > interval && !byTrace.CheckHitraceId()) { __anon08e4268c0102null100 std::thread tStart([&byTrace] { byTrace.ThreadGetTrace(); }); 101 currentTrigger = 1; 102 lastTriggerTime = curTime; 103 curNum++; 104 tStart.detach(); 105 } 106 bool haveHitraceId = byTrace.CheckHitraceId(); 107 108 EXPECT_EQ(haveHitraceId, true); 109 } 110 111 HWTEST_F(ByTraceTest, CheckHitraceIdTest, TestSize.Level1) 112 { 113 std::string result; 114 bool hitraceProc = false; 115 std::string hitrace = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_CMD); 116 SPUtils::LoadCmd(hitrace, result); 117 if (result.empty()) { 118 hitraceProc = false; 119 } 120 if (result.find("-t") != std::string::npos) { 121 hitraceProc = true; 122 } 123 hitraceProc = false; 124 125 ASSERT_FALSE(hitraceProc); 126 } 127 } 128 }