Home Reference Source

cables_dev/cables/src/core/core_profiler.js

  1. import { now } from "./timer.js";
  2.  
  3. class Profiler
  4. {
  5. constructor(patch)
  6. {
  7. this.startFrame = patch.getFrameNum();
  8. this.items = {};
  9. this.currentId = null;
  10. this.currentStart = 0;
  11. this._patch = patch;
  12. }
  13.  
  14. getItems()
  15. {
  16. return this.items;
  17. }
  18.  
  19. clear()
  20. {
  21. if (this.paused) return;
  22. this.items = {};
  23. }
  24.  
  25. togglePause()
  26. {
  27. this.paused = !this.paused;
  28. if (!this.paused)
  29. {
  30. this.items = {};
  31. this.currentStart = performance.now();
  32. }
  33. }
  34.  
  35. add(type, object)
  36. {
  37. if (this.paused) return;
  38.  
  39. if (this.currentId !== null)
  40. {
  41. if (!object || object.id != this.currentId)
  42. {
  43. if (this.items[this.currentId])
  44. {
  45. this.items[this.currentId].timeUsed += performance.now() - this.currentStart;
  46.  
  47. if (!this.items[this.currentId].peakTime || now() - this.items[this.currentId].peakTime > 5000)
  48. {
  49. this.items[this.currentId].peak = 0;
  50. this.items[this.currentId].peakTime = now();
  51. }
  52. this.items[this.currentId].peak = Math.max(this.items[this.currentId].peak, performance.now() - this.currentStart);
  53. }
  54. }
  55. }
  56.  
  57. if (object !== null)
  58. {
  59. if (!this.items[object.id])
  60. {
  61. this.items[object.id] = {
  62. "numTriggers": 0,
  63. "timeUsed": 0,
  64. };
  65. }
  66.  
  67. if (this.items[object.id].lastFrame != this._patch.getFrameNum()) this.items[object.id].numTriggers = 0;
  68.  
  69. this.items[object.id].lastFrame = this._patch.getFrameNum();
  70. this.items[object.id].numTriggers++;
  71. this.items[object.id].opid = object.op.id;
  72. this.items[object.id].title = object.op.name + "." + object.name;
  73. this.items[object.id].subPatch = object.op.uiAttribs.subPatch;
  74.  
  75. this.currentId = object.id;
  76. this.currentStart = performance.now();
  77. }
  78. else
  79. {
  80. this.currentId = null;
  81. }
  82. }
  83.  
  84. print()
  85. {
  86. console.log("--------");
  87. for (const i in this.items)
  88. {
  89. console.log(this.items[i].title + ": " + this.items[i].numTriggers + " / " + this.items[i].timeUsed);
  90. }
  91. }
  92. }
  93.  
  94. export { Profiler };