mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-01 19:41:54 +08:00
Perf: further speed up infra
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user