profiler.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import $ from 'jquery';
  2. import angular from 'angular';
  3. export class Profiler {
  4. panelsRendered: number;
  5. enabled: boolean;
  6. panelsInitCount: any;
  7. timings: any;
  8. digestCounter: any;
  9. $rootScope: any;
  10. scopeCount: any;
  11. init(config, $rootScope) {
  12. this.enabled = config.buildInfo.env === 'development';
  13. this.timings = {};
  14. this.timings.appStart = { loadStart: new Date().getTime() };
  15. this.$rootScope = $rootScope;
  16. if (!this.enabled) {
  17. return;
  18. }
  19. $rootScope.$watch(
  20. () => {
  21. this.digestCounter++;
  22. return false;
  23. },
  24. () => {}
  25. );
  26. $rootScope.onAppEvent('refresh', this.refresh.bind(this), $rootScope);
  27. $rootScope.onAppEvent('dashboard-fetch-end', this.dashboardFetched.bind(this), $rootScope);
  28. $rootScope.onAppEvent('dashboard-initialized', this.dashboardInitialized.bind(this), $rootScope);
  29. $rootScope.onAppEvent('panel-initialized', this.panelInitialized.bind(this), $rootScope);
  30. }
  31. refresh() {
  32. this.timings.query = 0;
  33. this.timings.render = 0;
  34. setTimeout(() => {
  35. console.log('panel count: ' + this.panelsInitCount);
  36. console.log('total query: ' + this.timings.query);
  37. console.log('total render: ' + this.timings.render);
  38. console.log('avg render: ' + this.timings.render / this.panelsInitCount);
  39. }, 5000);
  40. }
  41. dashboardFetched() {
  42. this.timings.dashboardLoadStart = new Date().getTime();
  43. this.panelsInitCount = 0;
  44. this.digestCounter = 0;
  45. this.panelsInitCount = 0;
  46. this.panelsRendered = 0;
  47. this.timings.query = 0;
  48. this.timings.render = 0;
  49. }
  50. dashboardInitialized() {
  51. setTimeout(() => {
  52. console.log('Dashboard::Performance Total Digests: ' + this.digestCounter);
  53. console.log('Dashboard::Performance Total Watchers: ' + this.getTotalWatcherCount());
  54. console.log('Dashboard::Performance Total ScopeCount: ' + this.scopeCount);
  55. var timeTaken = this.timings.lastPanelInitializedAt - this.timings.dashboardLoadStart;
  56. console.log('Dashboard::Performance All panels initialized in ' + timeTaken + ' ms');
  57. // measure digest performance
  58. var rootDigestStart = window.performance.now();
  59. for (var i = 0; i < 30; i++) {
  60. this.$rootScope.$apply();
  61. }
  62. console.log('Dashboard::Performance Root Digest ' + (window.performance.now() - rootDigestStart) / 30);
  63. }, 3000);
  64. }
  65. getTotalWatcherCount() {
  66. var count = 0;
  67. var scopes = 0;
  68. var root = $(document.getElementsByTagName('body'));
  69. var f = function(element) {
  70. if (element.data().hasOwnProperty('$scope')) {
  71. scopes++;
  72. angular.forEach(element.data().$scope.$$watchers, function() {
  73. count++;
  74. });
  75. }
  76. angular.forEach(element.children(), function(childElement) {
  77. f($(childElement));
  78. });
  79. };
  80. f(root);
  81. this.scopeCount = scopes;
  82. return count;
  83. }
  84. renderingCompleted(panelId, panelTimings) {
  85. // add render counter to root scope
  86. // used by phantomjs render.js to know when panel has rendered
  87. this.panelsRendered = (this.panelsRendered || 0) + 1;
  88. this.$rootScope.panelsRendered = this.panelsRendered;
  89. if (this.enabled) {
  90. panelTimings.renderEnd = new Date().getTime();
  91. this.timings.query += panelTimings.queryEnd - panelTimings.queryStart;
  92. this.timings.render += panelTimings.renderEnd - panelTimings.renderStart;
  93. }
  94. }
  95. panelInitialized() {
  96. if (!this.enabled) {
  97. return;
  98. }
  99. this.panelsInitCount++;
  100. this.timings.lastPanelInitializedAt = new Date().getTime();
  101. }
  102. }
  103. var profiler = new Profiler();
  104. export { profiler };