profiler.ts 3.5 KB

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