From fd7ac46a12e822cb266325eaf5168d3ede68ed3f Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Mon, 12 Nov 2012 04:53:49 -0500 Subject: [PATCH] refactored performance caching to seperate service --- js/controllers/PerfCtrl.js | 9 ++++--- js/services/appContext.js | 42 ------------------------------ js/services/appPerf.js | 53 ++++++++++++++++++++++++++++++++++++++ panel.html | 1 + 4 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 js/services/appPerf.js diff --git a/js/controllers/PerfCtrl.js b/js/controllers/PerfCtrl.js index 796157f..a68b0d2 100644 --- a/js/controllers/PerfCtrl.js +++ b/js/controllers/PerfCtrl.js @@ -1,4 +1,4 @@ -panelApp.controller('PerfCtrl', function PerfCtrl($scope, appContext, filesystem) { +panelApp.controller('PerfCtrl', function PerfCtrl($scope, appContext, appPerf, filesystem) { $scope.histogram = []; @@ -8,7 +8,7 @@ panelApp.controller('PerfCtrl', function PerfCtrl($scope, appContext, filesystem $scope.max = 100; $scope.clearHistogram = function () { - appContext.clearHistogram(); + appPerf.clear(); }; $scope.exportData = function () { @@ -40,7 +40,10 @@ panelApp.controller('PerfCtrl', function PerfCtrl($scope, appContext, filesystem }; var updateTree = function () { - $scope.histogram = appContext.getHistogram(); + appPerf.get(function (histogram) { + $scope.histogram = histogram; + }); + var roots = appContext.getListOfRoots(); if (!roots) { return; diff --git a/js/services/appContext.js b/js/services/appContext.js index 525906c..0bdb6f7 100644 --- a/js/services/appContext.js +++ b/js/services/appContext.js @@ -19,14 +19,12 @@ panelApp.factory('appContext', function (chromeExtension) { } return { deps: window.__ngDebug.getDeps(), - watchPerf: window.__ngDebug.getWatchPerf(), roots: window.__ngDebug.getRootScopeIds() }; }, function (data) { if (data) { _debugCache = data; - _incomingHistogramData = data.watchPerf; } _pollListeners.forEach(function (fn) { fn(); @@ -39,35 +37,6 @@ panelApp.factory('appContext', function (chromeExtension) { getDebugData(); - var _histogramCache = []; - var _incomingHistogramData = []; - var _watchNameToPerf = {}; - var _totalCache = 0; - - var processHistogram = function () { - if (_incomingHistogramData.length === 0) { - return; - } - - _incomingHistogramData.forEach(function (info) { - _totalCache += info.time; - - if (_watchNameToPerf[info.name]) { - _watchNameToPerf[info.name].time += info.time; - } else { - _watchNameToPerf[info.name] = info; - _histogramCache.push(info); - } - }); - - // recalculate all percentages - _histogramCache.forEach(function (item) { - item.percent = (100 * item.time / _totalCache).toPrecision(3); - }); - - // clear the incoming queue - _incomingHistogramData = []; - }; // Public API // ========== @@ -100,11 +69,6 @@ panelApp.factory('appContext', function (chromeExtension) { // Getters // ------- - getHistogram: function () { - processHistogram(); - return _histogramCache; - }, - getListOfRoots: function () { return _debugCache.roots; }, @@ -170,12 +134,6 @@ panelApp.factory('appContext', function (chromeExtension) { // Actions // ------- - clearHistogram: function (cb) { - chromeExtension.eval(function (window) { - window.__ngDebug.watchPerf = {}; - }, cb); - }, - refresh: function (cb) { chromeExtension.eval(function (window) { window.document.location.reload(); diff --git a/js/services/appPerf.js b/js/services/appPerf.js new file mode 100644 index 0000000..a5ac748 --- /dev/null +++ b/js/services/appPerf.js @@ -0,0 +1,53 @@ +// Service for retrieving and caching performance data +panelApp.factory('appPerf', function (chromeExtension) { + + var _histogramCache = [], + _watchNameToPerf = {}, + _totalCache = 0; + + var getHistogramData = function (callback) { + chromeExtension.eval(function (window) { + if (!window.__ngDebug) { + return {}; + } + return window.__ngDebug.getWatchPerf(); + }, + function (data) { + if (data && data.length) { + updateHistogram(data); + } + callback(); + }); + }; + + var updateHistogram = function (data) { + data.forEach(function (info) { + _totalCache += info.time; + + if (_watchNameToPerf[info.name]) { + _watchNameToPerf[info.name].time += info.time; + } else { + _watchNameToPerf[info.name] = info; + _histogramCache.push(info); + } + }); + + // recalculate all percentages + _histogramCache.forEach(function (item) { + item.percent = (100 * item.time / _totalCache).toPrecision(3); + }); + }; + + // Public API + // ========== + return { + get: function (callback) { + getHistogramData(function () { + callback(_histogramCache); + }); + }, + clear: function () { + _histogramCache = []; + } + }; +}); diff --git a/panel.html b/panel.html index de5a103..b486533 100644 --- a/panel.html +++ b/panel.html @@ -29,6 +29,7 @@ +