1"use strict"; 2/* 3 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16Object.defineProperty(exports, "__esModule", { value: true }); 17exports.KoalaProfiler = void 0; 18/** 19 * Adds statistics for constructing/disposing of the TreeNode instances. 20 * It is disabled by default because collecting such data affects performance. 21 */ 22const DEBUG_WITH_NODE_STATS = false; 23class KoalaProfiler { 24 constructor() { 25 this.invalidations = 0; 26 this.computes = 0; 27 this.builds = 0; 28 this.nodes = 0; 29 this.realDraws = 0; 30 this.cachedDraws = 0; 31 this.measures = 0; 32 this.layouts = 0; 33 this.frames = 0; 34 this.lastTime = 0.0; 35 this.lastFPS = 0; 36 this.updateEnterTime = 0.0; 37 this.updateExitTime = 0.0; 38 this.updateTime = 0.0; 39 this.buildEnterTime = 0.0; 40 this.buildExitTime = 0.0; 41 this.buildTime = 0.0; 42 this.layoutEnterTime = 0.0; 43 this.layoutExitTime = 0.0; 44 this.layoutTime = 0.0; 45 this.drawEnterTime = 0.0; 46 this.drawExitTime = 0.0; 47 this.drawTime = 0.0; 48 this.updatableStates = 0; 49 this.mutableStates = 0; 50 this.computableValues = 0; 51 } 52 static nodeCreated(nodeType, node) { 53 if (KoalaProfiler.map === undefined) 54 return; 55 let set = KoalaProfiler.map.get(nodeType); 56 if (set === undefined) { 57 set = new Set(); 58 KoalaProfiler.map.set(nodeType, set); 59 } 60 set.add(node); 61 } 62 static nodeDisposed(nodeType, node) { 63 if (KoalaProfiler.map === undefined) 64 return; 65 let set = KoalaProfiler.map.get(nodeType); 66 if (set === undefined) 67 throw new Error("node never existed"); 68 if (!set.delete(node)) 69 console.log("node is already disposed"); 70 } 71 static enable() { 72 KoalaProfiler.counters = new KoalaProfiler(); 73 } 74 static disable() { 75 KoalaProfiler.counters = undefined; 76 } 77 static enabled() { 78 return KoalaProfiler.counters != undefined; 79 } 80 reset() { 81 this.invalidations = 0; 82 this.computes = 0; 83 this.builds = 0; 84 this.nodes = 0; 85 this.realDraws = 0; 86 this.cachedDraws = 0; 87 this.layouts = 0; 88 this.measures = 0; 89 this.updateEnterTime = 0; 90 this.updateExitTime = 0; 91 this.updatableStates = 0; 92 this.mutableStates = 0; 93 this.computableValues = 0; 94 } 95 report() { 96 console.log(this.getReport()); 97 } 98 getReport() { 99 var _a; 100 const updateTime = Math.round(1000 * (this.updateExitTime - this.updateEnterTime)); 101 const buildTime = Math.round(1000 * (this.buildExitTime - this.buildEnterTime)); 102 const layoutTime = Math.round(1000 * (this.layoutExitTime - this.layoutEnterTime)); 103 const drawTime = Math.round(1000 * (this.drawExitTime - this.drawEnterTime)); 104 if (this.updateTime < updateTime) 105 this.updateTime = updateTime; 106 if (this.buildTime < buildTime) 107 this.buildTime = buildTime; 108 if (this.layoutTime < layoutTime) 109 this.layoutTime = layoutTime; 110 if (this.drawTime < drawTime) 111 this.drawTime = drawTime; 112 // TODO: OHOS does not properly handle \n in template literals 113 const array = Array.of(`invalidations: ${this.invalidations}`, `modified states: ${this.mutableStates}/${this.updatableStates} + ${this.computableValues}`, `update states (mks): ${this.updateTime} / ${updateTime}`, `build root node (mks): ${this.buildTime} / ${buildTime}`, `layout view (mks): ${this.layoutTime} / ${layoutTime}`, `draw view (mks): ${this.drawTime} / ${drawTime}`, `computes: ${this.computes}`, `builds: ${this.builds}`, `nodes: ${this.nodes}`, `realDraws: ${this.realDraws}`, `cachedDraws: ${this.cachedDraws}`, `measures: ${this.measures}`, `layouts: ${this.layouts}`, `FPS: ${this.lastFPS}`); 114 (_a = KoalaProfiler.map) === null || _a === void 0 ? void 0 : _a.forEach((set, kind) => { 115 if (set.size > 0) 116 array.push(kind + ":" + set.size); 117 }); 118 return array.join("\n"); 119 } 120 invalidation() { this.invalidations++; } 121 compute() { this.computes++; } 122 build() { this.builds++; } 123 node() { this.nodes++; } 124 realDraw() { this.realDraws++; } 125 cachedDraw() { this.cachedDraws++; } 126 layout() { this.layouts++; } 127 measure() { this.measures++; } 128 frame(ms) { 129 if (ms - this.lastTime <= 1000) { 130 this.frames++; 131 } 132 else { 133 this.lastFPS = Math.round(this.frames * 1000 / (ms - this.lastTime)); 134 this.frames = 1; 135 this.lastTime = ms; 136 } 137 } 138 buildRootEnter() { 139 this.buildEnterTime = Date.now(); 140 } 141 buildRootExit() { 142 this.buildExitTime = Date.now(); 143 } 144 layoutEnter() { 145 this.layoutEnterTime = Date.now(); 146 } 147 layoutExit() { 148 this.layoutExitTime = Date.now(); 149 } 150 drawEnter() { 151 this.drawEnterTime = Date.now(); 152 } 153 drawExit() { 154 this.drawExitTime = Date.now(); 155 } 156 updateSnapshotEnter() { 157 this.updateEnterTime = Date.now(); 158 } 159 updateSnapshotExit() { 160 this.updateExitTime = Date.now(); 161 } 162 updateSnapshot(modified, all) { 163 if (all === undefined) { 164 this.computableValues = modified - this.mutableStates; 165 } 166 else { 167 this.mutableStates = modified; 168 this.updatableStates = all; 169 } 170 } 171} 172exports.KoalaProfiler = KoalaProfiler; 173KoalaProfiler.map = DEBUG_WITH_NODE_STATS 174 ? new Map() 175 : undefined; 176KoalaProfiler.counters = undefined; 177//# sourceMappingURL=KoalaProfiler.js.map