@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 7.45 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as i18n from"../../core/i18n/i18n.js";import*as Platform from"../../core/platform/platform.js";import*as SDK from"../../core/sdk/sdk.js";import*as Workspace from"../../models/workspace/workspace.js";import*as DataGrid from"../../ui/legacy/components/data_grid/data_grid.js";import*as UI from"../../ui/legacy/legacy.js";import liveHeapProfileStyles from"./liveHeapProfile.css.js";const UIStrings={jsHeap:"JS Heap",allocatedJsHeapSizeCurrentlyIn:"Allocated JS heap size currently in use",vms:"VMs",numberOfVmsSharingTheSameScript:"Number of VMs sharing the same script source",scriptUrl:"Script URL",urlOfTheScriptSource:"URL of the script source",heapProfile:"Heap Profile",anonymousScriptS:"(Anonymous Script {PH1})",kb:"kB"},str_=i18n.i18n.registerUIStrings("panels/profiler/LiveHeapProfileView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let liveHeapProfileViewInstance,profilerActionDelegateInstance;export class LiveHeapProfileView extends UI.Widget.VBox{gridNodeByUrl;setting;toggleRecordAction;toggleRecordButton;startWithReloadButton;dataGrid;currentPollId;constructor(){super(!0),this.gridNodeByUrl=new Map,this.setting=Common.Settings.Settings.instance().moduleSetting("memoryLiveHeapProfile");const e=new UI.Toolbar.Toolbar("live-heap-profile-toolbar",this.contentElement);this.toggleRecordAction=UI.ActionRegistry.ActionRegistry.instance().action("live-heap-profile.toggle-recording"),this.toggleRecordButton=UI.Toolbar.Toolbar.createActionButton(this.toggleRecordAction),this.toggleRecordButton.setToggled(this.setting.get()),e.appendToolbarItem(this.toggleRecordButton);const t=SDK.TargetManager.TargetManager.instance().primaryPageTarget();if(t&&t.model(SDK.ResourceTreeModel.ResourceTreeModel)){const t=UI.ActionRegistry.ActionRegistry.instance().action("live-heap-profile.start-with-reload");this.startWithReloadButton=UI.Toolbar.Toolbar.createActionButton(t),e.appendToolbarItem(this.startWithReloadButton)}this.dataGrid=this.createDataGrid(),this.dataGrid.asWidget().show(this.contentElement),this.currentPollId=0}static instance(){return liveHeapProfileViewInstance||(liveHeapProfileViewInstance=new LiveHeapProfileView),liveHeapProfileViewInstance}createDataGrid(){const e={id:"",title:Common.UIString.LocalizedEmptyString,width:void 0,fixedWidth:!0,sortable:!0,align:DataGrid.DataGrid.Align.Right,sort:DataGrid.DataGrid.Order.Descending,titleDOMFragment:void 0,editable:void 0,nonSelectable:void 0,longText:void 0,disclosure:void 0,weight:void 0,allowInSortByEvenWhenHidden:void 0,dataType:void 0,defaultWeight:void 0},t=[{...e,id:"size",title:i18nString(UIStrings.jsHeap),width:"72px",fixedWidth:!0,sortable:!0,align:DataGrid.DataGrid.Align.Right,sort:DataGrid.DataGrid.Order.Descending,tooltip:i18nString(UIStrings.allocatedJsHeapSizeCurrentlyIn)},{...e,id:"isolates",title:i18nString(UIStrings.vms),width:"40px",fixedWidth:!0,align:DataGrid.DataGrid.Align.Right,tooltip:i18nString(UIStrings.numberOfVmsSharingTheSameScript)},{...e,id:"url",title:i18nString(UIStrings.scriptUrl),fixedWidth:!1,sortable:!0,tooltip:i18nString(UIStrings.urlOfTheScriptSource)}],i=new DataGrid.SortableDataGrid.SortableDataGrid({displayName:i18nString(UIStrings.heapProfile),columns:t,editCallback:void 0,deleteCallback:void 0,refreshCallback:void 0});i.setResizeMethod(DataGrid.DataGrid.ResizeMethod.Last),i.element.classList.add("flex-auto"),i.element.addEventListener("keydown",this.onKeyDown.bind(this),!1),i.addEventListener(DataGrid.DataGrid.Events.OpenedNode,this.revealSourceForSelectedNode,this),i.addEventListener(DataGrid.DataGrid.Events.SortingChanged,this.sortingChanged,this);for(const e of t){const t=i.headerTableHeader(e.id);t&&t.setAttribute("title",e.tooltip)}return i}wasShown(){super.wasShown(),this.poll(),this.registerCSSFiles([liveHeapProfileStyles]),this.setting.addChangeListener(this.settingChanged,this)}willHide(){++this.currentPollId,this.setting.removeChangeListener(this.settingChanged,this)}settingChanged(e){this.toggleRecordButton.setToggled(e.data)}async poll(){const e=this.currentPollId;do{const t=Array.from(SDK.IsolateManager.IsolateManager.instance().isolates()),i=await Promise.all(t.map((e=>{const t=e.heapProfilerModel();return t?t.getSamplingProfile():null})));if(this.currentPollId!==e)return;this.update(t,i),await new Promise((e=>window.setTimeout(e,3e3)))}while(this.currentPollId===e)}update(e,t){const i=new Map;t.forEach(((t,i)=>{t&&n(e[i],"",t.head)}));const r=this.dataGrid.rootNode(),o=new Set;for(const e of i){const t=e[0],i=e[1].size,n=e[1].isolates.size;if(!t){console.info(`Node with empty URL: ${i} bytes`);continue}let a=this.gridNodeByUrl.get(t);a?a.updateNode(i,n):(a=new GridNode(t,i,n),this.gridNodeByUrl.set(t,a),r.appendChild(a)),o.add(a)}for(const e of r.children.slice()){o.has(e)||e.remove();const t=e;this.gridNodeByUrl.delete(t.url)}function n(e,t,r){const o=r.callFrame.url||t||function(e){const t=e.callFrame.functionName;return t.startsWith("(")&&"(root)"!==t?t:""}(r)||function(e){return Number(e.callFrame.scriptId)?i18nString(UIStrings.anonymousScriptS,{PH1:e.callFrame.scriptId}):""}(r);if(r.children.forEach(n.bind(null,e,o)),!r.selfSize)return;let a=i.get(o);a||(a={size:0,isolates:new Set},i.set(o,a)),a.size+=r.selfSize,a.isolates.add(e)}this.sortingChanged()}onKeyDown(e){"Enter"===e.key&&(e.consume(!0),this.revealSourceForSelectedNode())}revealSourceForSelectedNode(){const e=this.dataGrid.selectedNode;if(!e||!e.url)return;const t=Workspace.Workspace.WorkspaceImpl.instance().uiSourceCodeForURL(e.url);t&&Common.Revealer.reveal(t)}sortingChanged(){const e=this.dataGrid.sortColumnId();if(!e)return;const t="url"===e?function(e,t){return t.url.localeCompare(e.url)}:function(e,t){return t.size-e.size};this.dataGrid.sortNodes(t,this.dataGrid.isSortOrderAscending())}toggleRecording(){!this.setting.get()?this.startRecording(!1):this.stopRecording()}startRecording(e){if(this.setting.set(!0),!e)return;const t=SDK.TargetManager.TargetManager.instance().primaryPageTarget();if(!t)return;const i=t.model(SDK.ResourceTreeModel.ResourceTreeModel);i&&i.reloadPage()}async stopRecording(){this.setting.set(!1)}}export class GridNode extends DataGrid.SortableDataGrid.SortableDataGridNode{url;size;isolateCount;constructor(e,t,i){super(),this.url=e,this.size=t,this.isolateCount=i}updateNode(e,t){this.size===e&&this.isolateCount===t||(this.size=e,this.isolateCount=t,this.refresh())}createCell(e){const t=this.createTD(e);switch(e){case"url":t.textContent=this.url;break;case"size":t.textContent=Platform.NumberUtilities.withThousandsSeparator(Math.round(this.size/1e3)),t.createChild("span","size-units").textContent=i18nString(UIStrings.kb);break;case"isolates":t.textContent=`${this.isolateCount}`}return t}}export class ActionDelegate{static instance(e={forceNew:null}){const{forceNew:t}=e;return profilerActionDelegateInstance&&!t||(profilerActionDelegateInstance=new ActionDelegate),profilerActionDelegateInstance}handleAction(e,t){return(async()=>{const e="live_heap_profile";await UI.ViewManager.ViewManager.instance().showView(e);const i=UI.ViewManager.ViewManager.instance().view(e);if(i){const e=await i.widget();this.innerHandleAction(e,t)}})(),!0}innerHandleAction(e,t){switch(t){case"live-heap-profile.toggle-recording":e.toggleRecording();break;case"live-heap-profile.start-with-reload":e.startRecording(!0);break;default:console.assert(!1,`Unknown action: ${t}`)}}}