From 38b668da54d4478df4423ffac54aefd4d941f5ad Mon Sep 17 00:00:00 2001 From: Zoe Codez Date: Thu, 19 Sep 2024 08:59:16 -0500 Subject: [PATCH] perf hooks --- src/services/configuration.extension.ts | 8 +++++--- src/services/lifecycle.extension.ts | 5 +++-- src/services/logger.extension.ts | 7 ++++--- src/services/wiring.extension.ts | 19 ++++++++++--------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/services/configuration.extension.ts b/src/services/configuration.extension.ts index ee9cf85..16e0a6a 100644 --- a/src/services/configuration.extension.ts +++ b/src/services/configuration.extension.ts @@ -25,6 +25,8 @@ export const EVENT_CONFIGURATION_UPDATED = "event_configuration_updated"; export const INJECTED_DEFINITIONS = Symbol.for("injected-config"); export type ConfigManager = ReturnType; +const DECIMALS = 2; + export function Configuration({ context, event, @@ -97,7 +99,7 @@ export function Configuration({ internal.boot.application.configurationLoaders ?? ([ConfigLoaderEnvironment, ConfigLoaderFile] as ConfigLoader[]); - const start = Date.now(); + const start = performance.now(); // * were configs disabled? if (is.empty(configLoaders)) { @@ -105,7 +107,7 @@ export function Configuration({ if (!configuration.boilerplate.IS_TEST) { logger.warn({ name: initialize }, `no config loaders defined`); } - return `${Date.now() - start}ms`; + return `${(performance.now() - start).toFixed(DECIMALS)}ms`; } // * load! @@ -121,7 +123,7 @@ export function Configuration({ validateConfig(); - return `${Date.now() - start}ms`; + return `${performance.now() - start}ms`; } function merge(merge: Partial) { diff --git a/src/services/lifecycle.extension.ts b/src/services/lifecycle.extension.ts index 8912292..a095d92 100644 --- a/src/services/lifecycle.extension.ts +++ b/src/services/lifecycle.extension.ts @@ -15,6 +15,7 @@ type EventMapObject = { priority: number; }; const PRE_CALLBACKS_START = 0; +const DECIMALS = 2; export function CreateLifecycle() { const events = new Map( @@ -44,7 +45,7 @@ export function CreateLifecycle() { onShutdownStart: (callback, priority) => attachEvent(callback, "ShutdownStart", priority), } satisfies TLifecycleBase, async exec(stage: LifecycleStages): Promise { - const start = Date.now(); + const start = performance.now(); const list = events.get(stage); events.delete(stage); if (!is.empty(list)) { @@ -82,7 +83,7 @@ export function CreateLifecycle() { async ({ callback }) => await callback(), ); } - return `${Date.now() - start}ms`; + return `${(performance.now() - start).toFixed(DECIMALS)}ms`; }, }; } diff --git a/src/services/logger.extension.ts b/src/services/logger.extension.ts index 4b594a2..fcf3c0e 100644 --- a/src/services/logger.extension.ts +++ b/src/services/logger.extension.ts @@ -28,6 +28,7 @@ const LOG_LEVEL_PRIORITY = { trace: 10, warn: 40, }; +const DECIMALS = 2; const LOG_LEVELS = Object.keys(LOG_LEVEL_PRIORITY) as TConfigLogLevel[]; let logger = {} as Record< @@ -48,7 +49,7 @@ export async function Logger({ internal, als, }: TServiceParams): Promise { - let lastMessage = Date.now(); + let lastMessage = performance.now(); let logCounter = START; let httpLogTarget: string; @@ -158,8 +159,8 @@ export async function Logger({ } if (loggerOptions.ms) { - const now = Date.now(); - const diff = Math.floor(now - lastMessage) + `ms`; + const now = performance.now(); + const diff = (now - lastMessage).toFixed(DECIMALS) + `ms`; lastMessage = now; rawData.ms = diff; diff --git a/src/services/wiring.extension.ts b/src/services/wiring.extension.ts index e4737eb..066762e 100644 --- a/src/services/wiring.extension.ts +++ b/src/services/wiring.extension.ts @@ -164,6 +164,7 @@ const processEvents = new Map([ // ["unhandledRejection", (reason, promise) => {}], ]); +const DECIMALS = 2; const BOILERPLATE = (internal: InternalDefinition) => internal.boot.loadedModules.get("boilerplate") as GetApis>; @@ -368,10 +369,10 @@ async function bootstrap { - start = Date.now(); + start = performance.now(); logger.info({ name: bootstrap }, `[%s] init project`, i.name); await i[WIRE_PROJECT](internal, wireService); - CONSTRUCT[i.name] = `${Date.now() - start}ms`; + CONSTRUCT[i.name] = `${(performance.now() - start).toFixed(DECIMALS)}ms`; }); logger.trace({ name: bootstrap }, `library wiring complete`); @@ -422,9 +423,9 @@ async function bootstrap