@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 7.97 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as Host from"../../core/host/host.js";import*as i18n from"../../core/i18n/i18n.js";import*as SDK from"../../core/sdk/sdk.js";import*as PerfUI from"../../ui/legacy/components/perf_ui/perf_ui.js";import*as Components from"../../ui/legacy/components/utils/utils.js";import*as UI from"../../ui/legacy/legacy.js";import*as CPUProfile from"../../models/cpu_profile/cpu_profile.js";import{ProfileFlameChartDataProvider}from"./CPUProfileFlameChart.js";import{ProfileEvents,ProfileType}from"./ProfileHeader.js";import{ProfileView,WritableProfileHeader}from"./ProfileView.js";const UIStrings={selfTime:"Self Time",totalTime:"Total Time",recordJavascriptCpuProfile:"Record JavaScript CPU Profile",stopCpuProfiling:"Stop CPU profiling",startCpuProfiling:"Start CPU profiling",cpuProfiles:"CPU PROFILES",cpuProfilesShow:"CPU profiles show where the execution time is spent in your page's JavaScript functions.",recording:"Recording…",fms:"{PH1} ms",formatPercent:"{PH1} %",name:"Name",url:"URL",aggregatedSelfTime:"Aggregated self time",aggregatedTotalTime:"Aggregated total time",notOptimized:"Not optimized"},str_=i18n.i18n.registerUIStrings("panels/profiler/CPUProfileView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class CPUProfileView extends ProfileView{profileHeader;adjustedTotal;constructor(e){super(),this.profileHeader=e,this.initialize(new NodeFormatter(this));const r=e.profileModel();this.adjustedTotal=r.profileHead.total,this.adjustedTotal-=r.idleNode?r.idleNode.total:0,this.setProfile(r)}wasShown(){super.wasShown(),PerfUI.LineLevelProfile.Performance.instance().reset(),PerfUI.LineLevelProfile.Performance.instance().appendCPUProfile(this.profileHeader.profileModel(),this.profileHeader.target)}columnHeader(e){switch(e){case"self":return i18nString(UIStrings.selfTime);case"total":return i18nString(UIStrings.totalTime)}return Common.UIString.LocalizedEmptyString}createFlameChartDataProvider(){return new CPUFlameChartDataProvider(this.profileHeader.profileModel(),this.profileHeader.cpuProfilerModel)}}export class CPUProfileType extends ProfileType{recording;constructor(){super(CPUProfileType.TypeId,i18nString(UIStrings.recordJavascriptCpuProfile)),this.recording=!1;const e=SDK.TargetManager.TargetManager.instance().models(SDK.CPUProfilerModel.CPUProfilerModel);for(const r of e)for(const e of r.registeredConsoleProfileMessages)this.consoleProfileFinished(e);SDK.TargetManager.TargetManager.instance().addModelListener(SDK.CPUProfilerModel.CPUProfilerModel,SDK.CPUProfilerModel.Events.ConsoleProfileFinished,(e=>this.consoleProfileFinished(e.data)),this)}profileBeingRecorded(){return super.profileBeingRecorded()}typeName(){return"CPU"}fileExtension(){return".cpuprofile"}get buttonTooltip(){return this.recording?i18nString(UIStrings.stopCpuProfiling):i18nString(UIStrings.startCpuProfiling)}buttonClicked(){return this.recording?(this.stopRecordingProfile(),!1):(this.startRecordingProfile(),!0)}get treeItemTitle(){return i18nString(UIStrings.cpuProfiles)}get description(){return i18nString(UIStrings.cpuProfilesShow)}consoleProfileFinished(e){const r=new CPUProfileHeader(e.cpuProfilerModel,this,e.title);r.setProtocolProfile(e.cpuProfile),this.addProfile(r)}startRecordingProfile(){const e=UI.Context.Context.instance().flavor(SDK.CPUProfilerModel.CPUProfilerModel);if(this.profileBeingRecorded()||!e)return;const r=new CPUProfileHeader(e,this);this.setProfileBeingRecorded(r),SDK.TargetManager.TargetManager.instance().suspendAllTargets(),this.addProfile(r),r.updateStatus(i18nString(UIStrings.recording)),this.recording=!0,e.startRecording(),Host.userMetrics.actionTaken(Host.UserMetrics.Action.ProfilesCPUProfileTaken)}async stopRecordingProfile(){this.recording=!1;const e=this.profileBeingRecorded();if(!e||!e.cpuProfilerModel)return;const r=await e.cpuProfilerModel.stopRecording(),i=this.profileBeingRecorded();if(i){if(!r)throw new Error("Expected profile to be non-null");i.setProtocolProfile(r),i.updateStatus(""),this.setProfileBeingRecorded(null)}await SDK.TargetManager.TargetManager.instance().resumeAllTargets(),this.dispatchEventToListeners(ProfileEvents.ProfileComplete,i)}createProfileLoadedFromFile(e){return new CPUProfileHeader(null,this,e)}profileBeingRecordedRemoved(){this.stopRecordingProfile()}static TypeId="CPU"}export class CPUProfileHeader extends WritableProfileHeader{cpuProfilerModel;profileModelInternal;target;constructor(e,r,i){super(e&&e.debuggerModel(),r,i),this.cpuProfilerModel=e,this.target=this.cpuProfilerModel&&this.cpuProfilerModel.target()||null}createView(){return new CPUProfileView(this)}protocolProfile(){if(!this.protocolProfile())throw new Error("Expected _protocolProfile to be available");return this.protocolProfile()}profileModel(){if(!this.profileModelInternal)throw new Error("Expected _profileModel to be available");return this.profileModelInternal}setProfile(e){this.profileModelInternal=new CPUProfile.CPUProfileDataModel.CPUProfileDataModel(e)}}export class NodeFormatter{profileView;constructor(e){this.profileView=e}formatValue(e){return i18nString(UIStrings.fms,{PH1:e.toFixed(1)})}formatValueAccessibleText(e){return this.formatValue(e)}formatPercent(e,r){if(this.profileView){const i=this.profileView.profile();if(i&&r.profileNode!==i.idleNode)return i18nString(UIStrings.formatPercent,{PH1:e.toFixed(2)})}return""}linkifyNode(e){const r=this.profileView.profileHeader.cpuProfilerModel,i=r?r.target():null;return this.profileView.linkifier().maybeLinkifyConsoleCallFrame(i,e.profileNode.callFrame,{className:"profile-node-file",inlineFrameIndex:0})}}export class CPUFlameChartDataProvider extends ProfileFlameChartDataProvider{cpuProfile;cpuProfilerModel;entrySelfTimes;constructor(e,r){super(),this.cpuProfile=e,this.cpuProfilerModel=r}minimumBoundary(){return this.cpuProfile.profileStartTime}totalTime(){return this.cpuProfile.profileHead.total}entryHasDeoptReason(e){const r=this.entryNodes[e];return Boolean(r.deoptReason)}calculateTimelineData(){const e=[],r=[];let i=5;this.cpuProfile.forEachFrame((function(){r.push(e.length),e.push(null)}),(function(t,o,n,l,s){const a=r.pop();e[a]=new CPUFlameChartDataProvider.ChartEntry(t,l,n,s,o),i=Math.max(i,t)}));const t=new Array(e.length),o=new Uint16Array(e.length),n=new Float32Array(e.length),l=new Float32Array(e.length),s=new Float64Array(e.length);for(let r=0;r<e.length;++r){const i=e[r];i&&(t[r]=i.node,o[r]=i.depth,n[r]=i.duration,s[r]=i.startTime,l[r]=i.selfTime)}return this.maxStackDepthInternal=i+1,this.entryNodes=t,this.timelineData_=PerfUI.FlameChart.FlameChartTimelineData.create({entryLevels:o,entryTotalTimes:n,entryStartTimes:s,groups:null}),this.entrySelfTimes=l,this.timelineData_}prepareHighlightedEntryInfo(e){const r=this.timelineData_,i=this.entryNodes[e];if(!i)return null;const t=[];function o(e,r){t.push({title:e,value:r})}function n(e){return 0===e?"0":e<1e3?i18nString(UIStrings.fms,{PH1:e.toFixed(1)}):i18n.TimeUtilities.secondsToString(e/1e3,!0)}const l=UI.UIUtils.beautifyFunctionName(i.functionName);o(i18nString(UIStrings.name),l);const s=n(this.entrySelfTimes[e]),a=n(r.entryTotalTimes[e]);o(i18nString(UIStrings.selfTime),s),o(i18nString(UIStrings.totalTime),a);const f=new Components.Linkifier.Linkifier,d=f.maybeLinkifyConsoleCallFrame(this.cpuProfilerModel&&this.cpuProfilerModel.target(),i.callFrame);d&&o(i18nString(UIStrings.url),d.textContent||""),f.dispose(),o(i18nString(UIStrings.aggregatedSelfTime),i18n.TimeUtilities.secondsToString(i.self/1e3,!0)),o(i18nString(UIStrings.aggregatedTotalTime),i18n.TimeUtilities.secondsToString(i.total/1e3,!0));const c=i.deoptReason;return c&&o(i18nString(UIStrings.notOptimized),c),ProfileView.buildPopoverTable(t)}}!function(e){e.ChartEntry=class{depth;duration;startTime;selfTime;node;constructor(e,r,i,t,o){this.depth=e,this.duration=r,this.startTime=i,this.selfTime=t,this.node=o}}}(CPUFlameChartDataProvider||(CPUFlameChartDataProvider={}));