GeezerVison is showing github.com — fix: respect bench abort signals in task concurrency (#738) · tinylibs/tinybench@1e5e66a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/concurrency.ts +9-5Lines changed: 9 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,9 +18,10 @@ interface WithConcurrencyOptions<R> { | 18 | 18 | */ | 19 | 19 | limit: number | 20 | 20 | /** | 21 | | - * An optional AbortSignal to cancel the execution. | | 21 | + * An AbortSignal or a list of signals; aborting any of them cancels the | | 22 | + * execution. | 22 | 23 | */ | 23 | | - signal?: AbortSignal | undefined | | 24 | + signal?: AbortSignal | AbortSignal[] | undefined | 24 | 25 | /** | 25 | 26 | * Time budget for starting iterations, in milliseconds. Already-started | 26 | 27 | * calls are awaited without a completion deadline. Zero disables this | @@ -88,9 +89,10 @@ export const withConcurrency = async <R>( | 88 | 89 | } | 89 | 90 |
| 90 | 91 | const onAbort = () => (isRunning = false) | | 92 | + const signals = Array.isArray(signal) ? signal : signal ? [signal] : [] | 91 | 93 |
| 92 | | - if (signal) { | 93 | | - if (signal.aborted) return [] | | 94 | + if (signals.some(signal => signal.aborted)) return [] | | 95 | + for (const signal of signals) { | 94 | 96 | signal.addEventListener('abort', onAbort) | 95 | 97 | } | 96 | 98 |
| @@ -114,7 +116,9 @@ export const withConcurrency = async <R>( | 114 | 116 | const promises = Array.from({ length: maxWorkers }, () => worker()) | 115 | 117 | await Promise.allSettled(promises) | 116 | 118 | } finally { | 117 | | - signal?.removeEventListener('abort', onAbort) | | 119 | + for (const signal of signals) { | | 120 | + signal.removeEventListener('abort', onAbort) | | 121 | + } | 118 | 122 | } | 119 | 123 |
| 120 | 124 | if (errors.length === 0) return results | |
Collapse file Copy file name to clipboardExpand all lines: src/task.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -414,7 +414,9 @@ export class Task extends EventTarget { | 414 | 414 | fn: benchmarkTask, | 415 | 415 | iterations, | 416 | 416 | limit: Math.max(1, Math.floor(this.#bench.threshold)), | 417 | | - signal: this.#signal ?? this.#bench.signal, | | 417 | + signal: [this.#signal, this.#bench.signal].filter( | | 418 | + signal => signal != null | | 419 | + ), | 418 | 420 | time, | 419 | 421 | timestampProvider: this.#timestampProvider, | 420 | 422 | }) | |
Collapse file Copy file name to clipboardExpand all lines: test/task-level-abort.test.ts +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,42 @@ import { expect, test } from 'vitest' | 3 | 3 | import { Bench } from '../src' | 4 | 4 | import { sleep } from './utils' | 5 | 5 |
| | 6 | +test.each([ | | 7 | + ['bench', 'run'], | | 8 | + ['task', 'run'], | | 9 | + ['bench', 'warmup'], | | 10 | + ['task', 'warmup'], | | 11 | +])('stops concurrent scheduling on %s abort during %s', async (source, phase) => { | | 12 | + const benchController = new AbortController() | | 13 | + const taskController = new AbortController() | | 14 | + let clockCalls = 0 | | 15 | + let taskCalls = 0 | | 16 | + const bench = new Bench({ | | 17 | + concurrency: 'task', | | 18 | + iterations: 0, | | 19 | + now: () => ++clockCalls, | | 20 | + signal: benchController.signal, | | 21 | + threshold: 2, | | 22 | + time: 10000, | | 23 | + warmup: phase === 'warmup', | | 24 | + warmupIterations: 0, | | 25 | + warmupTime: 10000, | | 26 | + }) | | 27 | + | | 28 | + bench.add('task', async () => { | | 29 | + taskCalls++ | | 30 | + const controller = source === 'bench' ? benchController : taskController | | 31 | + controller.abort() | | 32 | + await Promise.resolve() | | 33 | + }, { signal: taskController.signal }) | | 34 | + | | 35 | + await bench.run() | | 36 | + | | 37 | + expect(taskCalls).toBe(1) | | 38 | + expect(clockCalls).toBeLessThan(20) | | 39 | + expect(bench.getTask('task')?.result.state).toMatch(/^aborted/) | | 40 | +}) | | 41 | + | 6 | 42 | test('abort individual task (async)', async () => { | 7 | 43 | const controller = new AbortController() | 8 | 44 | const bench = new Bench({ iterations: 16, time: 100 }) | |
Collapse file Copy file name to clipboardExpand all lines: test/with-concurrency.test.ts +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,53 @@ import { performanceNowTimestampProvider } from '../src/timestamp' | 5 | 5 | import { hrtimeNow, withConcurrency } from '../src/utils' | 6 | 6 | import { asyncSleep } from './utils' | 7 | 7 |
| | 8 | +test.each(['completed', 'errored', 'aborted'])( | | 9 | + 'removes all abort listeners after a %s run', | | 10 | + async state => { | | 11 | + const first = new AbortController() | | 12 | + const second = new AbortController() | | 13 | + const failure = new Error('failure') | | 14 | + const run = withConcurrency({ | | 15 | + fn: async () => { | | 16 | + await Promise.resolve() | | 17 | + if (state === 'errored') throw failure | | 18 | + if (state === 'aborted') second.abort() | | 19 | + return 1 | | 20 | + }, | | 21 | + iterations: 1, | | 22 | + limit: 1, | | 23 | + signal: [first.signal, second.signal], | | 24 | + }) | | 25 | + | | 26 | + if (state === 'errored') await expect(run).rejects.toBe(failure) | | 27 | + else expect(await run).toEqual(state === 'aborted' ? [] : [1]) | | 28 | + | | 29 | + expect(getEventListeners(first.signal, 'abort')).toEqual([]) | | 30 | + expect(getEventListeners(second.signal, 'abort')).toEqual([]) | | 31 | + } | | 32 | +) | | 33 | + | | 34 | +test.each([0, 1])('does not add listeners when signal %i was aborted', async index => { | | 35 | + const controllers = [new AbortController(), new AbortController()] | | 36 | + controllers[index]?.abort() | | 37 | + let calls = 0 | | 38 | + const results = await withConcurrency({ | | 39 | + fn: async () => { | | 40 | + calls++ | | 41 | + await Promise.resolve() | | 42 | + }, | | 43 | + iterations: 1, | | 44 | + limit: 1, | | 45 | + signal: controllers.map(controller => controller.signal), | | 46 | + }) | | 47 | + | | 48 | + expect(results).toEqual([]) | | 49 | + expect(calls).toBe(0) | | 50 | + for (const controller of controllers) { | | 51 | + expect(getEventListeners(controller.signal, 'abort')).toEqual([]) | | 52 | + } | | 53 | +}) | | 54 | + | 8 | 55 | test.each(['completed', 'errored', 'aborted'])( | 9 | 56 | 'removes its abort listener after a %s run', | 10 | 57 | async state => { | |
|