profiler.ts 3.6 KB

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