Guest User

Untitled

a guest
Jul 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. // Package Definition //
  2. var Package = {
  3. NAME: 'Profiler',
  4. VERSION: 0.1,
  5. DEFAULT: ['addProfiling', 'removeProfiling', 'profileReport'],
  6. EXPORT: [ ],
  7. EXPORT_OK: ['_start','_stop','_init'],
  8. EXPORT_TAGS: { }
  9. };
  10.  
  11. // Private Variables //
  12. var _fnlist = {};
  13.  
  14. /**
  15. * Create a tracking object for a new function
  16. * to be profiled.
  17. * @function
  18. */
  19. function _init(fname, fn, scope) {
  20. console.log("Adding " + fname + ": ");
  21. if (typeof _fnlist[fname] == 'undefined') {
  22. _fnlist[fname] = {
  23. fn: fn, // save original function
  24. scope: scope, // object owner of function (context)
  25. ncalls: 0, // number of calls made
  26. calls: {} // individual call profiling
  27. };
  28. }
  29. else
  30. throw new Error(fname + " is already being profiled");
  31. }
  32.  
  33. /**
  34. * Start timing this function, run prior to the
  35. * actual function's execution.
  36. * @function
  37. */
  38. function _start(fname, fn, args) {
  39. _fnlist[fname].ncalls++;
  40. var start = new Date().getTime();
  41. var id = start + "_" + _fnlist[fname].ncalls;
  42. _fnlist[fname].calls[id] = {
  43. start: start,
  44. stop: null,
  45. args: args,
  46. total: 0
  47. };
  48. return id; // unique id for *this* call
  49. }
  50.  
  51. /**
  52. * Stop timing a function and calculate total
  53. * elapsed time (milliseconds)
  54. * @function
  55. */
  56. function _stop(fname, id) {
  57. var ref = _fnlist[fname].calls[id];
  58. ref.stop = new Date().getTime();
  59. ref.total = ref.stop - ref.start;
  60. }
  61.  
  62. /**
  63. * Add profiling for a given function, identified
  64. * by the given label. Returns the newly modified
  65. * function to be assigned to the old function.
  66. * @example addProfiling('func');
  67. * @example addProfiling('func', window);
  68. * @function
  69. */
  70. function addProfiling(fname, scope) {
  71. var scope = scope || window;
  72. _init(fname, scope[fname], scope);
  73. var tmpfn = scope[fname];
  74. scope[fname] = function() {
  75. var id = _start(fname, tmpfn, arguments);
  76. var rv = tmpfn.apply(tmpfn, arguments);
  77. _stop(fname, id);
  78. return rv; // return result of original function
  79. }
  80. }
  81.  
  82. /**
  83. * Remove a function identified by the lable
  84. * from being profiled. Returns original function
  85. * to be reassigned.
  86. * @example removeProfiling('func');
  87. * @function
  88. */
  89. function removeProfiling(fname) {
  90. _fnlist[fname]['scope'][fname] = _fnlist[fname].fn;
  91. delete _fnlist[fname];
  92. }
  93.  
  94. /**
  95. * Report our profiling information via the console.
  96. * @function
  97. */
  98. function profileReport() {
  99. for (fn in _fnlist) {
  100. var obj = _fnlist[fn];
  101. console.log("Function: " + fn + ":");
  102. console.dir(obj);
  103. console.log("\tTotal Calls: " + obj['ncalls']);
  104. var totaltime = 0;
  105. for (call in obj['calls']) {
  106. totaltime += obj['calls'][call].total;
  107. }
  108. console.log("\tTotal Time (sec): " + (totaltime/1000).toFixed(5));
  109. }
  110. }
Add Comment
Please, Sign In to add comment