UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 3.33 kB
import*as Platform from"../../../core/platform/platform.js";import*as Types from"../types/types.js";import*as CPUProfile from"../../cpu_profile/cpu_profile.js";import*as Helpers from"../helpers/helpers.js";const events=new Map,profilesInProcess=new Map,preprocessedData=new Map;let handlerState=1;export function buildProfileCalls(){for(const[i,s]of preprocessedData)for(const[l,t]of s){const a=t.threadId;if(!t.rawProfile.nodes.length||!a)continue;const o=[],n=new CPUProfile.CPUProfileDataModel.CPUProfileDataModel(t.rawProfile),c={rawProfile:t.rawProfile,parsedProfile:n,profileCalls:[]};n.forEachFrame(e,r),Helpers.Trace.sortTraceEventsInPlace(c.profileCalls);function e(e,r,s){const l=Helpers.Timing.millisecondsToMicroseconds(Types.Timing.MilliSeconds(s));o.push({callFrame:r.callFrame,ts:l,pid:i,children:[],tid:a})}function r(e,r,i,s,t){const a=o.pop();if(!a)return;const{callFrame:n,ts:p,pid:d,children:f,tid:m}=a;if(void 0===n||void 0===p||void 0===d||void 0===l||void 0===f||void 0===m)return;const u=Helpers.Timing.millisecondsToMicroseconds(Types.Timing.MilliSeconds(s)),h={callFrame:n,ts:p,pid:d,dur:u,selfTime:Helpers.Timing.millisecondsToMicroseconds(Types.Timing.MilliSeconds(t)),children:f,ph:"X",cat:"",name:"ProfileCall",tid:m,nodeId:r.id},P=o.at(-1);c.profileCalls.push(h),P&&(P.children=P.children||[],P.children.push(h),P.selfTime&&(P.selfTime=Types.Timing.MicroSeconds(P.selfTime-u)))}Platform.MapUtilities.getWithDefault(profilesInProcess,i,(()=>new Map)).set(a,c)}}export function reset(){events.clear(),preprocessedData.clear(),profilesInProcess.clear(),handlerState=1}export function initialize(){if(1!==handlerState)throw new Error("Samples Handler was not reset");handlerState=2}export function handleEvent(e){if(2!==handlerState)throw new Error("Samples Handler is not initialized");if(Types.TraceEvents.isTraceEventProfile(e)){const r=getOrCreatePreProcessedData(e.pid,e.id);return r.rawProfile.startTime=e.ts,void(r.threadId=e.tid)}if(Types.TraceEvents.isTraceEventProfileChunk(e)){const r=getOrCreatePreProcessedData(e.pid,e.id).rawProfile,i=e.args?.data?.cpuProfile||{samples:[]},s=i?.samples||[],l=[];for(const e of i?.nodes||[]){const r=e.callFrame.lineNumber||-1,i=e.callFrame.columnNumber||-1,s=String(e.callFrame.scriptId),t=e.callFrame.url||"",a={...e,callFrame:{...e.callFrame,url:t,lineNumber:r,columnNumber:i,scriptId:s}};l.push(a)}const t=e.args.data?.timeDeltas||[],a=e.args.data?.lines||Array(s.length).fill(0);if(r.nodes.push(...l),r.samples?.push(...s),r.timeDeltas?.push(...t),r.lines?.push(...a),r.samples&&r.timeDeltas&&r.samples.length!==r.timeDeltas.length)return void console.error("Failed to parse CPU profile.");if(!r.endTime&&r.timeDeltas){const e=r.timeDeltas;r.endTime=e.reduce(((e,r)=>e+r),r.startTime)}}else;}export async function finalize(){if(2!==handlerState)throw new Error("Samples Handler is not initialized");buildProfileCalls(),handlerState=3}export function data(){if(3!==handlerState)throw new Error("Samples Handler is not finalized");return{profilesInProcess:new Map(profilesInProcess)}}function getOrCreatePreProcessedData(e,r){const i=Platform.MapUtilities.getWithDefault(preprocessedData,e,(()=>new Map));return Platform.MapUtilities.getWithDefault(i,r,(()=>({rawProfile:{startTime:0,endTime:0,nodes:[],samples:[],timeDeltas:[],lines:[]},profileId:r})))}