Home Reference Source

src/performance/performance-monitor.ts

  1. /*
  2. * Push the performance monitor as the last core component in hls.ts
  3. * so that it is the last class to handle events.
  4. *
  5. * coreComponents.push(new PerformanceMonitor(this));
  6. *
  7. * TODO: Add this to the demo page or a performance test page
  8. */
  9.  
  10. import { Events } from '../events';
  11. import { logger } from '../utils/logger';
  12. import Hls from '../hls';
  13. import type { FragBufferedData } from '../types/events';
  14.  
  15. export default class PerformanceMonitor {
  16. private hls: Hls;
  17.  
  18. constructor(hls: Hls) {
  19. this.hls = hls;
  20. this.hls.on(Events.FRAG_BUFFERED, this.onFragBuffered);
  21. }
  22.  
  23. destroy() {
  24. this.hls.off(Events.FRAG_BUFFERED);
  25. }
  26.  
  27. onFragBuffered(event: Events.FRAG_BUFFERED, data: FragBufferedData) {
  28. logFragStats(data);
  29. }
  30. }
  31.  
  32. function logFragStats(data: FragBufferedData) {
  33. const { frag, part } = data;
  34. const stats = part ? part.stats : frag.stats;
  35. const tLoad = stats.loading.end - stats.loading.start;
  36. const tBuffer = stats.buffering.end - stats.buffering.start;
  37. const tParse = stats.parsing.end - stats.parsing.start;
  38. const tTotal = stats.buffering.end - stats.loading.start;
  39.  
  40. logger.log(`[performance-monitor]: Stats for fragment ${frag.sn} ${
  41. part ? ' part ' + part.index : ''
  42. } of level ${frag.level}:
  43. Size: ${(stats.total / 1024).toFixed(3)} kB
  44. Chunk Count: ${stats.chunkCount}
  45.  
  46. Request: ${stats.loading.start.toFixed(3)} ms
  47. First Byte: ${stats.loading.first.toFixed(3)} ms
  48. Parse Start ${stats.parsing.start.toFixed(3)} ms
  49. Buffering Start: ${stats.buffering.start.toFixed(3)} ms
  50. First Buffer: ${stats.buffering.first.toFixed(3)} ms
  51. Parse End: ${stats.parsing.end.toFixed(3)} ms
  52. Buffering End: ${stats.buffering.end.toFixed(3)} ms
  53.  
  54. Load Duration: ${tLoad.toFixed(3)} ms
  55. Parse Duration: ${tParse.toFixed(3)} ms
  56. Buffer Duration: ${tBuffer.toFixed(3)} ms
  57. End-To-End Duration: ${tTotal.toFixed(3)} ms`);
  58. }