profiler.ts 3.6 KB

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