diff --git a/cocos2d/core/utils/CCProfiler.js b/cocos2d/core/utils/CCProfiler.js index 40d98917aa..f4213934f0 100644 --- a/cocos2d/core/utils/CCProfiler.js +++ b/cocos2d/core/utils/CCProfiler.js @@ -23,19 +23,51 @@ THE SOFTWARE. ****************************************************************************/ -var LStats = require('../../../external/lstats/lstats'); +var PStats = require('../../../external/pstats/pstats'); +var macro = require('../platform/CCMacro'); var _fps = document.createElement('div'); _fps.id = 'fps'; -let stats = new LStats(_fps, [ - 'fps', 'ms', 'mem' -]); +let stats = PStats.new(_fps, { + values: { + frame: { desc: 'Frame time (ms)', min: 0, max: 50, average: 500 }, + fps: { desc: 'Framerate (FPS)', below: 30, average: 500 }, + draws: { desc: 'Draw call' }, + logic: { desc: 'Game Logic (ms)', min: 0, max: 50, average: 500, color: '#080' }, + render: { desc: 'Renderer (ms)', min: 0, max: 50, average: 500, color: '#f90' }, + memory: { desc: 'Memory', extension : 'memory.used', average: 2000, threshold: true } + }, + extensions: [ + 'memory' + ], + css: '.pstats {left: ' + macro.DIRECTOR_STATS_POSITION.x + 'px; bottom: ' + macro.DIRECTOR_STATS_POSITION.y + 'px;}' +}); let _showFPS = false; -function tick () { - stats.tick(); +function beforeUpdate () { + stats('frame').start(); + stats('logic').start(); +} + +function afterVisit () { + if (cc.director.isPaused()) { + stats('frame').start(); + } + else { + stats('logic').end(); + } + stats('render').start(); +} + +function afterDraw () { + stats('render').end(); + stats('draws').value = cc.g_NumberOfDraws; + stats('memory').snapshot(); + stats('frame').end(); + stats('fps').frame(); + stats().tick(); } cc.profiler = module.exports = { @@ -48,7 +80,9 @@ cc.profiler = module.exports = { if (_fps.parentElement === document.body) { document.body.removeChild(_fps); } - cc.director.off(cc.Director.EVENT_AFTER_VISIT, tick); + cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, beforeUpdate); + cc.director.off(cc.Director.EVENT_AFTER_VISIT, afterVisit); + cc.director.off(cc.Director.EVENT_AFTER_DRAW, afterDraw); _showFPS = false; } }, @@ -58,7 +92,9 @@ cc.profiler = module.exports = { if (_fps.parentElement === null) { document.body.appendChild(_fps); } - cc.director.on(cc.Director.EVENT_AFTER_VISIT, tick); + cc.director.on(cc.Director.EVENT_BEFORE_UPDATE, beforeUpdate); + cc.director.on(cc.Director.EVENT_AFTER_VISIT, afterVisit); + cc.director.on(cc.Director.EVENT_AFTER_DRAW, afterDraw); _showFPS = true; } } diff --git a/external/lstats/lstats.js b/external/lstats/lstats.js deleted file mode 100644 index 58f2f67c9d..0000000000 --- a/external/lstats/lstats.js +++ /dev/null @@ -1 +0,0 @@ -module.exports=function(){"use strict";function t(){if(typeof window.performance==="undefined"){window.performance={}}if(!window.performance.now){var 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();var i=80;var e=38;var s=Math.round(window.devicePixelRatio||1);var a=i*s;var h=e*s;var l=3*s;var n=2*s;var r=3*s;var c=15*s;var _=74*s;var o=(e-18)*s;var f=function t(f,d,p,m){this._name=d;this._fg=p;this._bg=m;this._canvas=document.createElement("canvas");this._canvas.width=a;this._canvas.height=h;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,a,h);this._ctx.fillStyle=p;this._ctx.fillText(d,l,n);this._ctx.fillRect(r,c,_,o);this._ctx.fillStyle=m;this._ctx.globalAlpha=.9;this._ctx.fillRect(r,c,_,o);f.appendChild(this._canvas)};f.prototype.draw=function t(i,e){var h=Math.round(i*100)/100;this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=1;this._ctx.fillRect(0,0,a,c);this._ctx.fillStyle=this._fg;this._ctx.fillText(h+" "+this._name,l,n);this._ctx.drawImage(this._canvas,r+s,c,_-s,o,r,c,_-s,o);this._ctx.fillRect(r+_-s,c,s,o);var f=Math.round((1-i/e)*o);this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=.9;this._ctx.fillRect(r+_-s,c,s,f)};var d=function(t){function i(i,e,a,h){t.call(this,i,e,a,h);this._threshold=0;this._canvas2=document.createElement("canvas");this._canvas2.width=_;this._canvas2.height=o;this._canvas2.style.cssText="width:"+_/s+"px;height:"+o/s+"px";this._ctx2=this._canvas2.getContext("2d");this._ctx2.fillStyle=h;this._ctx.globalAlpha=.9;this._ctx2.fillRect(0,0,_,o)}if(t)i.__proto__=t;i.prototype=Object.create(t&&t.prototype);i.prototype.constructor=i;i.prototype.draw=function t(i,e){var h=Math.round(i*100)/100;this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=1;this._ctx.fillRect(0,0,a,c);this._ctx.fillStyle=this._fg;this._ctx.fillText(h+" "+this._name,l,n);if(i>this._threshold){var f=(i-i%o)/o;var d=o*(f+1);var p=this._threshold;this._threshold=d;var m=p/d;this._ctx2.drawImage(this._canvas,r,c,_,o,0,0,_,o);this._ctx.fillStyle=this._fg;this._ctx.globalAlpha=1;this._ctx.fillRect(r,c,_,o);this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=.9;this._ctx.fillRect(r,c,_,o);this._ctx.fillStyle=this._fg;this._ctx.globalAlpha=1;this._ctx.drawImage(this._canvas2,s,0,_-s,o,r,c+(1-m)*o,_-s,m*o)}else{this._ctx.drawImage(this._canvas,r+s,c,_-s,o,r,c,_-s,o)}var x=Math.round((1-i/this._threshold)*o);this._ctx.fillRect(r+_-s,c,s,o);if(e){this._ctx.fillStyle="#e00";this._ctx.globalAlpha=1;this._ctx.fillRect(r+_-s,c,s,x)}else{this._ctx.fillStyle=this._bg;this._ctx.globalAlpha=.9;this._ctx.fillRect(r+_-s,c,s,x)}};return i}(f);var p=function t(i,e){var s=this;if(e===void 0)e=["fps","ms","mb"];this._mode=0;this._enableFPS=e.indexOf("fps")!==-1;this._enableMS=e.indexOf("ms")!==-1;if(window.performance.memory&&window.performance.memory.totalJSHeapSize){this._enableMB=e.indexOf("mb")!==-1}this._framesMS=0;this._lastTimeMS=0;this._frames=0;this._lastTimeFPS=0;this._used=0;this._lastUsed=0;this._lastTimeMB=0;var a=document.createElement("div");a.style.cssText="position:fixed;top:5px;right:5px;cursor:pointer;opacity:0.9;z-index:9999";a.addEventListener("mousedown",function(t){t.preventDefault();t.stopPropagation();s._mode=++s._mode%a.children.length;for(var i=0;i100){this._graphMS.draw(e/this._framesMS,50);this._framesMS=0;this._lastTimeMS=i}}if(this._enableFPS){++this._frames;var s=i-this._lastTimeFPS;if(s>1e3){this._graphFPS.draw(this._frames*1e3/s,100);this._frames=0;this._lastTimeFPS=i}}if(this._enableMB){var a=i-this._lastTimeMB;if(a>200){this._lastUsed=this._used;this._used=window.performance.memory.usedJSHeapSize;this._lastTimeMB=i;var h=this._used-this._lastUsed;this._graphMB.draw(this._used/1048576,h<0)}}};return p}(); diff --git a/external/pstats/pstats.js b/external/pstats/pstats.js new file mode 100644 index 0000000000..1d1d914b37 --- /dev/null +++ b/external/pstats/pstats.js @@ -0,0 +1 @@ +module.exports=function(){"use strict";function t(){if(typeof window.performance==="undefined"){window.performance={}}if(!window.performance.now){var t=Date.now();if(performance.timing&&performance.timing.navigationStart){t=performance.timing.navigationStart}window.performance.now=function e(){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}}}var e=function t(e,a){this._color=a||"#666";this._canvas=document.createElement("canvas");this._ctx=this._canvas.getContext("2d");this._canvasDot=document.createElement("canvas");this._ctxDot=this._canvasDot.getContext("2d");this._canvasAlarm=document.createElement("canvas"),this._ctxAlarm=this._canvasAlarm.getContext("2d");this._canvas.className="pstats-canvas";e.appendChild(this._canvas)};e.prototype.init=function t(e,a){this._canvas.width=e;this._canvas.height=a;this._canvas.style.width=e+"px";this._canvas.style.height=a+"px";this._ctx.fillStyle="#444";this._ctx.fillRect(0,0,e,a);this._canvasDot.width=1;this._canvasDot.height=2*a;this._ctxDot.fillStyle="#444";this._ctxDot.fillRect(0,0,1,2*a);this._ctxDot.fillStyle=this._color;this._ctxDot.fillRect(0,a,1,a);this._ctxDot.fillStyle="#fff";this._ctxDot.globalAlpha=.5;this._ctxDot.fillRect(0,a,1,1);this._ctxDot.globalAlpha=1;this._canvasAlarm.width=1;this._canvasAlarm.height=2*a;this._ctxAlarm.fillStyle="#444";this._ctxAlarm.fillRect(0,0,1,2*a);this._ctxAlarm.fillStyle="#b70000";this._ctxAlarm.fillRect(0,a,1,a);this._ctxAlarm.globalAlpha=.5;this._ctxAlarm.fillStyle="#fff";this._ctxAlarm.fillRect(0,a,1,1);this._ctxAlarm.globalAlpha=1};var a=function(t){function e(e,a){t.call(this,e,a);this._current=0;this._max=0}if(t)e.__proto__=t;e.prototype=Object.create(t&&t.prototype);e.prototype.constructor=e;e.prototype.draw=function t(e,a){this._current+=(e-this._current)*.1;this._max*=.99;if(this._current>this._max){this._max=this._current}var i=Math.round(-this._canvas.height*this._current/this._max);this._ctx.drawImage(this._canvas,1,0,this._canvas.width-1,this._canvas.height,0,0,this._canvas.width-1,this._canvas.height);if(a){this._ctx.drawImage(this._canvasAlarm,this._canvas.width-1,i)}else{this._ctx.drawImage(this._canvasDot,this._canvas.width-1,i)}};return e}(e);var i=function(t){function e(e,a){t.call(this,e,a);this._threshold=0;this._canvas2=document.createElement("canvas");this._ctx2=this._canvas2.getContext("2d")}if(t)e.__proto__=t;e.prototype=Object.create(t&&t.prototype);e.prototype.constructor=e;e.prototype.init=function e(a,i){t.prototype.init.call(this,a,i);this._canvas2.width=a;this._canvas2.height=i;this._ctx2.fillStyle="#444";this._ctx2.fillRect(0,0,a,i)};e.prototype.draw=function t(e,a){if(e>this._threshold){var i=(e-e%this._canvas.height)/this._canvas.height;var n=this._canvas.height*(i+1);var s=this._threshold;this._threshold=n;var r=s/n;this._ctx2.drawImage(this._canvas,0,0);this._ctx.fillRect(0,0,this._canvas.width,this._canvas.height);this._ctx.drawImage(this._canvas2,1,0,this._canvas.width-1,this._canvas.height,0,(1-r)*this._canvas.height,this._canvas.width-1,this._canvas.height)}else{this._ctx.drawImage(this._canvas,1,0,this._canvas.width-1,this._canvas.height,0,0,this._canvas.width-1,this._canvas.height)}var o=Math.round(-this._canvas.height*e/this._threshold);if(a){this._ctx.drawImage(this._canvasAlarm,this._canvas.width-1,o)}else{this._ctx.drawImage(this._canvasDot,this._canvas.width-1,o)}};return e}(e);var n=function(t){function e(e,a,i,n){t.call(this,e,a);this._min=i;this._max=n}if(t)e.__proto__=t;e.prototype=Object.create(t&&t.prototype);e.prototype.constructor=e;e.prototype.draw=function t(e,a){var i=(e-this._min)/(this._max-this._min);var n=-Math.ceil(this._canvas.height*i);this._ctx.drawImage(this._canvas,1,0,this._canvas.width-1,this._canvas.height,0,0,this._canvas.width-1,this._canvas.height);if(a){this._ctx.drawImage(this._canvasAlarm,this._canvas.width-1,n)}else{this._ctx.drawImage(this._canvasDot,this._canvas.width-1,n)}};return e}(e);var s=function t(e,a){this._colors=a;this._canvas=document.createElement("canvas");this._ctx=this._canvas.getContext("2d");this._canvas.className="pstats-canvas";e.appendChild(this._canvas)};s.prototype.init=function t(e,a,i){this._canvas.width=e;this._canvas.height=a*i;this._canvas.style.width=e+"px";this._canvas.style.height=a*i+"px";this._ctx.fillStyle="#444";this._ctx.fillRect(0,0,e,a*i)};s.prototype.draw=function t(e){var a=this;this._ctx.drawImage(this._canvas,1,0,this._canvas.width-1,this._canvas.height,0,0,this._canvas.width-1,this._canvas.height);var i=0;for(var n=0;n=this._opts.average){this._averageValue=this._accumValue/this._accumSamples;this._accumValue=0;this._accumStart=a;this._accumSamples=0}}};o.value.get=function(){return this._value};o.value.set=function(t){this._value=t};r.prototype.sample=function t(){this._average(this._value)};r.prototype.human=function t(){var e=this._opts.average?this._averageValue:this._value;return Math.round(e*100)/100};r.prototype.alarm=function t(){return this._opts.below&&this._valuethis._opts.over};Object.defineProperties(r.prototype,o);var c=function(t){function e(e,a){t.call(this,e,a);this._time=window.performance.now()}if(t)e.__proto__=t;e.prototype=Object.create(t&&t.prototype);e.prototype.constructor=e;e.prototype.start=function t(){this._time=window.performance.now()};e.prototype.end=function t(){this._value=window.performance.now()-this._time;this._average(this._value)};e.prototype.tick=function t(){this.end();this.start()};e.prototype.frame=function t(){var e=window.performance.now();var a=e-this._time;this._total++;var i=this._opts.average||1e3;if(a>i){this._value=this._total*1e3/a;this._total=0;this._time=e;this._average(this._value)}};return e}(r);var h=Math.log(1024);var l=["Bytes","KB","MB","GB","TB"];function _(t){var e=100;var a=Math.floor(Math.log(t)/h);if(t===0){return"n/a"}return Math.round(t*e/Math.pow(1024,a))/e+" "+l[a]}var v=function(t){function e(e,a,i){t.call(this,a,i);this._stats=e;this._start=0;if(i.extension.indexOf("memory.")===0){this._field=i.extension.substring(7)}}if(t)e.__proto__=t;e.prototype=Object.create(t&&t.prototype);e.prototype.constructor=e;e.prototype.snapshot=function t(){this._value=this._stats[this._field]};e.prototype.start=function t(){this._start=this._stats[this._field]};e.prototype.end=function t(){this._value=this._stats[this._field]-this._start};e.prototype.human=function e(){return _(t.prototype.human.call(this))};return e}(r);var p=function t(){var e=window.performance.memory;if(e.totalJSHeapSize===0){console.warn("totalJSHeapSize === 0, performance.memory is only available in Chrome.")}this._used=0;this._total=0;this._lastUsed=0};var u={alarm:{},used:{},total:{}};p.prototype.tick=function t(){this._lastUsed=this._used;this._used=window.performance.memory.usedJSHeapSize;this._total=window.performance.memory.totalJSHeapSize};u.alarm.get=function(){return this._used-this._lastUsed<0};u.used.get=function(){return window.performance.memory.usedJSHeapSize};u.total.get=function(){return this._total};p.prototype.counter=function t(e,a){return new v(this,e,a)};Object.defineProperties(p.prototype,u);var d={memory:p};var f=100;var m=10;var x="\n .pstats {\n position: absolute;\n z-index: 9999;\n\n padding: 5px;\n width: "+(f+150)+"px;\n right: 0;\n bottom: 0;\n\n font-size: 10px;\n font-family: 'Roboto Condensed', tahoma, sans-serif;\n overflow: hidden;\n user-select: none;\n cursor: default;\n\n background: #222;\n border-radius: 3px;\n\n }\n\n .pstats-container {\n display: flex;\n flex-direction: column;\n color: #888;\n white-space: nowrap;\n }\n\n .pstats-item {\n display: flex;\n flex-direction: row;\n align-items: center;\n }\n\n .pstats-label {\n display: flex;\n flex-direction: row;\n flex: 1;\n\n text-align: left;\n margin-right: 5px;\n\n transition: background 0.3s;\n }\n\n .pstats-label.alarm {\n color: #ccc;\n background: #800;\n\n transition: background 0s;\n }\n\n .pstats-counter-id {\n flex: 1;\n }\n\n .pstats-counter-value {\n flex: 1;\n text-align: right;\n }\n\n .pstats-fraction {\n display: flex;\n flex-direction: row;\n align-items: center;\n margin-top: 1px;\n margin-bottom: 1px;\n }\n\n .pstats-legend {\n display: flex;\n flex-direction: column;\n flex: 1;\n\n text-align: right;\n margin-right: 5px;\n }\n";var w=document.createElement("style");w.type="text/css";w.textContent=x;document.head.appendChild(w);var g=function t(e,r){var o=this;r=r||{};this._values=r.values||{};this._fractions=r.fractions||[];this._id2counter={};this._id2item={};this._name2extStats={};if(r.css){var c=document.createElement("style");c.type="text/css";c.textContent=r.css;document.head.appendChild(c)}if(r.extensions){for(var h=0;h0);c.valueText.nodeValue=o;c.graph.draw(s.value,r)}}for(var h=0;h