From 0400168842874f0efc66209ec5a8a9fbf25c1e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 23 Dec 2024 11:41:58 +0100 Subject: [PATCH] chore(deps): apply updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- package.json | 2 +- pnpm-lock.yaml | 92 +++++++++--------- src/bench.ts | 14 ++- src/task.ts | 77 +++++++++++---- test/index.test.ts | 234 ++++++++++++++++++++++++++------------------- 5 files changed, 252 insertions(+), 167 deletions(-) diff --git a/package.json b/package.json index 5ef793c..c4a50e3 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "@size-limit/preset-small-lib": "^11.1.6", "@size-limit/time": "^11.1.6", "@types/node": "^22.10.2", - "bumpp": "^9.9.1", + "bumpp": "^9.9.2", "changelogithub": "^0.13.11", "clean-publish": "^5.1.0", "eslint": "^9.17.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1e262fc..7ff432a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,8 +30,8 @@ importers: specifier: ^22.10.2 version: 22.10.2 bumpp: - specifier: ^9.9.1 - version: 9.9.1 + specifier: ^9.9.2 + version: 9.9.2 changelogithub: specifier: ^0.13.11 version: 0.13.11 @@ -199,8 +199,8 @@ packages: '@cspell/dict-bash@4.1.8': resolution: {integrity: sha512-I2CM2pTNthQwW069lKcrVxchJGMVQBzru2ygsHCwgidXRnJL/NTjAPOFTxN58Jc1bf7THWghfEDyKX/oyfc0yg==} - '@cspell/dict-companies@3.1.9': - resolution: {integrity: sha512-w7XEJ2B6x2jq9ws5XNyYgpYj2MxdZ3jW3PETLxjK7nc8pulCFmaGVgZ0JTnDWfJ3QMOczoagn5f9LM2PZ/CuJg==} + '@cspell/dict-companies@3.1.10': + resolution: {integrity: sha512-KpRLiVDCpTkF+IjWnuYc31B0gyHVh0TSf/MDrWPobl9oYNQRWFUMACAJO9FP+kHI0jzLjTyLC1KpKwqte/88iA==} '@cspell/dict-cpp@6.0.2': resolution: {integrity: sha512-yw5eejWvY4bAnc6LUA44m4WsFwlmgPt2uMSnO7QViGMBDuoeopMma4z9XYvs4lSjTi8fIJs/A1YDfM9AVzb8eg==} @@ -312,8 +312,8 @@ packages: '@cspell/dict-node@5.0.5': resolution: {integrity: sha512-7NbCS2E8ZZRZwlLrh2sA0vAk9n1kcTUiRp/Nia8YvKaItGXLfxYqD2rMQ3HpB1kEutal6hQLVic3N2Yi1X7AaA==} - '@cspell/dict-npm@5.1.18': - resolution: {integrity: sha512-/Nukl+DSxtEWSlb8svWFSpJVctAsM9SP+f5Q1n+qdDcXNKMb1bUCo/d3QZPwyOhuMjDawnsGBUAfp+iq7Mw83Q==} + '@cspell/dict-npm@5.1.19': + resolution: {integrity: sha512-ygpKeCXKURqymycqZEF9r/TytkWZxedDehudqiJLtPI6OZJhx3Kxy3hysiRq7RYpHiK+LSX39bwpxGM/h0Ghnw==} '@cspell/dict-php@4.0.13': resolution: {integrity: sha512-P6sREMZkhElzz/HhXAjahnICYIqB/HSGp1EhZh+Y6IhvC15AzgtDP8B8VYCIsQof6rPF1SQrFwunxOv8H1e2eg==} @@ -339,8 +339,8 @@ packages: '@cspell/dict-scala@5.0.6': resolution: {integrity: sha512-tl0YWAfjUVb4LyyE4JIMVE8DlLzb1ecHRmIWc4eT6nkyDqQgHKzdHsnusxFEFMVLIQomgSg0Zz6hJ5S1E4W4ww==} - '@cspell/dict-software-terms@4.1.20': - resolution: {integrity: sha512-ma51njqbk9ZKzZF9NpCZpZ+c50EwR5JTJ2LEXlX0tX+ExVbKpthhlDLhT2+mkUh5Zvj+CLf5F9z0qB4+X3re/w==} + '@cspell/dict-software-terms@4.1.21': + resolution: {integrity: sha512-LXoFLHzqPeehPMpu4mLpUoP+UVgT8aDfMuuvpsowvDc/nYaMq0UvnPVo632VOi5SUOzFX6NHsQ4x5eP3TRUOFA==} '@cspell/dict-sql@2.1.8': resolution: {integrity: sha512-dJRE4JV1qmXTbbGm6WIcg1knmR6K5RXnQxF4XHs5HA3LAjc/zf77F95i5LC+guOGppVF6Hdl66S2UyxT+SAF3A==} @@ -1032,11 +1032,11 @@ packages: '@sec-ant/readable-stream@0.4.1': resolution: {integrity: sha512-831qok9r2t8AlxLko40y2ebgSDhenenCatLVeW/uBtnHPyhHOvG0C7TvfgecV+wHzIm5KUICgzmVpWS+IMEAeg==} - '@shikijs/engine-oniguruma@1.24.3': - resolution: {integrity: sha512-iNnx950gs/5Nk+zrp1LuF+S+L7SKEhn8k9eXgFYPGhVshKppsYwRmW8tpmAMvILIMSDfrgqZ0w+3xWVQB//1Xw==} + '@shikijs/engine-oniguruma@1.24.4': + resolution: {integrity: sha512-Do2ry6flp2HWdvpj2XOwwa0ljZBRy15HKZITzPcNIBOGSeprnA8gOooA/bLsSPuy8aJBa+Q/r34dMmC3KNL/zw==} - '@shikijs/types@1.24.3': - resolution: {integrity: sha512-FPMrJ69MNxhRtldRk69CghvaGlbbN3pKRuvko0zvbfa2dXp4pAngByToqS5OY5jvN8D7LKR4RJE8UvzlCOuViw==} + '@shikijs/types@1.24.4': + resolution: {integrity: sha512-0r0XU7Eaow0PuDxuWC1bVqmWCgm3XqizIaT7SM42K03vc69LGooT0U8ccSR44xP/hGlNx4FKhtYpV+BU6aaKAA==} '@shikijs/vscode-textmate@9.3.1': resolution: {integrity: sha512-79QfK1393x9Ho60QFyLti+QfdJzRQCVLFb97kOIV7Eo9vQU/roINgk7m24uv0a7AUvN//RDH36FLjjK48v0s9g==} @@ -1337,8 +1337,8 @@ packages: buffer@5.7.1: resolution: {integrity: sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==} - bumpp@9.9.1: - resolution: {integrity: sha512-M2CnokltfM7kLpYigS9PTAljG5pKng2MBTVpjCIXD+StNvhc+pnO+SYVnQDGjEVXD1fw2/Ofrf7wH45fxovoPg==} + bumpp@9.9.2: + resolution: {integrity: sha512-ggRxRV1rWHEyWXnf55UqYzGvttS/Vpkl1zxcNdE5xoYMTHlSgRA0Td4nKn3ckCcMuC+MTgaGQrbKBeyr0V9+Hg==} engines: {node: '>=10'} hasBin: true @@ -1346,8 +1346,8 @@ packages: resolution: {integrity: sha512-PKA4BeSvBpQKQ8iPOGCSiell+N8P+Tf1DlwqmYhpe2gAhKPHn8EYOxVT+ShuGmhg8lN8XiSlS80yiExKXrURlw==} engines: {node: '>=12'} - bundle-require@5.0.0: - resolution: {integrity: sha512-GuziW3fSSmopcx4KRymQEJVbZUfqlCqcq7dvs6TYwKRZiegK/2buMxQTPs6MGlNv50wms1699qYO54R8XfRX4w==} + bundle-require@5.1.0: + resolution: {integrity: sha512-3WrrOuZiyaaZPWiEt4G3+IffISVC9HYlWueJEBWED4ZH4aIAC2PnkdnuRrR94M+w6yGWn4AglWtJtBI8YqvgoA==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} peerDependencies: esbuild: '>=0.18' @@ -1400,8 +1400,8 @@ packages: resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} engines: {node: '>=10'} - chalk@5.4.0: - resolution: {integrity: sha512-ZkD35Mx92acjB2yNJgziGqT9oKHEOxjTBTDRpOsRWtdecL/0jM3z5kM/CTzHWvHIen1GvkM85p6TuFfDGfc8/Q==} + chalk@5.4.1: + resolution: {integrity: sha512-zgVZuo2WcZgfUEmsn6eO3kINexW8RAE4maiQ8QNs8CtpPCSyMiYsULR3HQYkm3w8FIA3SberyMJMSldGsW+U3w==} engines: {node: ^12.17.0 || ^14.13 || >=16.0.0} changelogen@0.5.5: @@ -1924,8 +1924,8 @@ packages: fast-uri@3.0.3: resolution: {integrity: sha512-aLrHthzCjH5He4Z2H9YZ+v6Ujb9ocRuW6ZzkJQOrTxleEijANq4v1TsaPaVG1PZcuurEzrLcWRyYBYXD5cEiaw==} - fastq@1.17.1: - resolution: {integrity: sha512-sRVD3lWVIXWg6By68ZN7vho9a1pQcN/WBFaAAsDDFzlJjvoGx0P8z7V1t72grFJfJhu3YPZBuu25f7Kaw2jN1w==} + fastq@1.18.0: + resolution: {integrity: sha512-QKHXPW0hD8g4UET03SdOdunzSouc9N4AuHdsX8XNcTsuz+yYFILVNIX4l9yHABMhiEI9Db0JTTIpu0wB+Y1QQw==} fd-slicer@1.1.0: resolution: {integrity: sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==} @@ -2196,8 +2196,8 @@ packages: resolution: {integrity: sha512-1BC0BVFhS/p0qtw6enp8e+8OD0UrK0oFLztSjNzhcKA3WDuJxxAPXzPuPtKkjEY9UUoEWlX/8fgKeu2S8i9JTA==} engines: {node: '>= 0.4'} - is-core-module@2.16.0: - resolution: {integrity: sha512-urTSINYfAYgcbLb0yDQ6egFm6h3Mo1DcF9EkyXSRjjzdHbsulg01qhwWuXdOoUBuTkbQ80KDboXa0vFJ+BDH+g==} + is-core-module@2.16.1: + resolution: {integrity: sha512-UfoeMA6fIJ8wTYFEUjelnaGI67v6+N7qXJEvQuIGa99l4xsCruSYOVSQ0uPANn4dAzm8lkYPaKLrrijLq7x23w==} engines: {node: '>= 0.4'} is-data-view@1.0.2: @@ -3574,7 +3574,7 @@ snapshots: '@commitlint/format@19.5.0': dependencies: '@commitlint/types': 19.5.0 - chalk: 5.4.0 + chalk: 5.4.1 '@commitlint/is-ignored@19.6.0': dependencies: @@ -3594,7 +3594,7 @@ snapshots: '@commitlint/execute-rule': 19.5.0 '@commitlint/resolve-extends': 19.5.0 '@commitlint/types': 19.5.0 - chalk: 5.4.0 + chalk: 5.4.1 cosmiconfig: 9.0.0(typescript@5.7.2) cosmiconfig-typescript-loader: 6.1.0(@types/node@22.10.2)(cosmiconfig@9.0.0(typescript@5.7.2))(typescript@5.7.2) lodash.isplainobject: 4.0.6 @@ -3645,7 +3645,7 @@ snapshots: '@commitlint/types@19.5.0': dependencies: '@types/conventional-commits-parser': 5.0.1 - chalk: 5.4.0 + chalk: 5.4.1 '@cspell/cspell-bundled-dicts@8.17.1': dependencies: @@ -3653,7 +3653,7 @@ snapshots: '@cspell/dict-al': 1.0.3 '@cspell/dict-aws': 4.0.7 '@cspell/dict-bash': 4.1.8 - '@cspell/dict-companies': 3.1.9 + '@cspell/dict-companies': 3.1.10 '@cspell/dict-cpp': 6.0.2 '@cspell/dict-cryptocurrencies': 5.0.3 '@cspell/dict-csharp': 4.0.5 @@ -3688,7 +3688,7 @@ snapshots: '@cspell/dict-markdown': 2.0.7(@cspell/dict-css@4.0.16)(@cspell/dict-html-symbol-entities@4.0.3)(@cspell/dict-html@4.0.10)(@cspell/dict-typescript@3.1.11) '@cspell/dict-monkeyc': 1.0.9 '@cspell/dict-node': 5.0.5 - '@cspell/dict-npm': 5.1.18 + '@cspell/dict-npm': 5.1.19 '@cspell/dict-php': 4.0.13 '@cspell/dict-powershell': 5.0.13 '@cspell/dict-public-licenses': 2.0.11 @@ -3697,7 +3697,7 @@ snapshots: '@cspell/dict-ruby': 5.0.7 '@cspell/dict-rust': 4.0.10 '@cspell/dict-scala': 5.0.6 - '@cspell/dict-software-terms': 4.1.20 + '@cspell/dict-software-terms': 4.1.21 '@cspell/dict-sql': 2.1.8 '@cspell/dict-svelte': 1.0.5 '@cspell/dict-swift': 2.0.4 @@ -3723,7 +3723,7 @@ snapshots: '@cspell/dict-bash@4.1.8': {} - '@cspell/dict-companies@3.1.9': {} + '@cspell/dict-companies@3.1.10': {} '@cspell/dict-cpp@6.0.2': {} @@ -3800,7 +3800,7 @@ snapshots: '@cspell/dict-node@5.0.5': {} - '@cspell/dict-npm@5.1.18': {} + '@cspell/dict-npm@5.1.19': {} '@cspell/dict-php@4.0.13': {} @@ -3820,7 +3820,7 @@ snapshots: '@cspell/dict-scala@5.0.6': {} - '@cspell/dict-software-terms@4.1.20': {} + '@cspell/dict-software-terms@4.1.21': {} '@cspell/dict-sql@2.1.8': {} @@ -4118,8 +4118,8 @@ snapshots: '@gerrit0/mini-shiki@1.24.4': dependencies: - '@shikijs/engine-oniguruma': 1.24.3 - '@shikijs/types': 1.24.3 + '@shikijs/engine-oniguruma': 1.24.4 + '@shikijs/types': 1.24.4 '@shikijs/vscode-textmate': 9.3.1 '@humanfs/core@0.19.1': {} @@ -4173,7 +4173,7 @@ snapshots: '@nodelib/fs.walk@1.2.8': dependencies: '@nodelib/fs.scandir': 2.1.5 - fastq: 1.17.1 + fastq: 1.18.0 '@nolyfill/is-core-module@1.0.39': {} @@ -4267,12 +4267,12 @@ snapshots: '@sec-ant/readable-stream@0.4.1': {} - '@shikijs/engine-oniguruma@1.24.3': + '@shikijs/engine-oniguruma@1.24.4': dependencies: - '@shikijs/types': 1.24.3 + '@shikijs/types': 1.24.4 '@shikijs/vscode-textmate': 9.3.1 - '@shikijs/types@1.24.3': + '@shikijs/types@1.24.4': dependencies: '@shikijs/vscode-textmate': 9.3.1 '@types/hast': 3.0.4 @@ -4644,7 +4644,7 @@ snapshots: base64-js: 1.5.1 ieee754: 1.2.1 - bumpp@9.9.1: + bumpp@9.9.2: dependencies: c12: 2.0.1 cac: 6.7.14 @@ -4662,7 +4662,7 @@ snapshots: dependencies: run-applescript: 5.0.0 - bundle-require@5.0.0(esbuild@0.24.2): + bundle-require@5.1.0(esbuild@0.24.2): dependencies: esbuild: 0.24.2 load-tsconfig: 0.2.5 @@ -4733,7 +4733,7 @@ snapshots: ansi-styles: 4.3.0 supports-color: 7.2.0 - chalk@5.4.0: {} + chalk@5.4.1: {} changelogen@0.5.5: dependencies: @@ -5270,7 +5270,7 @@ snapshots: eslint-import-resolver-node@0.3.9: dependencies: debug: 3.2.7 - is-core-module: 2.16.0 + is-core-module: 2.16.1 resolve: 1.22.10 transitivePeerDependencies: - supports-color @@ -5553,7 +5553,7 @@ snapshots: fast-uri@3.0.3: {} - fastq@1.17.1: + fastq@1.18.0: dependencies: reusify: 1.0.4 @@ -5834,7 +5834,7 @@ snapshots: is-callable@1.2.7: {} - is-core-module@2.16.0: + is-core-module@2.16.1: dependencies: hasown: 2.0.2 @@ -6539,13 +6539,13 @@ snapshots: resolve@1.22.10: dependencies: - is-core-module: 2.16.0 + is-core-module: 2.16.1 path-parse: 1.0.7 supports-preserve-symlinks-flag: 1.0.0 resolve@2.0.0-next.5: dependencies: - is-core-module: 2.16.0 + is-core-module: 2.16.1 path-parse: 1.0.7 supports-preserve-symlinks-flag: 1.0.0 @@ -6912,7 +6912,7 @@ snapshots: tsup@8.3.5(jiti@2.4.2)(postcss@8.4.49)(tsx@4.19.2)(typescript@5.7.2)(yaml@2.6.1): dependencies: - bundle-require: 5.0.0(esbuild@0.24.2) + bundle-require: 5.1.0(esbuild@0.24.2) cac: 6.7.14 chokidar: 4.0.3 consola: 3.3.0 diff --git a/src/bench.ts b/src/bench.ts index 6f1ad15..d76bf4e 100644 --- a/src/bench.ts +++ b/src/bench.ts @@ -20,7 +20,14 @@ import { } from './constants' import { createBenchEvent } from './event' import { Task } from './task' -import { invariant, type JSRuntime, mToNs, now, runtime, runtimeVersion } from './utils' +import { + invariant, + type JSRuntime, + mToNs, + now, + runtime, + runtimeVersion, +} from './utils' /** * The Bench class keeps track of the benchmark tasks and controls them. @@ -208,7 +215,10 @@ export class Bench extends EventTarget { } runSync (): Task[] { - invariant(this.concurrency === null, 'Cannot use `concurrency` option when using `runSync`') + invariant( + this.concurrency === null, + 'Cannot use `concurrency` option when using `runSync`' + ) if (this.opts.warmup) { this.warmupTasksSync() } diff --git a/src/task.ts b/src/task.ts index fb21c77..b0d910d 100644 --- a/src/task.ts +++ b/src/task.ts @@ -12,7 +12,12 @@ import type { } from './types' import { createBenchEvent, createErrorEvent } from './event' -import { getStatisticsSorted, invariant, isFnAsyncResource, isPromiseLike } from './utils' +import { + getStatisticsSorted, + invariant, + isFnAsyncResource, + isPromiseLike, +} from './utils' /** * A class that represents each benchmark task in Tinybench. It keeps track of the @@ -124,21 +129,30 @@ export class Task extends EventTarget { return this } - invariant(this.bench.concurrency === null, 'Cannot use `concurrency` option when using `runSync`') + invariant( + this.bench.concurrency === null, + 'Cannot use `concurrency` option when using `runSync`' + ) this.dispatchEvent(createBenchEvent('start', this)) const setupResult = this.bench.opts.setup?.(this, 'run') - invariant(!isPromiseLike(setupResult), '`setup` function must be sync when using `runSync()`') + invariant( + !isPromiseLike(setupResult), + '`setup` function must be sync when using `runSync()`' + ) - const { error, samples: latencySamples } = (this.benchmarkSync( + const { error, samples: latencySamples } = this.benchmarkSync( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.bench.opts.time!, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.bench.opts.iterations! - )) as { error?: Error; samples?: number[] } + ) as { error?: Error; samples?: number[] } const teardownResult = this.bench.opts.teardown?.(this, 'run') - invariant(!isPromiseLike(teardownResult), '`teardown` function must be sync when using `runSync()`') + invariant( + !isPromiseLike(teardownResult), + '`teardown` function must be sync when using `runSync()`' + ) this.processRunResult({ error, latencySamples }) @@ -178,17 +192,23 @@ export class Task extends EventTarget { this.dispatchEvent(createBenchEvent('warmup', this)) const setupResult = this.bench.opts.setup?.(this, 'warmup') - invariant(!isPromiseLike(setupResult), '`setup` function must be sync when using `runSync()`') + invariant( + !isPromiseLike(setupResult), + '`setup` function must be sync when using `runSync()`' + ) - const { error } = (this.benchmarkSync( + const { error } = this.benchmarkSync( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.bench.opts.warmupTime!, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.bench.opts.warmupIterations! - )) as { error?: Error } + ) as { error?: Error } const teardownResult = this.bench.opts.teardown?.(this, 'warmup') - invariant(!isPromiseLike(teardownResult), '`teardown` function must be sync when using `runSync()`') + invariant( + !isPromiseLike(teardownResult), + '`teardown` function must be sync when using `runSync()`' + ) this.postWarmup(error) } @@ -277,7 +297,10 @@ export class Task extends EventTarget { if (this.fnOpts.beforeAll != null) { try { const beforeAllResult = this.fnOpts.beforeAll.call(this) - invariant(!isPromiseLike(beforeAllResult), '`beforeAll` function must be sync when using `runSync()`') + invariant( + !isPromiseLike(beforeAllResult), + '`beforeAll` function must be sync when using `runSync()`' + ) } catch (error) { return { error } } @@ -289,7 +312,10 @@ export class Task extends EventTarget { const benchmarkTask = () => { if (this.fnOpts.beforeEach != null) { const beforeEachResult = this.fnOpts.beforeEach.call(this) - invariant(!isPromiseLike(beforeEachResult), '`beforeEach` function must be sync when using `runSync()`') + invariant( + !isPromiseLike(beforeEachResult), + '`beforeEach` function must be sync when using `runSync()`' + ) } let taskTime = 0 // ms; @@ -301,21 +327,29 @@ export class Task extends EventTarget { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion taskTime = this.bench.opts.now!() - taskStart - invariant(!isPromiseLike(result), 'task function must be sync when using `runSync()`') + invariant( + !isPromiseLike(result), + 'task function must be sync when using `runSync()`' + ) samples.push(taskTime) totalTime += taskTime if (this.fnOpts.afterEach != null) { const afterEachResult = this.fnOpts.afterEach.call(this) - invariant(!isPromiseLike(afterEachResult), '`afterEach` function must be sync when using `runSync()`') + invariant( + !isPromiseLike(afterEachResult), + '`afterEach` function must be sync when using `runSync()`' + ) } } try { while ( // eslint-disable-next-line no-unmodified-loop-condition - (totalTime < time || samples.length < iterations)) { + totalTime < time || + samples.length < iterations + ) { benchmarkTask() } } catch (error) { @@ -325,7 +359,10 @@ export class Task extends EventTarget { if (this.fnOpts.afterAll != null) { try { const afterAllResult = this.fnOpts.afterAll.call(this) - invariant(!isPromiseLike(afterAllResult), '`afterAll` function must be sync when using `runSync()`') + invariant( + !isPromiseLike(afterAllResult), + '`afterAll` function must be sync when using `runSync()`' + ) } catch (error) { return { error } } @@ -355,7 +392,13 @@ export class Task extends EventTarget { } } - private processRunResult ({ error, latencySamples }: { error?: Error, latencySamples?: number[] }): void { + private processRunResult ({ + error, + latencySamples, + }: { + error?: Error + latencySamples?: number[] + }): void { if (latencySamples) { this.runs = latencySamples.length const totalTime = latencySamples.reduce((a, b) => a + b, 0) diff --git a/test/index.test.ts b/test/index.test.ts index b1b08ff..a326054 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -307,9 +307,11 @@ test('events order (async)', async () => { events.push('complete') }) - bench.add('temporary', () => { - // noop - }).remove('temporary') + bench + .add('temporary', () => { + // noop + }) + .remove('temporary') setTimeout(() => { controller.abort() @@ -412,9 +414,11 @@ test('events order (sync)', () => { events.push('complete') }) - bench.add('temporary', () => { - // noop - }).remove('temporary') + bench + .add('temporary', () => { + // noop + }) + .remove('temporary') bench.runSync() bench.reset() @@ -605,57 +609,61 @@ test.each(['warmup', 'run'])('%s throws (sync)', mode => { expect(task?.result?.error).toStrictEqual(error) }) -test('detect faster task (async)', { skip: platform() !== 'linux' }, async () => { - const bench = new Bench({ iterations: 32, time: 100 }) - bench - .add('faster', async () => { - await new Promise(resolve => setTimeout(resolve, 0)) - }) - .add('slower', async () => { - await new Promise(resolve => setTimeout(resolve, 50)) - }) - - await bench.run() - - const fasterTask = bench.getTask('faster') - const slowerTask = bench.getTask('slower') +test( + 'detect faster task (async)', + { skip: platform() !== 'linux' }, + async () => { + const bench = new Bench({ iterations: 32, time: 100 }) + bench + .add('faster', async () => { + await new Promise(resolve => setTimeout(resolve, 0)) + }) + .add('slower', async () => { + await new Promise(resolve => setTimeout(resolve, 50)) + }) - expect(fasterTask?.result?.latency.mean).toBeLessThan( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - slowerTask!.result!.latency.mean - ) - expect(fasterTask?.result?.latency.min).toBeLessThan( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - slowerTask!.result!.latency.min - ) - expect(fasterTask?.result?.latency.max).toBeLessThan( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - slowerTask!.result!.latency.max - ) - // latency moe should be lesser since it's faster - expect(fasterTask?.result?.latency.moe).toBeLessThan( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - slowerTask!.result!.latency.moe - ) + await bench.run() - expect(fasterTask?.result?.throughput.mean).toBeGreaterThan( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - slowerTask!.result!.throughput.mean - ) - expect(fasterTask?.result?.throughput.min).toBeGreaterThan( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - slowerTask!.result!.throughput.min - ) - expect(fasterTask?.result?.throughput.max).toBeGreaterThan( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - slowerTask!.result!.throughput.max - ) - // throughput moe should be greater since it's faster - expect(fasterTask?.result?.throughput.moe).toBeGreaterThan( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - slowerTask!.result!.throughput.moe - ) -}) + const fasterTask = bench.getTask('faster') + const slowerTask = bench.getTask('slower') + + expect(fasterTask?.result?.latency.mean).toBeLessThan( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + slowerTask!.result!.latency.mean + ) + expect(fasterTask?.result?.latency.min).toBeLessThan( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + slowerTask!.result!.latency.min + ) + expect(fasterTask?.result?.latency.max).toBeLessThan( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + slowerTask!.result!.latency.max + ) + // latency moe should be lesser since it's faster + expect(fasterTask?.result?.latency.moe).toBeLessThan( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + slowerTask!.result!.latency.moe + ) + + expect(fasterTask?.result?.throughput.mean).toBeGreaterThan( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + slowerTask!.result!.throughput.mean + ) + expect(fasterTask?.result?.throughput.min).toBeGreaterThan( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + slowerTask!.result!.throughput.min + ) + expect(fasterTask?.result?.throughput.max).toBeGreaterThan( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + slowerTask!.result!.throughput.max + ) + // throughput moe should be greater since it's faster + expect(fasterTask?.result?.throughput.moe).toBeGreaterThan( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + slowerTask!.result!.throughput.moe + ) + } +) test('detect faster task (sync)', { skip: platform() !== 'linux' }, () => { const bench = new Bench({ iterations: 32, time: 100 }) @@ -1055,71 +1063,95 @@ test( } ) -test( - 'task with promiseLike return (sync)', - () => { - const bench = new Bench({ iterations: 16, time: 100 }) +test('task with promiseLike return (sync)', () => { + const bench = new Bench({ iterations: 16, time: 100 }) - bench - .add('foo', async () => { - // noop - }) - .add('fum', () => ({ - then: (resolve: () => void) => Promise.resolve(setTimeout(resolve, 50)), - })) - .add('bar', () => new Promise(resolve => setTimeout(resolve, 50))) + bench + .add('foo', async () => { + // noop + }) + .add('fum', () => ({ + then: (resolve: () => void) => Promise.resolve(setTimeout(resolve, 50)), + })) + .add('bar', () => new Promise(resolve => setTimeout(resolve, 50))) - bench.runSync() + bench.runSync() - expect(bench.getTask('foo')?.result?.error?.message).toStrictEqual('task function must be sync when using `runSync()`') - expect(bench.getTask('fum')?.result?.error?.message).toStrictEqual('task function must be sync when using `runSync()`') - expect(bench.getTask('bar')?.result?.error?.message).toStrictEqual('task function must be sync when using `runSync()`') - } -) + expect(bench.getTask('foo')?.result?.error?.message).toStrictEqual( + 'task function must be sync when using `runSync()`' + ) + expect(bench.getTask('fum')?.result?.error?.message).toStrictEqual( + 'task function must be sync when using `runSync()`' + ) + expect(bench.getTask('bar')?.result?.error?.message).toStrictEqual( + 'task function must be sync when using `runSync()`' + ) +}) -test( - 'async hooks in sync tests', - () => { - const bench = new Bench({ iterations: 16, time: 100 }) +test('async hooks in sync tests', () => { + const bench = new Bench({ iterations: 16, time: 100 }) - bench - .add('async-beforeAll', () => { + bench + .add( + 'async-beforeAll', + () => { // noop - }, { + }, + { beforeAll: async () => { // noop }, - }) - .add('async-beforeEach', () => { + } + ) + .add( + 'async-beforeEach', + () => { // noop - }, { + }, + { beforeEach: async () => { // noop }, - }) - .add('async-afterAll', () => { + } + ) + .add( + 'async-afterAll', + () => { // noop - }, { + }, + { afterAll: async () => { // noop }, - }) - .add('async-afterEach', () => { + } + ) + .add( + 'async-afterEach', + () => { // noop - }, { + }, + { afterEach: async () => { // noop }, - }) + } + ) - bench.runSync() + bench.runSync() - expect(bench.getTask('async-beforeAll')?.result?.error?.message).toStrictEqual('`beforeAll` function must be sync when using `runSync()`') - expect(bench.getTask('async-beforeEach')?.result?.error?.message).toStrictEqual('`beforeEach` function must be sync when using `runSync()`') - expect(bench.getTask('async-afterAll')?.result?.error?.message).toStrictEqual('`afterAll` function must be sync when using `runSync()`') - expect(bench.getTask('async-afterEach')?.result?.error?.message).toStrictEqual('`afterEach` function must be sync when using `runSync()`') - } -) + expect( + bench.getTask('async-beforeAll')?.result?.error?.message + ).toStrictEqual('`beforeAll` function must be sync when using `runSync()`') + expect( + bench.getTask('async-beforeEach')?.result?.error?.message + ).toStrictEqual('`beforeEach` function must be sync when using `runSync()`') + expect(bench.getTask('async-afterAll')?.result?.error?.message).toStrictEqual( + '`afterAll` function must be sync when using `runSync()`' + ) + expect( + bench.getTask('async-afterEach')?.result?.error?.message + ).toStrictEqual('`afterEach` function must be sync when using `runSync()`') +}) test.each(['warmup', 'run'])('%s error handling (async)', async mode => { const bench = new Bench({ warmup: mode === 'warmup' }) @@ -1204,22 +1236,22 @@ test('throw error in beforeAll, afterAll, beforeEach, afterEach (sync)', () => { .add('BA test', () => 1, { beforeAll: () => { throw BAerror - } + }, }) .add('BE test', () => 1, { beforeEach: () => { throw BEerror - } + }, }) .add('AE test', () => 1, { afterEach: () => { throw AEerror - } + }, }) .add('AA test', () => 1, { afterAll: () => { throw AAerror - } + }, }) bench.runSync()