router-profiler.js
1 /** 2 * Profiling utility for the video router 3 */ 4 import { profileFunction } from './profiler.js'; 5 import { getMultipleVideoSources, _getCidProviderPerformance } from '../app/services/video/router.js'; 6 import { createLogger } from './logger.js'; 7 8 const logger = createLogger('RouterProfiler'); 9 10 /** 11 * Profiles the getMultipleVideoSources function under different conditions 12 * @param {string} cid - Content ID to test with 13 * @param {number} [maxSources=0] - Maximum number of sources to return (0 for unlimited) 14 * @param {string} [altcid=''] - Alternative content ID 15 * @returns {Object} Profiling results 16 */ 17 export function profileGetMultipleVideoSources(cid, maxSources = 0, altcid = '') { 18 if (!cid) { 19 throw new Error('CID is required for profiling'); 20 } 21 22 // Get CID performance data to check if we have history 23 const cidPerformance = _getCidProviderPerformance(cid); 24 const hasCidData = Object.keys(cidPerformance).length > 0; 25 26 logger.info(`Profiling getMultipleVideoSources for CID ${cid.slice(0, 8)}`); 27 logger.info(`CID performance data available: ${hasCidData ? 'Yes' : 'No'}`); 28 29 // Profile the function 30 const profileResults = profileFunction( 31 () => getMultipleVideoSources(cid, maxSources, altcid), 32 [], 33 5 // Run 5 iterations for more reliable results 34 ); 35 36 // Log results 37 logger.info(`Average execution time: ${profileResults.stats.averageTimeMs.toFixed(2)}ms`); 38 logger.info(`Min execution time: ${profileResults.stats.minTimeMs.toFixed(2)}ms`); 39 logger.info(`Max execution time: ${profileResults.stats.maxTimeMs.toFixed(2)}ms`); 40 41 // Log the number of sources returned 42 const sourcesCount = profileResults.results.length; 43 logger.info(`Number of sources returned: ${sourcesCount}`); 44 45 // Log the providers selected 46 const providers = profileResults.results.map(source => source.provider); 47 logger.info(`Selected providers: ${providers.join(', ')}`); 48 49 return { 50 profileStats: profileResults.stats, 51 sources: profileResults.results, 52 hasCidData, 53 providers 54 }; 55 } 56 57 /** 58 * Profiles the getMultipleVideoSources function with and without CID data 59 * @param {string} cid - Content ID with performance data 60 * @param {string} newCid - Content ID without performance data 61 * @param {number} [maxSources=0] - Maximum number of sources to return 62 * @returns {Object} Comparison results 63 */ 64 export function compareWithAndWithoutCidData(cid, newCid, maxSources = 0) { 65 if (!cid || !newCid) { 66 throw new Error('Both CIDs are required for comparison'); 67 } 68 69 logger.info('Comparing getMultipleVideoSources with and without CID data'); 70 71 // Profile with CID data 72 logger.info(`Profiling with CID data (${cid.slice(0, 8)})...`); 73 const withCidData = profileGetMultipleVideoSources(cid, maxSources, ''); 74 75 // Profile without CID data 76 logger.info(`Profiling without CID data (${newCid.slice(0, 8)})...`); 77 const withoutCidData = profileGetMultipleVideoSources(newCid, maxSources, ''); 78 79 // Compare results 80 logger.info('Comparison results:'); 81 logger.info(`With CID data: ${withCidData.profileStats.averageTimeMs.toFixed(2)}ms`); 82 logger.info(`Without CID data: ${withoutCidData.profileStats.averageTimeMs.toFixed(2)}ms`); 83 logger.info(`Difference: ${(withCidData.profileStats.averageTimeMs - withoutCidData.profileStats.averageTimeMs).toFixed(2)}ms`); 84 85 return { 86 withCidData, 87 withoutCidData, 88 difference: withCidData.profileStats.averageTimeMs - withoutCidData.profileStats.averageTimeMs 89 }; 90 } 91 92 /** 93 * Runs a comprehensive profiling of the router under various conditions 94 * @param {Array<string>} cids - Array of CIDs to test with 95 * @returns {Object} Comprehensive profiling results 96 */ 97 export function runComprehensiveRouterProfiling(cids) { 98 if (!Array.isArray(cids) || cids.length === 0) { 99 throw new Error('At least one CID is required for profiling'); 100 } 101 102 const results = {}; 103 104 for (const cid of cids) { 105 logger.info(`Running comprehensive profiling for CID ${cid.slice(0, 8)}`); 106 107 // Profile with different maxSources values 108 const unlimited = profileGetMultipleVideoSources(cid, 0, ''); 109 const limited3 = profileGetMultipleVideoSources(cid, 3, ''); 110 const limited5 = profileGetMultipleVideoSources(cid, 5, ''); 111 112 results[cid] = { 113 unlimited, 114 limited3, 115 limited5 116 }; 117 } 118 119 return results; 120 }