|
| 1 | +import logger, { LoggerLevel } from "../ts-common/logger"; |
| 2 | +import { MediaType } from "../wasm/wasp_hls"; |
| 3 | + |
| 4 | +export interface HiddenTransmuxProfilingConfig { |
| 5 | + transmuxProfiling?: boolean; |
| 6 | + transmuxProfilingSampleSize?: number; |
| 7 | + transmuxProfilingSlowThreshold?: number; |
| 8 | +} |
| 9 | + |
| 10 | +interface TransmuxProfilingConfig { |
| 11 | + enabled: boolean; |
| 12 | + sampleSize: number; |
| 13 | + slowThreshold: number; |
| 14 | +} |
| 15 | + |
| 16 | +interface TransmuxProfilingTotals { |
| 17 | + count: number; |
| 18 | + totalDurationMs: number; |
| 19 | + maxDurationMs: number; |
| 20 | + totalInputBytes: number; |
| 21 | + totalOutputBytes: number; |
| 22 | +} |
| 23 | + |
| 24 | +const DEFAULT_CONFIG: TransmuxProfilingConfig = { |
| 25 | + enabled: false, |
| 26 | + sampleSize: 20, |
| 27 | + slowThreshold: 12, |
| 28 | +}; |
| 29 | + |
| 30 | +const currentConfig: TransmuxProfilingConfig = { ...DEFAULT_CONFIG }; |
| 31 | +let totals = createEmptyTotals(); |
| 32 | + |
| 33 | +function createEmptyTotals(): TransmuxProfilingTotals { |
| 34 | + return { |
| 35 | + count: 0, |
| 36 | + totalDurationMs: 0, |
| 37 | + maxDurationMs: 0, |
| 38 | + totalInputBytes: 0, |
| 39 | + totalOutputBytes: 0, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function clampPositiveInteger(value: number, fallback: number): number { |
| 44 | + if (!Number.isFinite(value) || value < 1) { |
| 45 | + return fallback; |
| 46 | + } |
| 47 | + return Math.floor(value); |
| 48 | +} |
| 49 | + |
| 50 | +function clampNonNegativeNumber(value: number, fallback: number): number { |
| 51 | + if (!Number.isFinite(value) || value < 0) { |
| 52 | + return fallback; |
| 53 | + } |
| 54 | + return value; |
| 55 | +} |
| 56 | + |
| 57 | +function formatBytesPerMs(bytes: number, durationMs: number): string { |
| 58 | + if (durationMs <= 0) { |
| 59 | + return "n/a"; |
| 60 | + } |
| 61 | + return (bytes / durationMs).toFixed(1); |
| 62 | +} |
| 63 | + |
| 64 | +function mediaTypeToString(mediaType: MediaType): string { |
| 65 | + switch (mediaType) { |
| 66 | + case MediaType.Audio: |
| 67 | + return "audio"; |
| 68 | + case MediaType.Video: |
| 69 | + return "video"; |
| 70 | + default: |
| 71 | + return "unknown"; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export function updateTransmuxProfilingConfig( |
| 76 | + config: HiddenTransmuxProfilingConfig, |
| 77 | +): void { |
| 78 | + const previousEnabled = currentConfig.enabled; |
| 79 | + if (config.transmuxProfiling !== undefined) { |
| 80 | + currentConfig.enabled = config.transmuxProfiling; |
| 81 | + } |
| 82 | + if (config.transmuxProfilingSampleSize !== undefined) { |
| 83 | + currentConfig.sampleSize = clampPositiveInteger( |
| 84 | + config.transmuxProfilingSampleSize, |
| 85 | + DEFAULT_CONFIG.sampleSize, |
| 86 | + ); |
| 87 | + } |
| 88 | + if (config.transmuxProfilingSlowThreshold !== undefined) { |
| 89 | + currentConfig.slowThreshold = clampNonNegativeNumber( |
| 90 | + config.transmuxProfilingSlowThreshold, |
| 91 | + DEFAULT_CONFIG.slowThreshold, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + if (currentConfig.enabled !== previousEnabled || !currentConfig.enabled) { |
| 96 | + resetTransmuxProfiling(); |
| 97 | + } |
| 98 | + |
| 99 | + if ( |
| 100 | + currentConfig.enabled && |
| 101 | + !previousEnabled && |
| 102 | + logger.hasLevel(LoggerLevel.Info) |
| 103 | + ) { |
| 104 | + logger.info( |
| 105 | + "[transmux-profile] enabled sampleSize=", |
| 106 | + currentConfig.sampleSize, |
| 107 | + "slowThresholdMs=", |
| 108 | + currentConfig.slowThreshold, |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export function resetTransmuxProfiling(): void { |
| 114 | + totals = createEmptyTotals(); |
| 115 | +} |
| 116 | + |
| 117 | +export function recordTransmuxProfile(args: { |
| 118 | + durationMs: number; |
| 119 | + inputBytes: number; |
| 120 | + outputBytes: number; |
| 121 | + mediaType: MediaType; |
| 122 | +}): void { |
| 123 | + if (!currentConfig.enabled) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + totals.count += 1; |
| 128 | + totals.totalDurationMs += args.durationMs; |
| 129 | + totals.maxDurationMs = Math.max(totals.maxDurationMs, args.durationMs); |
| 130 | + totals.totalInputBytes += args.inputBytes; |
| 131 | + totals.totalOutputBytes += args.outputBytes; |
| 132 | + |
| 133 | + if ( |
| 134 | + args.durationMs >= currentConfig.slowThreshold && |
| 135 | + logger.hasLevel(LoggerLevel.Info) |
| 136 | + ) { |
| 137 | + logger.info( |
| 138 | + "[transmux-profile] slow-segment mediaType=", |
| 139 | + mediaTypeToString(args.mediaType), |
| 140 | + "durationMs=", |
| 141 | + args.durationMs.toFixed(2), |
| 142 | + "inputBytes=", |
| 143 | + args.inputBytes, |
| 144 | + "outputBytes=", |
| 145 | + args.outputBytes, |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + if ( |
| 150 | + totals.count % currentConfig.sampleSize === 0 && |
| 151 | + logger.hasLevel(LoggerLevel.Info) |
| 152 | + ) { |
| 153 | + logger.info( |
| 154 | + "[transmux-profile] summary segments=", |
| 155 | + totals.count, |
| 156 | + "avgMs=", |
| 157 | + (totals.totalDurationMs / totals.count).toFixed(2), |
| 158 | + "maxMs=", |
| 159 | + totals.maxDurationMs.toFixed(2), |
| 160 | + "avgInputBytes=", |
| 161 | + Math.round(totals.totalInputBytes / totals.count), |
| 162 | + "avgOutputBytes=", |
| 163 | + Math.round(totals.totalOutputBytes / totals.count), |
| 164 | + "avgInputBytesPerMs=", |
| 165 | + formatBytesPerMs(totals.totalInputBytes, totals.totalDurationMs), |
| 166 | + "avgOutputBytesPerMs=", |
| 167 | + formatBytesPerMs(totals.totalOutputBytes, totals.totalDurationMs), |
| 168 | + ); |
| 169 | + } |
| 170 | +} |
0 commit comments