public
Created

This is some assorted code to add support for Chrome Canary timline API to track angular timings. It also outputs some timings to the console.

  • Download Gist
AngularTimings.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
(function(console)
{
"use strict";
var nop = function() {};
var ops = ["log", "info", "error", "warn", "time", "timeEnd", "group", "groupEnd", "timeline", "timelineEnd", "timeStamp"];
for(var x = 0; x < ops.length; x++)
{
if(!console[ops[x]]) { console[ops[x]] = nop; }
}
}(window.console = window.console || { $replaced: true }));
 
(function()
{
"use strict";
window.pageLoad = new Date().getTime();
window.lastLog = new Date().getTime();
console.$log = function $log()
{
try
{
if(typeof(arguments[0]) === "string")
{
var message = "(@%d +%d) " + arguments[0];
var newArgs = [];
 
newArgs.push(message, new Date().getTime() - window.pageLoad, new Date().getTime() - window.lastLog);
for(var i = 1; i < arguments.length; i++)
{
newArgs.push(arguments[i]);
}
return console.log.apply(console, newArgs);
}
else
{
console.log.apply(console, arguments); //Log timings to the console
if(console.timeStamp) { console.timeStamp.apply(console, arguments); } //Mark timing on the timeline
}
}
catch(ex) { console.error(ex); }
finally
{
window.lastLog = new Date().getTime();
}
};
 
console.timeline("Page Cycle"); //Start Timing
}());
 
(function()
{
"use strict";
var App = window.App = window.App || angular.module("App", []);
 
App.updateStatus = function updateStatus(status)
{
if(status) { console.$log('Status change: %s', status); }
var root = App && App.$root;
if(root) { root.$emit('mad-status', status); }
document.documentElement[status ? 'setAttribute' : 'removeAttribute']('data-status', status);
};
 
App.factory('ajaxTrackerService',
['$timeout',
function($timeout)
{
var stack = 0, timeout;
 
var fn = function()
{
if(stack === 0)
{
App.updateStatus('ready');
}
};
 
return {
inc: function()
{
App.updateStatus();
++stack;
$timeout.cancel(timeout);
},
dec: function()
{
if(stack > 0)
{
--stack;
}
if(stack === 0)
{
timeout = $timeout(fn, 50);
}
}
};
}]);
 
App.config(['$httpProvider', function($httpProvider)
{
$httpProvider.responseInterceptors.push(['ajaxTrackerService', function(ajaxTrackerService)
{
return function(promise)
{
var start = new Date().getTime();
ajaxTrackerService.inc();
var cntd = function(r)
{
var now = new Date().getTime();
var elapsed = now - start;
console.$log("Request %s took %d ms to complete", r.config.url, elapsed);
ajaxTrackerService.dec(r);
return r;
};
return promise.then(cntd, cntd);
};
}]);
}]);
 
App.run(['$rootScope', '$window', function($rootScope, $window)
{
$rootScope.$on('routeChangeStart', function()
{
$window.pageLoad = new Date().getTime();
(console.timeline || function() {})('Page Cycle'); //Restart timing on angular navigation
});
 
$rootScope.$on('mad-status', function(e, status)
{
if(status === 'ready')
{
(console.timelineEnd || function() {})('Page Cycle');
}
});
}]);
 
}());

Note that Response Interceptors are deprecated in angular 1.2, but there's some comparable API that is available.

console.timestamp and console timeline api might only work in Chrome Canary.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.