1 2 3/* 4 * Copyright (C) 2022 Huawei Device Co., Ltd. 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18import { HeapLoader } from '../../../src/js-heap/logic/HeapLoader'; 19import { ConstructorItem, FileType } from '../../../src/js-heap/model/UiStruct'; 20 21jest.mock('../../../src/js-heap/model/DatabaseStruct', () => ({ 22 DetachedNessState: { 23 UNKNOWN: 0, 24 ATTACHED: 1, 25 DETACHED: 2, 26 }, 27 NodeType: { 28 HIDDEN : 0, 29 ARRAY : 1, 30 STRING : 2, 31 OBJECT : 3, 32 CODE : 4, 33 CLOSURE : 5, 34 REGEXP : 6, 35 NUMBER : 7, 36 NATIVE : 8, 37 SYNTHETIC : 9, 38 CONCATENATED_STRING : 10, 39 SLICED_STRING : 11, 40 SYMBOL : 12, 41 BIGINT : 13, 42 OBJECT_SHAPE : 14, 43 }, 44 EdgeType: { 45 CONTEXT : 0, 46 ELEMENT : 1, 47 PROPERTY : 2, 48 INTERNAL : 3, 49 HIDDEN : 4, 50 SHORTCUT : 5, 51 WEAK : 6, 52 STRING_OR_NUMBER : 6, 53 NODE : 7, 54 INVISIBLE : 8, 55 } 56})) 57 58jest.mock('../../../src/js-heap/utils/Utils', () => { 59 return { 60 HeapNodeToConstructorItem: (node: any) => { 61 return {}; 62 }, 63 }; 64}); 65 66describe('HeapLoader Test', () => { 67 let rootNode = { 68 detachedness: 7, 69 displayName: '', 70 distance: 13200000, 71 edgeCount: 215, 72 fileId: 1, 73 firstEdgeIndex: 0, 74 flag: 1, 75 id: 24, 76 name: 'Test', 77 nodeIndex: 0, 78 nodeOldIndex: 0, 79 retainedSize: 728167, 80 retainsCount: 0, 81 retainsEdgeIdx: [2], 82 retainsNodeIdx: [0,2], 83 selfSize: 0, 84 traceNodeId: 0, 85 type: 9, 86 edges: [ 87 { 88 edgeIndex: 43, 89 edgeOldIndex: 3, 90 fromNodeId: 1, 91 nameOrIndex: '-test-', 92 nodeId: 15436, 93 retainEdge: [], 94 retainsNode: [], 95 toNodeId: 43427, 96 type: 5, 97 }, 98 { 99 edgeIndex: 15, 100 edgeOldIndex: 53, 101 fromNodeId: 15, 102 nameOrIndex: '-test-', 103 nodeId: 76, 104 retainEdge: [], 105 retainsNode: [], 106 toNodeId: 23, 107 type: 5, 108 }, 109 ], 110 }; 111 let data = { 112 end_ts: 2206, 113 id: 29, 114 isParseSuccess: true, 115 name: 'Test', 116 path: '', 117 pid: 423, 118 tart_ts: 83693464, 119 type: 0, 120 heapLoader: { 121 rootNode: rootNode, 122 }, 123 snapshotStruct: { 124 traceNodes: [], 125 nodeMap: new Map(), 126 nodeCount: 1, 127 edges: [ 128 { 129 edgeIndex: 1, 130 edgeOldIndex: 0, 131 fromNodeId: 5, 132 nameOrIndex: 152, 133 nodeId: 1436, 134 retainEdge: [], 135 retainsNode: [], 136 toNodeId: 4347, 137 type: 15, 138 }, 139 { 140 edgeIndex: 31, 141 edgeOldIndex: 33, 142 fromNodeId: 1, 143 nameOrIndex: 958, 144 nodeId: 344, 145 retainEdge: [], 146 retainsNode: [], 147 toNodeId: 44405, 148 type: 5, 149 }, 150 ], 151 samples: [], 152 }, 153 }; 154 155 let item = { 156 addedCount: 1, 157 addedIndx: [30, 326], 158 addedSize: 316, 159 childCount: 1296, 160 children: [], 161 classChildren: [], 162 deletedIdx: [9, 338], 163 deltaCount: 0, 164 deltaSize: -16, 165 distance: -1, 166 edgeCount: 0, 167 edgeName: '', 168 fileId: 0, 169 hasNext: true, 170 id: 30, 171 index: 0, 172 isAdd: false, 173 isHover: false, 174 isSelected: false, 175 nextId: [], 176 nodeName: 'SourceTextModule', 177 objectName: 'SourceTextModule@521', 178 removedCount: 28, 179 removedSize: 332, 180 retainedSize: 32, 181 retains: [], 182 shallowSize: 21, 183 showBox: false, 184 showCut: false, 185 status: true, 186 targetFileId: 211, 187 traceNodeId: 34, 188 type: 4, 189 }; 190 191 it('HeapLoaderTest01', () => { 192 let heapLoader = new HeapLoader(data); 193 heapLoader.fileId = jest.fn(() => true); 194 expect(heapLoader).not.toBeUndefined(); 195 }); 196 it('HeapLoaderTest02', () => { 197 let heapLoader = new HeapLoader(data); 198 expect(heapLoader.loadAllocationParent({})).toBeUndefined(); 199 }); 200 201 it('HeapLoaderTest03', () => { 202 let heapLoader = new HeapLoader(data); 203 heapLoader.rootNode = rootNode; 204 heapLoader.nodes = [rootNode]; 205 heapLoader.nodes[0].addEdge = jest.fn(() => true); 206 heapLoader.isEssentialEdge = jest.fn(() => false); 207 expect(heapLoader.preprocess()).toBeUndefined(); 208 }); 209 210 it('HeapLoaderTest04', () => { 211 let heapLoader = new HeapLoader(data); 212 heapLoader.nodes = [rootNode]; 213 expect(heapLoader.getClassesForSummary(1,2)).toBeTruthy(); 214 }); 215 216 it('HeapLoaderTest05', () => { 217 let heapLoader = new HeapLoader(data); 218 heapLoader.nodes = [rootNode]; 219 heapLoader.rootNode = rootNode; 220 expect(heapLoader.getRetains(item)).toBeDefined(); 221 }); 222 it('HeapLoaderTest06', () => { 223 let heapLoader = new HeapLoader(data); 224 expect(heapLoader.getAllocationFunctionList()).toStrictEqual([]); 225 }); 226 it('HeapLoaderTest07', () => { 227 let heapLoader = new HeapLoader(data); 228 expect(heapLoader.getAllocationStack(1)).toStrictEqual([]); 229 }); 230 it('HeapLoaderTest08', () => { 231 let heapLoader = new HeapLoader(data); 232 expect(heapLoader.getFunctionNodeIds(1)).toStrictEqual([]); 233 }); 234 it('HeapLoaderTest09', () => { 235 let heapLoader = new HeapLoader(data); 236 expect(heapLoader.calDistances()).toBeUndefined(); 237 }); 238 it('HeapLoaderTest10', () => { 239 let heapLoader = new HeapLoader(data); 240 heapLoader.buildOrderIdxAndDominateTree = jest.fn(()=>{true}); 241 expect(heapLoader.calRetainedSize()).toBeUndefined(); 242 }); 243 it('HeapLoaderTest11', () => { 244 let heapLoader = new HeapLoader(data); 245 heapLoader.buildDominatedNode = jest.fn(()=>{true}); 246 expect(heapLoader.buildDominatedNode()).toStrictEqual(); 247 }); 248 it('HeapLoaderTest12', () => { 249 let heapLoader = new HeapLoader(data); 250 expect(heapLoader.buildSamples()).toBeUndefined(); 251 }); 252 it('HeapLoaderTest13', () => { 253 let heapLoader = new HeapLoader(data); 254 let samples = [ 255 {length: 1}, 256 {mid: 1} 257 ] 258 expect(heapLoader.binarySearchNodeInSamples(1, samples)).toBe(0); 259 }); 260 it('HeapLoaderTest14', () => { 261 let heapLoader = new HeapLoader(data); 262 let node = { 263 index:1, 264 nodesToVisitLen:1 265 } 266 let edge = { 267 nodesToVisitLen:3, 268 } 269 expect(heapLoader.bfs(node,edge)).toBeUndefined(); 270 }); 271 it('HeapLoaderTest15', () => { 272 let heapLoader = new HeapLoader(data); 273 expect(heapLoader.markPageOwnedNodes()).toBeUndefined(); 274 }); 275 it('HeapLoaderTest16', () => { 276 let heapLoader = new HeapLoader(data); 277 let node = { 278 nodeIndex:1 279 } 280 expect(heapLoader.hasOnlyWeakRetainers(node)).toBe(true); 281 }); 282 it('HeapLoaderTest17', () => { 283 let heapLoader = new HeapLoader(data); 284 let targetClass = { 285 fileId:1, 286 nodeName:'', 287 childCount:2, 288 classChildren:[], 289 } 290 let baseClass = { 291 childCount:1, 292 classChildren:[] 293 } 294 heapLoader.calClassDiff = jest.fn(()=>{true}) 295 expect(heapLoader.calClassDiff(targetClass,baseClass)).toBeUndefined(); 296 }); 297 it('HeapLoaderTest18', () => { 298 let heapLoader = new HeapLoader(data); 299 let item = { 300 children:[{length: 1}], 301 index:1, 302 childCount:1, 303 edgeName:'', 304 hasNext:true, 305 traceNodeId:1, 306 type:0, 307 parent:{}, 308 id:1 309 } 310 expect(heapLoader.getNextNode(item)).toStrictEqual([{"length": 1}]); 311 }); 312 it('HeapLoaderTest19', () => { 313 let heapLoader = new HeapLoader(data); 314 let item = { 315 retains:[{length: 1}], 316 index:1, 317 childCount:1, 318 edgeName:'', 319 hasNext:true, 320 traceNodeId:1, 321 type:0, 322 parent:{}, 323 id:1 324 } 325 expect(heapLoader.getRetains(item)).toStrictEqual([{"length": 1}]); 326 }); 327 it('HeapLoaderTest20', () => { 328 let heapLoader = new HeapLoader(data); 329 let node = { 330 index:1, 331 nodesToVisitLen:1 332 } 333 let edge = { 334 nodesToVisitLen:3, 335 } 336 expect(heapLoader.filterForBpf(node,edge)).toBe(true); 337 }); 338 it('HeapLoaderTest21', () => { 339 let heapLoader = new HeapLoader(data); 340 let datas = { 341 visited:[ 342 {node:{nodeIndex:1}}, 343 ], 344 attached:[], 345 detached:[], 346 } 347 let node = { 348 nodeIndex:1, 349 type:1, 350 detachedness:1, 351 id:1, 352 displayName: 'Detached ', 353 name:'', 354 flag:1 355 } 356 expect(heapLoader.processNode(datas,node,1)).toBeUndefined(); 357 }); 358 it('HeapLoaderTest22', () => { 359 let heapLoader = new HeapLoader(data); 360 expect(heapLoader.allocation).toBeTruthy(); 361 }); 362 it('HeapLoaderTest23', () => { 363 let heapLoader = new HeapLoader(data); 364 expect(heapLoader.getAllocation()).toBeTruthy(); 365 }); 366}); 367