perfCounter.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { PrecisionDate } from "./precisionDate.js";
  2. /**
  3. * This class is used to track a performance counter which is number based.
  4. * The user has access to many properties which give statistics of different nature.
  5. *
  6. * The implementer can track two kinds of Performance Counter: time and count.
  7. * For time you can optionally call fetchNewFrame() to notify the start of a new frame to monitor, then call beginMonitoring() to start and endMonitoring() to record the lapsed time. endMonitoring takes a newFrame parameter for you to specify if the monitored time should be set for a new frame or accumulated to the current frame being monitored.
  8. * For count you first have to call fetchNewFrame() to notify the start of a new frame to monitor, then call addCount() how many time required to increment the count value you monitor.
  9. */
  10. export class PerfCounter {
  11. /**
  12. * Returns the smallest value ever
  13. */
  14. get min() {
  15. return this._min;
  16. }
  17. /**
  18. * Returns the biggest value ever
  19. */
  20. get max() {
  21. return this._max;
  22. }
  23. /**
  24. * Returns the average value since the performance counter is running
  25. */
  26. get average() {
  27. return this._average;
  28. }
  29. /**
  30. * Returns the average value of the last second the counter was monitored
  31. */
  32. get lastSecAverage() {
  33. return this._lastSecAverage;
  34. }
  35. /**
  36. * Returns the current value
  37. */
  38. get current() {
  39. return this._current;
  40. }
  41. /**
  42. * Gets the accumulated total
  43. */
  44. get total() {
  45. return this._totalAccumulated;
  46. }
  47. /**
  48. * Gets the total value count
  49. */
  50. get count() {
  51. return this._totalValueCount;
  52. }
  53. /**
  54. * Creates a new counter
  55. */
  56. constructor() {
  57. this._startMonitoringTime = 0;
  58. this._min = 0;
  59. this._max = 0;
  60. this._average = 0;
  61. this._lastSecAverage = 0;
  62. this._current = 0;
  63. this._totalValueCount = 0;
  64. this._totalAccumulated = 0;
  65. this._lastSecAccumulated = 0;
  66. this._lastSecTime = 0;
  67. this._lastSecValueCount = 0;
  68. }
  69. /**
  70. * Call this method to start monitoring a new frame.
  71. * This scenario is typically used when you accumulate monitoring time many times for a single frame, you call this method at the start of the frame, then beginMonitoring to start recording and endMonitoring(false) to accumulated the recorded time to the PerfCounter or addCount() to accumulate a monitored count.
  72. */
  73. fetchNewFrame() {
  74. this._totalValueCount++;
  75. this._current = 0;
  76. this._lastSecValueCount++;
  77. }
  78. /**
  79. * Call this method to monitor a count of something (e.g. mesh drawn in viewport count)
  80. * @param newCount the count value to add to the monitored count
  81. * @param fetchResult true when it's the last time in the frame you add to the counter and you wish to update the statistics properties (min/max/average), false if you only want to update statistics.
  82. */
  83. addCount(newCount, fetchResult) {
  84. if (!PerfCounter.Enabled) {
  85. return;
  86. }
  87. this._current += newCount;
  88. if (fetchResult) {
  89. this._fetchResult();
  90. }
  91. }
  92. /**
  93. * Start monitoring this performance counter
  94. */
  95. beginMonitoring() {
  96. if (!PerfCounter.Enabled) {
  97. return;
  98. }
  99. this._startMonitoringTime = PrecisionDate.Now;
  100. }
  101. /**
  102. * Compute the time lapsed since the previous beginMonitoring() call.
  103. * @param newFrame true by default to fetch the result and monitor a new frame, if false the time monitored will be added to the current frame counter
  104. */
  105. endMonitoring(newFrame = true) {
  106. if (!PerfCounter.Enabled) {
  107. return;
  108. }
  109. if (newFrame) {
  110. this.fetchNewFrame();
  111. }
  112. const currentTime = PrecisionDate.Now;
  113. this._current = currentTime - this._startMonitoringTime;
  114. if (newFrame) {
  115. this._fetchResult();
  116. }
  117. }
  118. /**
  119. * Call this method to end the monitoring of a frame.
  120. * This scenario is typically used when you accumulate monitoring time many times for a single frame, you call this method at the end of the frame, after beginMonitoring to start recording and endMonitoring(false) to accumulated the recorded time to the PerfCounter or addCount() to accumulate a monitored count.
  121. */
  122. endFrame() {
  123. this._fetchResult();
  124. }
  125. /** @internal */
  126. _fetchResult() {
  127. this._totalAccumulated += this._current;
  128. this._lastSecAccumulated += this._current;
  129. // Min/Max update
  130. this._min = Math.min(this._min, this._current);
  131. this._max = Math.max(this._max, this._current);
  132. this._average = this._totalAccumulated / this._totalValueCount;
  133. // Reset last sec?
  134. const now = PrecisionDate.Now;
  135. if (now - this._lastSecTime > 1000) {
  136. this._lastSecAverage = this._lastSecAccumulated / this._lastSecValueCount;
  137. this._lastSecTime = now;
  138. this._lastSecAccumulated = 0;
  139. this._lastSecValueCount = 0;
  140. }
  141. }
  142. }
  143. /**
  144. * Gets or sets a global boolean to turn on and off all the counters
  145. */
  146. PerfCounter.Enabled = true;
  147. //# sourceMappingURL=perfCounter.js.map