profiler.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 Raymond Hill
  4. Copyright (C) 2019-2022 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uMatrix
  17. */
  18. 'use strict';
  19. /******************************************************************************/
  20. var quickProfiler = (function() {
  21. var timer = performance;
  22. var time = 0;
  23. var count = 0;
  24. var tstart = 0;
  25. var lastlog = timer.now();
  26. var prompt = '';
  27. var reset = function() {
  28. time = 0;
  29. count = 0;
  30. tstart = 0;
  31. };
  32. var avg = function() {
  33. return count > 0 ? time / count : 0;
  34. };
  35. var start = function(s) {
  36. prompt = s || '';
  37. tstart = timer.now();
  38. };
  39. var stop = function(period) {
  40. if ( period === undefined ) {
  41. period = 10000;
  42. }
  43. var now = timer.now();
  44. count += 1;
  45. time += (now - tstart);
  46. if ( (now - lastlog) >= period ) {
  47. console.log('ηMatrix> %s: %s ms (%d samples)', prompt, avg().toFixed(3), count);
  48. lastlog = now;
  49. }
  50. };
  51. return {
  52. reset: reset,
  53. start: start,
  54. stop: stop
  55. };
  56. })();
  57. /******************************************************************************/