@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 7.25 kB
JavaScript
import*as TraceEngine from"../../models/trace/trace.js";import{TimelineJSProfileProcessor}from"./TimelineJSProfile.js";import{RecordType,EventOnTimelineData,TimelineModelImpl}from"./TimelineModel.js";export class Node{totalTime;selfTime;id;event;parent;groupId;isGroupNodeInternal;depth;constructor(e,t){this.totalTime=0,this.selfTime=0,this.id=e,this.event=t,this.groupId="",this.isGroupNodeInternal=!1,this.depth=0}isGroupNode(){return this.isGroupNodeInternal}hasChildren(){throw"Not implemented"}setHasChildren(e){throw"Not implemented"}children(){throw"Not implemented"}searchTree(e,t){t=t||[],this.event&&e(this.event)&&t.push(this);for(const n of this.children().values())n.searchTree(e,t);return t}}export class TopDownNode extends Node{root;hasChildrenInternal;childrenInternal;parent;constructor(e,t,n){super(e,t),this.root=n&&n.root,this.hasChildrenInternal=!1,this.childrenInternal=null,this.parent=n}hasChildren(){return this.hasChildrenInternal}setHasChildren(e){this.hasChildrenInternal=e}children(){return this.childrenInternal||this.buildChildren()}buildChildren(){const e=[];for(let t=this;t.parent&&!t.isGroupNode();t=t.parent)e.push(t);e.reverse();const t=new Map,n=this,r=this.root;if(!r)return this.childrenInternal=t,this.childrenInternal;const i=r.startTime,s=r.endTime,o=r.doNotAggregate?function(t){++a,d===e.length&&a<=e.length+2&&p(t,0);--a}:void 0,l=r.doNotAggregate?void 0:_eventId,h=r.getEventGroupIdCallback();let a=0,d=0,c=null;function p(r,i){if(a===e.length+2){if(!c)return;return c.setHasChildren(!0),void(c.selfTime-=i)}let s,o="";l?(s=l(r),o=h?h(r):"",o&&(s+="/"+o)):s=Symbol("uniqueId");let d=t.get(s);d||(d=new TopDownNode(s,r,n),d.groupId=o,t.set(s,d)),d.selfTime+=i,d.totalTime+=i,c=d}return TimelineModelImpl.forEachEvent(r.events,(function(t){const{startTime:n,endTime:r}=TraceEngine.Legacy.timesForEventInMilliseconds(t);if(++a,a>e.length+2)return;if(!function(t){const{endTime:n}=TraceEngine.Legacy.timesForEventInMilliseconds(t);if(d===e.length)return!0;if(d!==a-1)return!1;if(!n)return!1;if(!l)return t===e[d].event&&++d,!1;let r=l(t);const i=h?h(t):"";i&&(r+="/"+i);r===e[d].id&&++d;return!1}(t))return;const o=(void 0!==r?Math.min(r,s):s)-Math.max(i,n);o<0&&console.error("Negative event duration");p(t,o)}),(function(e){--a,d>a&&(d=a)}),o,i,s,r.filter,!1),this.childrenInternal=t,t}getRoot(){return this.root}}export class TopDownRootNode extends TopDownNode{filter;events;startTime;endTime;eventGroupIdCallback;doNotAggregate;totalTime;selfTime;constructor(e,t,n,r,i,s){super("",null,null),this.root=this,this.events=e,this.filter=e=>t.every((t=>t.accept(e))),this.startTime=n,this.endTime=r,this.eventGroupIdCallback=s,this.doNotAggregate=i,this.totalTime=r-n,this.selfTime=this.totalTime}children(){return this.childrenInternal||this.grouppedTopNodes()}grouppedTopNodes(){const e=super.children();for(const t of e.values())this.selfTime-=t.totalTime;if(!this.eventGroupIdCallback)return e;const t=new Map;for(const n of e.values()){const e=this.eventGroupIdCallback(n.event);let r=t.get(e);r||(r=new GroupNode(e,this,n.event),t.set(e,r)),r.addChild(n,n.selfTime,n.totalTime)}return this.childrenInternal=t,t}getEventGroupIdCallback(){return this.eventGroupIdCallback}}export class BottomUpRootNode extends Node{childrenInternal;events;textFilter;filter;startTime;endTime;eventGroupIdCallback;totalTime;constructor(e,t,n,r,i,s){super("",null),this.childrenInternal=null,this.events=e,this.textFilter=t,this.filter=e=>n.every((t=>t.accept(e))),this.startTime=r,this.endTime=i,this.eventGroupIdCallback=s,this.totalTime=i-r}hasChildren(){return!0}filterChildren(e){for(const[t,n]of e)n.event&&!this.textFilter.accept(n.event)&&e.delete(t);return e}children(){return this.childrenInternal||(this.childrenInternal=this.filterChildren(this.grouppedTopNodes())),this.childrenInternal}ungrouppedTopNodes(){const e=this,t=this.startTime,n=this.endTime,r=new Map,i=[n-t],s=[],o=new Map;TimelineModelImpl.forEachEvent(this.events,(function(e){const{startTime:r,endTime:l}=TraceEngine.Legacy.timesForEventInMilliseconds(e),h=(void 0!==l?Math.min(l,n):n)-Math.max(r,t);i[i.length-1]-=h,i.push(h);const a=_eventId(e),d=!o.has(a);d&&o.set(a,h);s.push(d)}),(function(t){const n=_eventId(t);let l=r.get(n);l||(l=new BottomUpNode(e,n,t,!1,e),r.set(n,l));l.selfTime+=i.pop()||0,s.pop()&&(l.totalTime+=o.get(n)||0,o.delete(n));s.length&&l.setHasChildren(!0)}),void 0,t,n,this.filter,!1),this.selfTime=i.pop()||0;for(const e of r)e[1].selfTime<=0&&r.delete(e[0]);return r}grouppedTopNodes(){const e=this.ungrouppedTopNodes();if(!this.eventGroupIdCallback)return e;const t=new Map;for(const n of e.values()){const e=this.eventGroupIdCallback(n.event);let r=t.get(e);r||(r=new GroupNode(e,this,n.event),t.set(e,r)),r.addChild(n,n.selfTime,n.selfTime)}return t}}export class GroupNode extends Node{childrenInternal;isGroupNodeInternal;constructor(e,t,n){super(e,n),this.childrenInternal=new Map,this.parent=t,this.isGroupNodeInternal=!0}addChild(e,t,n){this.childrenInternal.set(e.id,e),this.selfTime+=t,this.totalTime+=n,e.parent=this}hasChildren(){return!0}children(){return this.childrenInternal}}export class BottomUpNode extends Node{parent;root;depth;cachedChildren;hasChildrenInternal;constructor(e,t,n,r,i){super(t,n),this.parent=i,this.root=e,this.depth=(i.depth||0)+1,this.cachedChildren=null,this.hasChildrenInternal=r}hasChildren(){return this.hasChildrenInternal}setHasChildren(e){this.hasChildrenInternal=e}children(){if(this.cachedChildren)return this.cachedChildren;const e=[0],t=[],n=[],r=new Map,i=this.root.startTime,s=this.root.endTime;let o=i;const l=this;return TimelineModelImpl.forEachEvent(this.root.events,(function(r){const{startTime:o,endTime:l}=TraceEngine.Legacy.timesForEventInMilliseconds(r),h=(void 0!==l?Math.min(l,s):s)-Math.max(o,i);h<0&&console.assert(!1,"Negative duration of an event");e[e.length-1]-=h,e.push(h);const a=_eventId(r);t.push(a),n.push(r)}),(function(i){const{startTime:h,endTime:a}=TraceEngine.Legacy.timesForEventInMilliseconds(i),d=e.pop(),c=t.pop();let p;for(n.pop(),p=l;p.depth>1;p=p.parent)if(p.id!==t[t.length+1-p.depth])return;if(p.id!==c||t.length<l.depth)return;const u=t[t.length-l.depth];if(p=r.get(u),!p){const e=n[n.length-l.depth],t=n.length>l.depth;p=new BottomUpNode(l.root,u,e,t,l),r.set(u,p)}const m=void 0!==a?Math.min(a,s):s,T=m-Math.max(h,o);p.selfTime+=d||0,p.totalTime+=T,o=m}),void 0,i,s,this.root.filter,!1),this.cachedChildren=this.root.filterChildren(r),this.cachedChildren}searchTree(e,t){return t=t||[],this.event&&e(this.event)&&t.push(this),t}}export function eventURL(e){const t=e.args.data||e.args.beginData;if(t&&t.url)return t.url;let n=eventStackFrame(e);for(;n;){const e=n.url;if(e)return e;n=n.parent}return null}export function eventStackFrame(e){return TimelineModelImpl.isJsFrameEvent(e)?e.args.data||null:EventOnTimelineData.forEvent(e).topFrame()}export function _eventId(e){if(e.name===RecordType.TimeStamp)return`${e.name}:${e.args.data.message}`;if(!TimelineModelImpl.isJsFrameEvent(e))return e.name;const t=e.args.data,n=t.scriptId||t.url||"",r=t.functionName;return`f:${TimelineJSProfileProcessor.isNativeRuntimeFrame(t)?TimelineJSProfileProcessor.nativeGroup(r)||r:`${r}:${t.lineNumber}:${t.columnNumber}`}@${n}`}