Perf: further speed up infra

This commit is contained in:
SukkaW
2023-09-14 20:15:49 +08:00
parent adb8b43357
commit 78afa595a9
25 changed files with 431 additions and 173 deletions

View File

@@ -1,15 +1,42 @@
const path = require('path');
const { performance } = require('perf_hooks');
/**
* @param {Function} fn
* @param {string} __filename
* @template T
* @param {string} prefix
* @param {() => T} fn
* @returns {T}
*/
module.exports.runner = async (__filename, fn) => {
const runnerName = path.basename(__filename, path.extname(__filename));
const start = Date.now();
const result = await fn();
const end = Date.now();
console.log(`⌛ [${runnerName}]: ${end - start}ms`);
const traceSync = (prefix, fn) => {
const start = performance.now();
const result = fn();
const end = performance.now();
console.log(`${prefix}: ${(end - start).toFixed(3)}ms`);
return result;
};
module.exports.traceSync = traceSync;
/**
* @template T
* @param {string} prefix
* @param {() => Promise<T>} fn
* @returns {Promise<T>}
*/
const traceAsync = async (prefix, fn) => {
const start = performance.now();
const result = await fn();
const end = performance.now();
console.log(`${prefix}: ${(end - start).toFixed(3)}ms`);
return result;
};
module.exports.traceAsync = traceAsync;
/**
* @template T
* @param {string} __filename
* @param {() => Promise<T>} fn
* @returns {T}
*/
module.exports.runner = async (__filename, fn) => {
return traceAsync(`⌛ [${path.basename(__filename, path.extname(__filename))}]`, fn);
};