ProfileTreeModel.js 1.6 KB

123456789101112131415
  1. export class ProfileNode{constructor(callFrame){this.callFrame=callFrame;this.callUID=`${callFrame.functionName}@${callFrame.scriptId}:${callFrame.lineNumber}:${callFrame.columnNumber}`;this.self=0;this.total=0;this.id=0;this.parent=null;this.children=[];}
  2. get functionName(){return this.callFrame.functionName;}
  3. get scriptId(){return this.callFrame.scriptId;}
  4. get url(){return this.callFrame.url;}
  5. get lineNumber(){return this.callFrame.lineNumber;}
  6. get columnNumber(){return this.callFrame.columnNumber;}}
  7. export default class ProfileTreeModel{constructor(target){this._target=target||null;}
  8. initialize(root){this.root=root;this._assignDepthsAndParents();this.total=this._calculateTotals(this.root);}
  9. _assignDepthsAndParents(){const root=this.root;root.depth=-1;root.parent=null;this.maxDepth=0;const nodesToTraverse=[root];while(nodesToTraverse.length){const parent=nodesToTraverse.pop();const depth=parent.depth+1;if(depth>this.maxDepth){this.maxDepth=depth;}
  10. const children=parent.children;const length=children.length;for(let i=0;i<length;++i){const child=children[i];child.depth=depth;child.parent=parent;if(child.children.length){nodesToTraverse.push(child);}}}}
  11. _calculateTotals(root){const nodesToTraverse=[root];const dfsList=[];while(nodesToTraverse.length){const node=nodesToTraverse.pop();node.total=node.self;dfsList.push(node);nodesToTraverse.push(...node.children);}
  12. while(dfsList.length>1){const node=dfsList.pop();node.parent.total+=node.total;}
  13. return root.total;}
  14. target(){return this._target;}}
  15. self.SDK=self.SDK||{};SDK=SDK||{};SDK.ProfileTreeModel=ProfileTreeModel;SDK.ProfileNode=ProfileNode;