1 /* 2 * Copyright (c) 2023 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 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_CHECK_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_CHECK_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <memory> 22 #include <string> 23 #include <unordered_map> 24 #include <vector> 25 26 #include "base/json/json_util.h" 27 #include "base/utils/macros.h" 28 #include "base/utils/noncopyable.h" 29 #include "bridge/common/utils/source_map.h" 30 31 namespace OHOS::Ace { 32 struct PerformanceCheckNode { 33 int32_t pageDepth = 0; // node depth 34 int32_t childrenSize = 0; // node children size 35 int32_t codeRow = 0; // js code row 36 int32_t codeCol = 0; // js code col 37 int64_t layoutTime = 0; // node layout time 38 int32_t flexLayouts = 0; // node flex layout times 39 bool isForEachItem = false; // foreach item 40 int32_t foreachItems = 0; // foreach item count 41 std::string nodeTag; // node tag 42 std::string pagePath; // page path 43 }; 44 45 struct CodeInfo { 46 int32_t row = 0; // ets code row 47 int32_t col = 0; // ets code col 48 std::string sources; // ets page 49 }; 50 51 using PerformanceCheckNodeMap = std::unordered_map<int32_t, PerformanceCheckNode>; 52 53 class ACE_EXPORT AcePerformanceCheck final { 54 public: 55 static void Start(); 56 static void Stop(); 57 58 private: 59 AcePerformanceCheck() = default; 60 ~AcePerformanceCheck() = default; 61 62 static std::unique_ptr<JsonValue> performanceInfo_; 63 64 friend class AceScopedPerformanceCheck; 65 ACE_DISALLOW_COPY_AND_MOVE(AcePerformanceCheck); 66 }; 67 68 class ACE_EXPORT AceScopedPerformanceCheck final { 69 public: 70 explicit AceScopedPerformanceCheck(const std::string& name); 71 ~AceScopedPerformanceCheck(); 72 73 static CodeInfo GetCodeInfo(int32_t row, int32_t col); 74 static void RecordPerformanceCheckData(const PerformanceCheckNodeMap& nodeMap, int64_t vsyncTimeout, 75 std::string path, std::string fromPath = "", std::string moduleName = "", bool isNavgation = false); 76 static void UpdateRecordPath(const std::string& path); 77 static void ReportAllRecord(); 78 private: 79 static std::string GetCurrentTime(); 80 static bool CheckIsRuleContainsPage(const std::string& ruleType, const std::string& pagePath); 81 static void RecordPageNodeCountAndDepth(int32_t pageNodeCount, int32_t pageDepth, 82 std::vector<PerformanceCheckNode>& pageNodeList, const CodeInfo& info, const std::string& pageRoute); 83 static void RecordForEachItemsCount( 84 int32_t count, std::unordered_map<int32_t, PerformanceCheckNode>& foreachNodeMap, 85 const CodeInfo& info, const std::string& pageRoute); 86 static void RecordFlexLayoutsCount( 87 const std::vector<PerformanceCheckNode>& nodeList, const CodeInfo& info, const std::string& pageRoute); 88 static void RecordVsyncTimeout( 89 const PerformanceCheckNodeMap& nodeMap, int64_t vsyncTimeout, 90 const CodeInfo& info, const std::string& pageRoute); 91 static bool CheckPage(const CodeInfo& codeInfo, const std::string& rule); 92 static void RecordFunctionTimeout(); 93 static RefPtr<Framework::RevSourceMap> GetCurrentSourceMap(); 94 static bool CheckIsRuleWebsocket(const std::string& ruleType); 95 int64_t markTime_ = 0; 96 std::string name_; 97 std::string pagePath_; 98 static std::string recordPath_; 99 static std::string currentPath_; 100 static std::vector<std::pair<int64_t, std::string>> records_; 101 ACE_DISALLOW_COPY_AND_MOVE(AceScopedPerformanceCheck); 102 }; 103 } // namespace OHOS::Ace 104 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_CHECK_H 105