profiler.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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(
  28. "dashboard-fetch-end",
  29. this.dashboardFetched.bind(this),
  30. $rootScope
  31. );
  32. $rootScope.onAppEvent(
  33. "dashboard-initialized",
  34. this.dashboardInitialized.bind(this),
  35. $rootScope
  36. );
  37. $rootScope.onAppEvent(
  38. "panel-initialized",
  39. this.panelInitialized.bind(this),
  40. $rootScope
  41. );
  42. }
  43. refresh() {
  44. this.timings.query = 0;
  45. this.timings.render = 0;
  46. setTimeout(() => {
  47. console.log("panel count: " + this.panelsInitCount);
  48. console.log("total query: " + this.timings.query);
  49. console.log("total render: " + this.timings.render);
  50. console.log("avg render: " + this.timings.render / this.panelsInitCount);
  51. }, 5000);
  52. }
  53. dashboardFetched() {
  54. this.timings.dashboardLoadStart = new Date().getTime();
  55. this.panelsInitCount = 0;
  56. this.digestCounter = 0;
  57. this.panelsInitCount = 0;
  58. this.panelsRendered = 0;
  59. this.timings.query = 0;
  60. this.timings.render = 0;
  61. }
  62. dashboardInitialized() {
  63. setTimeout(() => {
  64. console.log(
  65. "Dashboard::Performance Total Digests: " + this.digestCounter
  66. );
  67. console.log(
  68. "Dashboard::Performance Total Watchers: " + this.getTotalWatcherCount()
  69. );
  70. console.log(
  71. "Dashboard::Performance Total ScopeCount: " + this.scopeCount
  72. );
  73. var timeTaken =
  74. this.timings.lastPanelInitializedAt - this.timings.dashboardLoadStart;
  75. console.log(
  76. "Dashboard::Performance All panels initialized in " + timeTaken + " ms"
  77. );
  78. // measure digest performance
  79. var rootDigestStart = window.performance.now();
  80. for (var i = 0; i < 30; i++) {
  81. this.$rootScope.$apply();
  82. }
  83. console.log(
  84. "Dashboard::Performance Root Digest " +
  85. (window.performance.now() - rootDigestStart) / 30
  86. );
  87. }, 3000);
  88. }
  89. getTotalWatcherCount() {
  90. var count = 0;
  91. var scopes = 0;
  92. var root = $(document.getElementsByTagName("body"));
  93. var f = function(element) {
  94. if (element.data().hasOwnProperty("$scope")) {
  95. scopes++;
  96. angular.forEach(element.data().$scope.$$watchers, function() {
  97. count++;
  98. });
  99. }
  100. angular.forEach(element.children(), function(childElement) {
  101. f($(childElement));
  102. });
  103. };
  104. f(root);
  105. this.scopeCount = scopes;
  106. return count;
  107. }
  108. renderingCompleted(panelId, panelTimings) {
  109. // add render counter to root scope
  110. // used by phantomjs render.js to know when panel has rendered
  111. this.panelsRendered = (this.panelsRendered || 0) + 1;
  112. this.$rootScope.panelsRendered = this.panelsRendered;
  113. if (this.enabled) {
  114. panelTimings.renderEnd = new Date().getTime();
  115. this.timings.query += panelTimings.queryEnd - panelTimings.queryStart;
  116. this.timings.render += panelTimings.renderEnd - panelTimings.renderStart;
  117. }
  118. }
  119. panelInitialized() {
  120. if (!this.enabled) {
  121. return;
  122. }
  123. this.panelsInitCount++;
  124. this.timings.lastPanelInitializedAt = new Date().getTime();
  125. }
  126. }
  127. var profiler = new Profiler();
  128. export { profiler };