From 8fb54a20f167ea757eb8a953b97c7b80d0642947 Mon Sep 17 00:00:00 2001 From: dej611 Date: Tue, 12 Nov 2024 16:19:04 +0100 Subject: [PATCH] :sparkles: Add time logging --- .../functional/page_objects/log_wrapper.ts | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/x-pack/test/functional/page_objects/log_wrapper.ts b/x-pack/test/functional/page_objects/log_wrapper.ts index 97f5a7a89369f..afcead60b2905 100644 --- a/x-pack/test/functional/page_objects/log_wrapper.ts +++ b/x-pack/test/functional/page_objects/log_wrapper.ts @@ -7,6 +7,10 @@ import { ToolingLog } from '@kbn/tooling-log'; +function isPromise(value: unknown): value is Promise { + return value instanceof Promise; +} + /** * Wraps the specified object instance with debug log statements of all method calls. * @@ -19,17 +23,45 @@ export function logWrapper>( log: ToolingLog, instance: T ): T { + const logger = prepareLogger(log, prefix); return Object.keys(instance).reduce((acc, prop) => { const baseFn = acc[prop]; (acc as Record)[prop] = (...args: unknown[]) => { - logMethodCall(log, prefix, prop, args); - return baseFn.apply(instance, args); + logger.start(prop, args); + const result = baseFn.apply(instance, args); + if (isPromise(result)) { + result.then(logger.end, logger.end); + } else { + logger.end(); + } + return result; }; return acc; }, instance); } -function logMethodCall(log: ToolingLog, prefix: string, prop: string, args: unknown[]) { - const argsStr = args.map((arg) => (typeof arg === 'string' ? `'${arg}'` : arg)).join(', '); - log.debug(`${prefix}.${prop}(${argsStr})`); +function prepareLogger(log: ToolingLog, prefix: string) { + let now = Date.now(); + let currentContext = ''; + + return { + start: (prop: string, args: unknown[]) => { + if (prop === '') { + return; + } + currentContext = `${prop}(${args + .map((arg) => (typeof arg === 'string' ? `'${arg}'` : JSON.stringify(arg))) + .join(', ')})`; + log.debug(`${prefix}.${currentContext}`); + now = Date.now(); + }, + end: () => { + if (currentContext === '') { + return; + } + log.debug(`${prefix}.${currentContext} - (Took ${Date.now() - now} ms)`); + now = Date.now(); + currentContext = ''; + }, + }; }