From 5347c5f30e593fb2616011dddab64f1b225d4d41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Mon, 8 Apr 2024 15:41:09 +0200 Subject: [PATCH] Add two simple perf helpers. (#3399) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Can be useful for ad-hoc measure of performance. One is a method decorator, which can be use on methods like so: ```typescript @measureDuration someLongRunningProccess() { // .... } ``` And the other offer more granular control. It also returns what the callback returns, so it can be use in assignments / return statements. ```typescript return measureCbDuration('sorting took', () => renderingShapes.sort(sortById)) ``` ### Change Type - [ ] `sdk` — Changes the tldraw SDK - [ ] `dotcom` — Changes the tldraw.com web app - [ ] `docs` — Changes to the documentation, examples, or templates. - [ ] `vs code` — Changes to the vscode plugin - [x] `internal` — Does not affect user-facing stuff - [ ] `bugfix` — Bug fix - [ ] `feature` — New feature - [ ] `improvement` — Improving existing features - [ ] `chore` — Updating dependencies, other boring stuff - [ ] `galaxy brain` — Architectural changes - [ ] `tests` — Changes to any test code - [x] `tools` — Changes to infrastructure, CI, internal scripts, debugging tools, etc. - [ ] `dunno` — I don't know --- packages/utils/api-report.md | 6 ++++++ packages/utils/src/index.ts | 1 + packages/utils/src/lib/perf.ts | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 packages/utils/src/lib/perf.ts diff --git a/packages/utils/api-report.md b/packages/utils/api-report.md index e1379999c..fdf4cd9b4 100644 --- a/packages/utils/api-report.md +++ b/packages/utils/api-report.md @@ -176,6 +176,12 @@ export function mapObjectMapValues( [K in Key]: ValueAfter; }; +// @internal (undocumented) +export function measureCbDuration(name: string, cb: () => any): any; + +// @internal (undocumented) +export function measureDuration(_target: any, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor; + // @public export class MediaHelpers { static getImageSize(blob: Blob): Promise<{ diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index b008b9753..598a26dd7 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -36,6 +36,7 @@ export { objectMapKeys, objectMapValues, } from './lib/object' +export { measureCbDuration, measureDuration } from './lib/perf' export { PngHelpers } from './lib/png' export { type IndexKey } from './lib/reordering/IndexKey' export { diff --git a/packages/utils/src/lib/perf.ts b/packages/utils/src/lib/perf.ts new file mode 100644 index 000000000..689f8640a --- /dev/null +++ b/packages/utils/src/lib/perf.ts @@ -0,0 +1,22 @@ +/** @internal */ +export function measureCbDuration(name: string, cb: () => any) { + const now = performance.now() + const result = cb() + // eslint-disable-next-line no-console + console.log(`${name} took`, performance.now() - now, 'ms') + return result +} + +/** @internal */ +export function measureDuration(_target: any, propertyKey: string, descriptor: PropertyDescriptor) { + const originalMethod = descriptor.value + descriptor.value = function (...args: any[]) { + const start = performance.now() + const result = originalMethod.apply(this, args) + const end = performance.now() + // eslint-disable-next-line no-console + console.log(`${propertyKey} took ${end - start}ms `) + return result + } + return descriptor +}