Use lstats for profiler

This commit is contained in:
pandamicro
2017-05-03 14:18:30 +08:00
parent 4f034357d8
commit 0571671338
3 changed files with 32 additions and 173 deletions
-18
View File
@@ -137,7 +137,6 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
// FPS
self._totalFrames = 0;
self._lastUpdate = Date.now();
self._secondsPerFrame = 0;
self._deltaTime = 0.0;
self._dirtyRegion = null;
@@ -1073,16 +1072,6 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
}
},
/**
* !#en Returns seconds per frame.
* !#zh 获取实际记录的上一帧执行时间,可能与单位帧执行时间(AnimationInterval)有出入。
* @method getSecondsPerFrame
* @return {Number}
*/
getSecondsPerFrame: function () {
return this._secondsPerFrame;
},
/**
* !#en Returns whether next delta time equals to zero.
* !#zh 返回下一个 “delta time” 是否等于零。
@@ -1241,11 +1230,6 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
getDeltaTime: function () {
return this._deltaTime;
},
_calculateMPF: function () {
var now = Date.now();
this._secondsPerFrame = (now - this._lastUpdate) / 1000;
}
});
// Event target
@@ -1424,8 +1408,6 @@ cc.DisplayLinkDirector = cc.Director.extend(/** @lends cc.Director# */{
this.emit(cc.Director.EVENT_AFTER_DRAW);
cc.eventManager.frameUpdateListeners();
this._calculateMPF();
}
},
+31 -155
View File
@@ -23,167 +23,43 @@
THE SOFTWARE.
****************************************************************************/
/**
* @class profiler
*/
cc.profiler = (function () {
var _inited = false, _showFPS = false;
var _frames = 0, _frameRate = 0, _lastSPF = 0, _accumDt = 0;
var _FPSLabel = null,
_SPFLabel = null,
_drawsLabel = null,
_ModeLabel = null,
_fps = null;
var LEVEL_DET_FACTOR = 0.6, _levelDetCycle = 10;
var LEVELS = [0, 10, 20, 30];
var _fpsCount = [0, 0, 0, 0];
var _currLevel = 3, _analyseCount = 0, _totalFPS = 0;
var LStats = require('../../../external/lstats/lstats');
var createStatsLabel = function () {
_FPSLabel = document.createElement('div');
_SPFLabel = document.createElement('div');
_drawsLabel = document.createElement('div');
_ModeLabel = document.createElement('div');
_fps = document.createElement('div');
var _fps = document.createElement('div');
_fps.id = 'fps';
_fps.id = 'fps';
_fps.style.position = 'absolute';
_fps.style.padding = '3px';
_fps.style.textAlign = 'left';
_fps.style.backgroundColor = 'rgb(0, 0, 34)';
_fps.style.bottom = cc.macro.DIRECTOR_STATS_POSITION.y + '0px';
_fps.style.left = cc.macro.DIRECTOR_STATS_POSITION.x + 'px';
_fps.style.width = '45px';
_fps.style.height = '80px';
let stats = new LStats(_fps, [
'fps', 'ms', 'mem'
]);
var labels = [_drawsLabel, _SPFLabel, _FPSLabel, _ModeLabel];
for (var i = 0; i < labels.length; ++i) {
var style = labels[i].style;
style.color = 'rgb(0, 255, 255)';
style.font = 'bold 12px Helvetica, Arial';
style.lineHeight = '20px';
style.width = '100%';
_fps.appendChild(labels[i]);
}
};
let _showFPS = false;
var analyseFPS = function (fps) {
var lastId = LEVELS.length - 1,
i = lastId,
ratio,
average = 0;
_analyseCount++;
_totalFPS += fps;
function tick () {
stats.tick();
}
for (; i >= 0; i--) {
if (fps >= LEVELS[i]) {
_fpsCount[i]++;
break;
cc.profiler = module.exports = {
isShowingStats () {
return _showFPS;
},
hideStats () {
if (_showFPS) {
if (_fps.parentElement === document.body) {
document.body.removeChild(_fps);
}
}
if (_analyseCount >= _levelDetCycle) {
average = _totalFPS / _levelDetCycle;
for (i = lastId; i >0; i--) {
ratio = _fpsCount[i] / _levelDetCycle;
// Determined level
if (ratio >= LEVEL_DET_FACTOR && average >= LEVELS[i]) {
// Level changed
if (i !== _currLevel) {
_currLevel = i;
profiler.onFrameRateChange && profiler.onFrameRateChange(average.toFixed(2));
}
break;
}
// If no level determined, that means the framerate is not stable
}
_analyseCount = 0;
_totalFPS = 0;
for (i = lastId; i > 0; i--) {
_fpsCount[i] = 0;
}
}
};
var afterVisit = function () {
_lastSPF = cc.director.getSecondsPerFrame();
_frames++;
_accumDt += cc.director.getDeltaTime();
if (_accumDt > cc.macro.DIRECTOR_FPS_INTERVAL) {
_frameRate = _frames / _accumDt;
_frames = 0;
_accumDt = 0;
if (profiler.onFrameRateChange) {
analyseFPS(_frameRate);
}
if (_showFPS) {
_SPFLabel.innerHTML = _lastSPF.toFixed(3);
_FPSLabel.innerHTML = _frameRate.toFixed(1);
_drawsLabel.innerHTML = (0 | cc.g_NumberOfDraws).toString();
_ModeLabel.innerHTML = (cc._renderType === cc.game.RENDER_TYPE_CANVAS) ? 'Canvas' : 'WebGL';
}
}
};
var profiler = {
onFrameRateChange: null,
getSecondsPerFrame: function () {
return _lastSPF;
},
getFrameRate: function () {
return _frameRate;
},
setProfileDuration: function (duration) {
if (!isNaN(duration) && duration > 0) {
_levelDetCycle = duration / cc.macro.DIRECTOR_FPS_INTERVAL;
}
},
resumeProfiling: function () {
cc.director.on(cc.Director.EVENT_AFTER_VISIT, afterVisit);
},
stopProfiling: function () {
cc.director.off(cc.Director.EVENT_AFTER_VISIT, afterVisit);
},
isShowingStats: function () {
return _showFPS;
},
showStats: function () {
if (!_inited) {
this.init();
}
if (_fps.parentElement === null) {
cc.container.appendChild(_fps);
}
_showFPS = true;
},
hideStats: function () {
cc.director.off(cc.Director.EVENT_AFTER_VISIT, tick);
_showFPS = false;
if (_fps && _fps.parentElement === cc.container) {
cc.container.removeChild(_fps);
}
},
init: function () {
if (!_inited) {
createStatsLabel();
this.resumeProfiling();
_inited = true;
}
}
};
},
return profiler;
})();
module.exports = cc.profiler;
showStats () {
if (!_showFPS) {
if (_fps.parentElement === null) {
document.body.appendChild(_fps);
}
cc.director.on(cc.Director.EVENT_AFTER_VISIT, tick);
_showFPS = true;
}
}
}
+1
View File
@@ -0,0 +1 @@
module.exports=function(){"use strict";function t(){if(typeof window.performance==="undefined"){window.performance={}}if(!window.performance.now){let t=Date.now();if(performance.timing&&performance.timing.navigationStart){t=performance.timing.navigationStart}window.performance.now=function i(){return Date.now()-t}}if(!window.performance.mark){window.performance.mark=function(){}}if(!window.performance.measure){window.performance.measure=function(){}}if(!window.performance.memory){window.performance.memory={usedJSHeapSize:0,totalJSHeapSize:0}}}t();const i=80;const e=38;const s=Math.round(window.devicePixelRatio||1);const h=i*s;const a=e*s;const l=3*s;const n=2*s;const c=3*s;const _=15*s;const o=74*s;const f=(e-18)*s;class r{constructor(t,r,d,m){this._name=r;this._fg=d;this._bg=m;this._canvas=document.createElement("canvas");this._canvas.width=h;this._canvas.height=a;this._canvas.style.cssText=`width:${i}px;height:${e}px`;this._ctx=this._canvas.getContext("2d");this._ctx.font=`bold ${9*s}px Helvetica,Arial,sans-serif`;this._ctx.textBaseline="top";this._ctx.fillStyle=m;this._ctx.fillRect(0,0,h,a);this._ctx.fillStyle=d;this._ctx.fillText(r,l,n);this._ctx.fillRect(c,_,o,f);this._ctx.fillStyle=m;this._ctx.globalAlpha=.9;this._ctx.fillRect(c,_,o,f);t.appendChild(this._canvas)}draw(t,i){let e=Math.round(t*100)/100;this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=1;this._ctx.fillRect(0,0,h,_);this._ctx.fillStyle=this._fg;this._ctx.fillText(`${e} ${this._name}`,l,n);this._ctx.drawImage(this._canvas,c+s,_,o-s,f,c,_,o-s,f);this._ctx.fillRect(c+o-s,_,s,f);let a=Math.round((1-t/i)*f);this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=.9;this._ctx.fillRect(c+o-s,_,s,a)}}class d extends r{constructor(t,i,e,h){super(t,i,e,h);this._threshold=0;this._canvas2=document.createElement("canvas");this._canvas2.width=o;this._canvas2.height=f;this._canvas2.style.cssText=`width:${o/s}px;height:${f/s}px`;this._ctx2=this._canvas2.getContext("2d");this._ctx2.fillStyle=h;this._ctx.globalAlpha=.9;this._ctx2.fillRect(0,0,o,f)}draw(t,i){let e=Math.round(t*100)/100;this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=1;this._ctx.fillRect(0,0,h,_);this._ctx.fillStyle=this._fg;this._ctx.fillText(`${e} ${this._name}`,l,n);if(t>this._threshold){let i=(t-t%f)/f;let e=f*(i+1);let h=this._threshold;this._threshold=e;let a=h/e;this._ctx2.drawImage(this._canvas,c,_,o,f,0,0,o,f);this._ctx.fillStyle=this._fg;this._ctx.globalAlpha=1;this._ctx.fillRect(c,_,o,f);this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=.9;this._ctx.fillRect(c,_,o,f);this._ctx.fillStyle=this._fg;this._ctx.globalAlpha=1;this._ctx.drawImage(this._canvas2,s,0,o-s,f,c,_+(1-a)*f,o-s,a*f)}else{this._ctx.drawImage(this._canvas,c+s,_,o-s,f,c,_,o-s,f)}let a=Math.round((1-t/this._threshold)*f);this._ctx.fillRect(c+o-s,_,s,f);if(i){this._ctx.fillStyle="#e00";this._ctx.globalAlpha=1;this._ctx.fillRect(c+o-s,_,s,a)}else{this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=.9;this._ctx.fillRect(c+o-s,_,s,a)}}}class m{constructor(t,i=["fps","ms","mb"]){this._mode=0;this._enableFPS=i.indexOf("fps")!==-1;this._enableMS=i.indexOf("ms")!==-1;if(window.performance.memory&&window.performance.memory.totalJSHeapSize){this._enableMB=i.indexOf("mb")!==-1}this._framesMS=0;this._lastTimeMS=0;this._frames=0;this._lastTimeFPS=0;this._used=0;this._lastUsed=0;this._lastTimeMB=0;let e=document.createElement("div");e.style.cssText="position:fixed;top:5px;right:5px;cursor:pointer;opacity:0.9;z-index:9999";e.addEventListener("mousedown",t=>{t.preventDefault();t.stopPropagation();this._mode=++this._mode%e.children.length;for(let t=0;t<e.children.length;++t){e.children[t].style.display=t===this._mode?"block":"none"}});if(this._enableFPS){this._graphFPS=new r(e,"FPS","#0ff","#002")}if(this._enableMS){this._graphMS=new r(e,"MS","#f08","#201")}if(this._enableMB){this._graphMB=new d(e,"MB","#0f0","#020")}for(let t=0;t<e.children.length;++t){e.children[t].style.display=t===0?"block":"none"}t.appendChild(e)}tick(){let t=window.performance.now();if(this._enableMS){++this._framesMS;let i=t-this._lastTimeMS;if(i>100){this._graphMS.draw(i/this._framesMS,50);this._framesMS=0;this._lastTimeMS=t}}if(this._enableFPS){++this._frames;let i=t-this._lastTimeFPS;if(i>1e3){this._graphFPS.draw(this._frames*1e3/i,100);this._frames=0;this._lastTimeFPS=t}}if(this._enableMB){let i=t-this._lastTimeMB;if(i>200){this._lastUsed=this._used;this._used=window.performance.memory.usedJSHeapSize;this._lastTimeMB=t;let i=this._used-this._lastUsed;this._graphMB.draw(this._used/1048576,i<0)}}}}return m}();