/**
* Measures the performance of configuration file loading with a timeout.
*
* @param {string} filePath The path to the configuration file.
* @param {number} timeoutMs The timeout in milliseconds.
* @param {function} callback A callback function to execute with the result.
*/
function measureConfigLoad(filePath, timeoutMs, callback) {
const startTime = Date.now();
const timeoutId = setTimeout(() => {
callback({ error: 'Timeout: Configuration file loading timed out.' });
}, timeoutMs);
const fs = require('fs');
fs.readFile(filePath, 'utf8', (err, data) => {
clearTimeout(timeoutId); // Clear the timeout if the file is read successfully.
if (err) {
callback({ error: `Error reading configuration file: ${err.message}` });
return;
}
const endTime = Date.now();
const duration = endTime - startTime;
callback({
success: true,
data: data,
duration: duration,
});
});
}
module.exports = measureConfigLoad;
Add your comment