選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

stats.ts 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * MARTINS.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022 Alexandre Martins <alemartf(at)gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * stats.ts
  20. * Stats for performance measurements
  21. */
  22. /** update interval, given in seconds */
  23. const UPDATE_INTERVAL = 0.5;
  24. /**
  25. * Stats for performance measurements
  26. */
  27. export class Stats
  28. {
  29. private _timeOfLastUpdate: number;
  30. private _partialCycleCount: number;
  31. private _cyclesPerSecond: number;
  32. /**
  33. * Constructor
  34. */
  35. constructor()
  36. {
  37. this._timeOfLastUpdate = this._now();
  38. this._partialCycleCount = 0;
  39. this._cyclesPerSecond = 0;
  40. }
  41. /**
  42. * Update stats - call every frame
  43. */
  44. update(): void
  45. {
  46. const now = this._now();
  47. ++this._partialCycleCount;
  48. if(now >= this._timeOfLastUpdate + 1000 * UPDATE_INTERVAL) {
  49. this._cyclesPerSecond = this._partialCycleCount / UPDATE_INTERVAL;
  50. this._partialCycleCount = 0;
  51. this._timeOfLastUpdate = now;
  52. }
  53. }
  54. /**
  55. * Reset stats
  56. */
  57. reset(): void
  58. {
  59. this._timeOfLastUpdate = this._now();
  60. this._partialCycleCount = 0;
  61. this._cyclesPerSecond = 0;
  62. }
  63. /**
  64. * Number of cycles per second
  65. */
  66. get cyclesPerSecond(): number
  67. {
  68. return this._cyclesPerSecond;
  69. }
  70. /**
  71. * A measurement of time, in milliseconds
  72. * @returns time in ms
  73. */
  74. private _now(): number
  75. {
  76. return performance.now();
  77. }
  78. }