| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | |
| 5 /** | 4 /** |
| 6 * @constructor | 5 * @unrestricted |
| 7 * @param {!RuntimeAgent.CallFrame} callFrame | |
| 8 */ | 6 */ |
| 9 WebInspector.ProfileNode = function(callFrame) | 7 WebInspector.ProfileNode = class { |
| 10 { | 8 /** |
| 9 * @param {!RuntimeAgent.CallFrame} callFrame |
| 10 */ |
| 11 constructor(callFrame) { |
| 11 /** @type {!RuntimeAgent.CallFrame} */ | 12 /** @type {!RuntimeAgent.CallFrame} */ |
| 12 this.callFrame = callFrame; | 13 this.callFrame = callFrame; |
| 13 /** @type {string} */ | 14 /** @type {string} */ |
| 14 this.callUID = `${this.callFrame.functionName}@${this.callFrame.scriptId}:${
this.callFrame.lineNumber}`; | 15 this.callUID = `${this.callFrame.functionName}@${this.callFrame.scriptId}:${
this.callFrame.lineNumber}`; |
| 15 /** @type {number} */ | 16 /** @type {number} */ |
| 16 this.self = 0; | 17 this.self = 0; |
| 17 /** @type {number} */ | 18 /** @type {number} */ |
| 18 this.total = 0; | 19 this.total = 0; |
| 19 /** @type {number} */ | 20 /** @type {number} */ |
| 20 this.id = 0; | 21 this.id = 0; |
| 21 /** @type {?WebInspector.ProfileNode} */ | 22 /** @type {?WebInspector.ProfileNode} */ |
| 22 this.parent = null; | 23 this.parent = null; |
| 23 /** @type {!Array<!WebInspector.ProfileNode>} */ | 24 /** @type {!Array<!WebInspector.ProfileNode>} */ |
| 24 this.children = []; | 25 this.children = []; |
| 25 }; | 26 } |
| 26 | 27 |
| 27 WebInspector.ProfileNode.prototype = { | 28 /** |
| 28 /** | 29 * @return {string} |
| 29 * @return {string} | 30 */ |
| 30 */ | 31 get functionName() { |
| 31 get functionName() | 32 return this.callFrame.functionName; |
| 32 { | 33 } |
| 33 return this.callFrame.functionName; | |
| 34 }, | |
| 35 | 34 |
| 36 /** | 35 /** |
| 37 * @return {string} | 36 * @return {string} |
| 38 */ | 37 */ |
| 39 get scriptId() | 38 get scriptId() { |
| 40 { | 39 return this.callFrame.scriptId; |
| 41 return this.callFrame.scriptId; | 40 } |
| 42 }, | |
| 43 | 41 |
| 44 /** | 42 /** |
| 45 * @return {string} | 43 * @return {string} |
| 46 */ | 44 */ |
| 47 get url() | 45 get url() { |
| 48 { | 46 return this.callFrame.url; |
| 49 return this.callFrame.url; | 47 } |
| 50 }, | |
| 51 | 48 |
| 52 /** | 49 /** |
| 53 * @return {number} | 50 * @return {number} |
| 54 */ | 51 */ |
| 55 get lineNumber() | 52 get lineNumber() { |
| 56 { | 53 return this.callFrame.lineNumber; |
| 57 return this.callFrame.lineNumber; | 54 } |
| 58 }, | |
| 59 | 55 |
| 60 /** | 56 /** |
| 61 * @return {number} | 57 * @return {number} |
| 62 */ | 58 */ |
| 63 get columnNumber() | 59 get columnNumber() { |
| 64 { | 60 return this.callFrame.columnNumber; |
| 65 return this.callFrame.columnNumber; | 61 } |
| 66 } | |
| 67 }; | 62 }; |
| 68 | 63 |
| 69 /** | 64 /** |
| 70 * @constructor | 65 * @unrestricted |
| 71 */ | 66 */ |
| 72 WebInspector.ProfileTreeModel = function() | 67 WebInspector.ProfileTreeModel = class { |
| 73 { | 68 /** |
| 69 * @param {!WebInspector.ProfileNode} root |
| 70 * @protected |
| 71 */ |
| 72 initialize(root) { |
| 73 this.root = root; |
| 74 this._assignDepthsAndParents(); |
| 75 this.total = this._calculateTotals(this.root); |
| 76 } |
| 77 |
| 78 _assignDepthsAndParents() { |
| 79 var root = this.root; |
| 80 root.depth = -1; |
| 81 root.parent = null; |
| 82 this.maxDepth = 0; |
| 83 var nodesToTraverse = [root]; |
| 84 while (nodesToTraverse.length) { |
| 85 var parent = nodesToTraverse.pop(); |
| 86 var depth = parent.depth + 1; |
| 87 if (depth > this.maxDepth) |
| 88 this.maxDepth = depth; |
| 89 var children = parent.children; |
| 90 var length = children.length; |
| 91 for (var i = 0; i < length; ++i) { |
| 92 var child = children[i]; |
| 93 child.depth = depth; |
| 94 child.parent = parent; |
| 95 if (child.children.length) |
| 96 nodesToTraverse.push(child); |
| 97 } |
| 98 } |
| 99 } |
| 100 |
| 101 /** |
| 102 * @param {!WebInspector.ProfileNode} root |
| 103 * @return {number} |
| 104 */ |
| 105 _calculateTotals(root) { |
| 106 var nodesToTraverse = [root]; |
| 107 var dfsList = []; |
| 108 while (nodesToTraverse.length) { |
| 109 var node = nodesToTraverse.pop(); |
| 110 node.total = node.self; |
| 111 dfsList.push(node); |
| 112 nodesToTraverse.push(...node.children); |
| 113 } |
| 114 while (dfsList.length > 1) { |
| 115 var node = dfsList.pop(); |
| 116 node.parent.total += node.total; |
| 117 } |
| 118 return root.total; |
| 119 } |
| 74 }; | 120 }; |
| 75 | |
| 76 WebInspector.ProfileTreeModel.prototype = { | |
| 77 /** | |
| 78 * @param {!WebInspector.ProfileNode} root | |
| 79 * @protected | |
| 80 */ | |
| 81 initialize: function(root) | |
| 82 { | |
| 83 this.root = root; | |
| 84 this._assignDepthsAndParents(); | |
| 85 this.total = this._calculateTotals(this.root); | |
| 86 }, | |
| 87 | |
| 88 _assignDepthsAndParents: function() | |
| 89 { | |
| 90 var root = this.root; | |
| 91 root.depth = -1; | |
| 92 root.parent = null; | |
| 93 this.maxDepth = 0; | |
| 94 var nodesToTraverse = [root]; | |
| 95 while (nodesToTraverse.length) { | |
| 96 var parent = nodesToTraverse.pop(); | |
| 97 var depth = parent.depth + 1; | |
| 98 if (depth > this.maxDepth) | |
| 99 this.maxDepth = depth; | |
| 100 var children = parent.children; | |
| 101 var length = children.length; | |
| 102 for (var i = 0; i < length; ++i) { | |
| 103 var child = children[i]; | |
| 104 child.depth = depth; | |
| 105 child.parent = parent; | |
| 106 if (child.children.length) | |
| 107 nodesToTraverse.push(child); | |
| 108 } | |
| 109 } | |
| 110 }, | |
| 111 | |
| 112 /** | |
| 113 * @param {!WebInspector.ProfileNode} root | |
| 114 * @return {number} | |
| 115 */ | |
| 116 _calculateTotals: function(root) | |
| 117 { | |
| 118 var nodesToTraverse = [root]; | |
| 119 var dfsList = []; | |
| 120 while (nodesToTraverse.length) { | |
| 121 var node = nodesToTraverse.pop(); | |
| 122 node.total = node.self; | |
| 123 dfsList.push(node); | |
| 124 nodesToTraverse.push(...node.children); | |
| 125 } | |
| 126 while (dfsList.length > 1) { | |
| 127 var node = dfsList.pop(); | |
| 128 node.parent.total += node.total; | |
| 129 } | |
| 130 return root.total; | |
| 131 } | |
| 132 }; | |
| OLD | NEW |