@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 2.94 kB
JavaScript
import e from"../../../../core/CircularArray.js";import t from"../../../../core/Evented.js";import has from"../../../../core/has.js";import{isSome as s}from"../../../../core/maybe.js";import{createDisjointTimerQuery as r}from"../../../webgl/capabilities/DisjointTimerQuery.js";const n=has("esri-2d-profiler");class i{constructor(s,i){if(this._events=new t,this._entries=new Map,this._timings=new e(10),this._currentContainer=null,this._currentPass=null,this._currentBrush=null,this._currentSummary=null,!n)return;this._ext=r(s.gl,{}),this._debugOutput=i;const o=s.gl;if(this.enableCommandLogging)for(const e in o)if("function"==typeof o[e]){const t=o[e],s=e.includes("draw");o[e]=(...r)=>(this._events.emit("command",{container:this._currentContainer,pass:this._currentPass,brush:this._currentBrush,method:e,args:r,isDrawCommand:s}),this._currentSummary&&(this._currentSummary.commands++,s&&this._currentSummary.drawCommands++),t.apply(o,r))}}get enableCommandLogging(){return!("object"==typeof n&&n.disableCommands)}recordContainerStart(e){n&&(this._currentContainer=e)}recordContainerEnd(){n&&(this._currentContainer=null)}recordPassStart(e){n&&(this._currentPass=e,this._initSummary())}recordPassEnd(){n&&(this._currentPass=null,this._emitSummary())}recordBrushStart(e){n&&(this._currentBrush=e)}recordBrushEnd(){n&&(this._currentBrush=null)}recordStart(e){if(n&&s(this._ext)){if(this._entries.has(e)){const t=this._entries.get(e),r=this._ext.resultAvailable(t.query),n=this._ext.disjoint();if(r&&!n){const r=this._ext.getResult(t.query)/1e6;let n=0;if(s(this._timings.enqueue(r))){const e=this._timings.entries,t=e.length;let s=0;for(const r of e)s+=r;n=s/t}const i=r.toFixed(2),o=n?n.toFixed(2):"--";this.enableCommandLogging?(console.groupCollapsed(`Frame report for ${e}, ${i} ms (${o} last 10 avg)\n${t.commandsLen} Commands (${t.drawCommands} draw)`),console.log("RenderPass breakdown: "),console.table(t.summaries),console.log("Commands: ",t.commands),console.groupEnd()):console.log(`Frame report for ${e}, ${i} ms (${o} last 10 avg)`),this._debugOutput.innerHTML=`${i} (${o})`}for(const e of t.handles)e.remove();this._ext.deleteQuery(t.query),this._entries.delete(e)}const t={name:e,query:this._ext.createQuery(),commands:[],commandsLen:0,drawCommands:0,summaries:[],handles:[]};this.enableCommandLogging&&(t.handles.push(this._events.on("command",(e=>{t.commandsLen++,t.commands.push(e),e.isDrawCommand&&t.drawCommands++}))),t.handles.push(this._events.on("summary",(e=>{t.summaries.push(e)})))),this._ext.beginTimeElapsed(t.query),this._entries.set(e,t)}}recordEnd(e){n&&s(this._ext)&&this._entries.has(e)&&this._ext.endTimeElapsed()}_initSummary(){this.enableCommandLogging&&(this._currentSummary={container:this._currentContainer,pass:this._currentPass,drawCommands:0,commands:0})}_emitSummary(){this.enableCommandLogging&&this._currentSummary&&this._events.emit("summary",this._currentSummary)}}export{i as Profiler};