/
monitorjs.min.js
1 lines (1 loc) · 2.33 KB
/
monitorjs.min.js
1
var Monitor=function(){var e={},t={};return e.Start=function(e){if("undefined"==typeof e)try{e=arguments.callee.caller.name.toString()}catch(r){throw"Monitor.Start : You must provide a referring name as first argument"}t[e]||(t[e]={tt:0,cc:0,queue:[],nextId:1});var n=t[e].nextId++;return t[e].queue.push({id:n,ct:performance.now()}),{name:e,id:n}},e.Stop=function(e){var r=performance.now(),n=typeof e;if("undefined"===n)try{e=arguments.callee.caller.name.toString()}catch(o){throw"Monitor.Stop : You must provide a referring name as first argument"}var i,a=null;if("string"===n){if(i=e,!t[i]||0===t[i].queue.length)throw"Called Monitor.Stop before Monitor.Start";a=t[i].queue.shift()}else if("object"===n){if(!e.name||!e.id)throw"Monitor.Stop: input should be a string or an object returned by Monitor.Start";if(i=e.name,!t[i]||0===t[i].queue.length)throw"Called Monitor.Stop before Monitor.Start";for(var u=0;u<t[i].queue.length;u++)if(t[i].queue[u].id===e.id){a=t[i].queue.splice(u,1)[0];break}if(null===a)throw"Monitor.Stop: Unable to find record with provided identifier"}t[i].tt+=r-a.ct,t[i].cc++},e.Cancel=function(e){var r=typeof e;if("undefined"===r)try{e=arguments.callee.caller.name.toString()}catch(n){throw"Monitor.Cancel : You must provide a referring name as first argument"}if("string"===r){if(!t[e]||0===t[e].queue.length)throw"Called Monitor.Cancel before Monitor.Start";t[e].queue.shift()}else if("object"===r){if(!e.name||!e.id)throw"Monitor.Cancel: input should be a string or an object returned by Monitor.Start";if(!t[e.name]||0===t[e].queue.length)throw"Called Monitor.Cancel before Monitor.Start";for(var o=null,i=0;i<t[e.name].queue.length;i++)if(t[e.name].queue[i].id===e.id){o=t[e.name].queue.splice(i,1)[0];break}if(null===o)throw"Monitor.Cancel: Unable to find record with provided identifier"}},e.Get=function(e){if("undefined"==typeof e)throw"Monitor.Get: You must specify the record reference";return t[e]?{name:e,total_time:t[e].tt,call_count:t[e].cc}:(console.error('Monitor: no records for "'+e+'"'),null)},e.GetAll=function(){var e=new Array;for(var r in t)e.push({name:r,total_time:t[r].tt,call_count:t[r].cc});return e},e.Clear=function(e){if("undefined"==typeof e)throw"Monitor.Clear: You must specify the record reference";return t[e]?void delete t[e]:(console.error('Monitor: no records for "'+e+'"'),null)},e.ClearAll=function(){t={}},e}();