You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

127 lines
4.3 KiB

(function (global) {
'use strict';
/**
* Auto engine markers — no project invasion.
* @constructor
* @param {InstanceType<typeof PWPerfSampler>} sampler
*/
function AutoMarkers(sampler) {
this.sampler = sampler;
this.enabledAssetSpike = true;
this.assetSpikeThreshold = 20;
this._cc = null;
this._lastScene = '';
this._bundles = {};
this._lastAssetCount = 0;
this._pollTimer = 0;
this._boundScene = null;
}
/** @param {*} cc */
AutoMarkers.prototype.bindEngine = function (cc) {
var self = this;
this._cc = cc;
if (!cc) return;
this.dispose();
var director = cc.director;
var Director = cc.Director;
if (director && Director && Director.EVENT_AFTER_SCENE_LAUNCH) {
this._boundScene = function () {
var scene = director.getScene();
var name = scene && scene.name ? scene.name : 'unknown';
if (name !== self._lastScene) {
self._lastScene = name;
self.sampler.mark('scene:' + name);
}
};
director.on(Director.EVENT_AFTER_SCENE_LAUNCH, this._boundScene);
}
var game = cc.game;
var Game = cc.Game;
if (game && Game) {
if (!this._boundHide && Game.EVENT_HIDE) {
this._boundHide = function () { self.sampler.mark('game:hide'); };
game.on(Game.EVENT_HIDE, this._boundHide);
}
if (!this._boundShow && Game.EVENT_SHOW) {
this._boundShow = function () { self.sampler.mark('game:show'); };
game.on(Game.EVENT_SHOW, this._boundShow);
}
}
this._pollTimer = setInterval(function () { self._poll(); }, 1000);
this._poll();
};
/** Stop polling / listeners. */
AutoMarkers.prototype.dispose = function () {
if (this._pollTimer) {
clearInterval(this._pollTimer);
this._pollTimer = 0;
}
var cc = this._cc;
if (cc && cc.director && this._boundScene && cc.Director) {
cc.director.off(cc.Director.EVENT_AFTER_SCENE_LAUNCH, this._boundScene);
}
this._boundScene = null;
if (cc && cc.game) {
if (this._boundHide && cc.Game && cc.Game.EVENT_HIDE) cc.game.off(cc.Game.EVENT_HIDE, this._boundHide);
if (this._boundShow && cc.Game && cc.Game.EVENT_SHOW) cc.game.off(cc.Game.EVENT_SHOW, this._boundShow);
}
this._boundHide = null;
this._boundShow = null;
};
AutoMarkers.prototype._poll = function () {
var cc = this._cc;
if (!cc) return;
var scene = cc.director && cc.director.getScene ? cc.director.getScene() : null;
var name = scene && scene.name ? scene.name : '';
if (name && name !== this._lastScene) {
this._lastScene = name;
this.sampler.mark('scene:' + name);
}
if (cc.assetManager && cc.assetManager.bundles) {
var self = this;
try {
cc.assetManager.bundles.forEach(function (bundle, bundleName) {
var key = bundleName || (bundle && bundle.name) || '';
if (!key) return;
if (!self._bundles[key]) {
self._bundles[key] = true;
self.sampler.mark('bundle:' + key);
}
});
} catch (e) {
console.warn('[Workbench] bundle poll failed', e);
}
}
if (!this.enabledAssetSpike || !cc.assetManager || !cc.assetManager.assets) return;
var assets = cc.assetManager.assets;
var count = 0;
if (typeof assets.size === 'number' && isFinite(assets.size)) {
count = assets.size;
} else {
try {
assets.forEach(function () { count += 1; });
} catch (e2) {
return;
}
}
if (this._lastAssetCount > 0) {
var delta = count - this._lastAssetCount;
if (delta >= this.assetSpikeThreshold) {
this.sampler.mark('assets:+' + delta);
}
}
this._lastAssetCount = count;
};
global.PWAutoMarkers = AutoMarkers;
})(window);