profiler.ts 3.7 KB

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