1. /**
  2. * Measures the performance of configuration file loading with a timeout.
  3. *
  4. * @param {string} filePath The path to the configuration file.
  5. * @param {number} timeoutMs The timeout in milliseconds.
  6. * @param {function} callback A callback function to execute with the result.
  7. */
  8. function measureConfigLoad(filePath, timeoutMs, callback) {
  9. const startTime = Date.now();
  10. const timeoutId = setTimeout(() => {
  11. callback({ error: 'Timeout: Configuration file loading timed out.' });
  12. }, timeoutMs);
  13. const fs = require('fs');
  14. fs.readFile(filePath, 'utf8', (err, data) => {
  15. clearTimeout(timeoutId); // Clear the timeout if the file is read successfully.
  16. if (err) {
  17. callback({ error: `Error reading configuration file: ${err.message}` });
  18. return;
  19. }
  20. const endTime = Date.now();
  21. const duration = endTime - startTime;
  22. callback({
  23. success: true,
  24. data: data,
  25. duration: duration,
  26. });
  27. });
  28. }
  29. module.exports = measureConfigLoad;

Add your comment