Skip to content

Commit e5862b6

Browse files
authored
fix!: remove deprecated statistics fields on TaskResult (#407)
1 parent a0182ee commit e5862b6

File tree

3 files changed

+43
-218
lines changed

3 files changed

+43
-218
lines changed

src/task.ts

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const hookNames = ['afterAll', 'beforeAll', 'beforeEach', 'afterEach'] as const
2929

3030
const abortableStates = ['not-started', 'started'] as const
3131

32+
const notStartedTaskResult: TaskResult = { state: 'not-started' }
33+
const abortedTaskResult: TaskResult = { state: 'aborted' }
34+
const startedTaskResult: TaskResult = { state: 'started' }
35+
3236
/**
3337
* A class that represents each benchmark task in Tinybench. It keeps track of the
3438
* results, name, the task function, the number times the task function has been executed, ...
@@ -46,15 +50,6 @@ export class Task extends EventTarget {
4650
options?: RemoveEventListenerOptionsArgument
4751
) => void
4852

49-
/**
50-
* The result object
51-
*/
52-
result: Readonly<TaskResult & TaskResultRuntimeInfo> = {
53-
runtime: 'unknown',
54-
runtimeVersion: 'unknown',
55-
state: 'not-started',
56-
}
57-
5853
/**
5954
* The number of times the task function has been executed
6055
*/
@@ -64,6 +59,14 @@ export class Task extends EventTarget {
6459
return this.#name
6560
}
6661

62+
get result (): TaskResult & TaskResultRuntimeInfo {
63+
return {
64+
...this.#result,
65+
runtime: this.#bench.runtime,
66+
runtimeVersion: this.#bench.runtimeVersion,
67+
}
68+
}
69+
6770
/**
6871
* The task asynchronous status
6972
*/
@@ -89,6 +92,11 @@ export class Task extends EventTarget {
8992
*/
9093
readonly #name: string
9194

95+
/**
96+
* The result object
97+
*/
98+
#result: TaskResult = notStartedTaskResult
99+
92100
/**
93101
* The task-level abort signal
94102
*/
@@ -148,7 +156,7 @@ export class Task extends EventTarget {
148156
if (emit) this.dispatchEvent(new BenchEvent('reset', this))
149157
this.runs = 0
150158

151-
this.#setTaskResult({ state: this.#aborted ? 'aborted' : 'not-started' })
159+
this.#result = this.#aborted ? abortedTaskResult : notStartedTaskResult
152160
}
153161

154162
/**
@@ -157,12 +165,10 @@ export class Task extends EventTarget {
157165
* @internal
158166
*/
159167
async run (): Promise<Task> {
160-
if (this.result.state !== 'not-started') {
168+
if (this.#result.state !== 'not-started') {
161169
return this
162170
}
163-
this.#setTaskResult({
164-
state: 'started'
165-
})
171+
this.#result = { state: 'started' }
166172
this.dispatchEvent(new BenchEvent('start', this))
167173
await this.#bench.opts.setup(this, 'run')
168174
const { error, samples: latencySamples } = (await this.#benchmark(
@@ -183,17 +189,15 @@ export class Task extends EventTarget {
183189
* @internal
184190
*/
185191
runSync (): this {
186-
if (this.result.state !== 'not-started') {
192+
if (this.#result.state !== 'not-started') {
187193
return this
188194
}
189195

190196
invariant(
191197
this.#bench.concurrency === null,
192198
'Cannot use `concurrency` option when using `runSync`'
193199
)
194-
this.#setTaskResult({
195-
state: 'started'
196-
})
200+
this.#result = startedTaskResult
197201
this.dispatchEvent(new BenchEvent('start', this))
198202

199203
const setupResult = this.#bench.opts.setup(this, 'run')
@@ -224,7 +228,7 @@ export class Task extends EventTarget {
224228
* @internal
225229
*/
226230
async warmup (): Promise<void> {
227-
if (this.result.state !== 'not-started') {
231+
if (this.#result.state !== 'not-started') {
228232
return
229233
}
230234
this.dispatchEvent(new BenchEvent('warmup', this))
@@ -244,7 +248,7 @@ export class Task extends EventTarget {
244248
* @internal
245249
*/
246250
warmupSync (): void {
247-
if (this.result.state !== 'not-started') {
251+
if (this.#result.state !== 'not-started') {
248252
return
249253
}
250254

@@ -464,11 +468,9 @@ export class Task extends EventTarget {
464468

465469
#onAbort (): void {
466470
if (
467-
abortableStates.includes(this.result.state as typeof abortableStates[number])
471+
abortableStates.includes(this.#result.state as typeof abortableStates[number])
468472
) {
469-
this.#setTaskResult({
470-
state: 'aborted',
471-
})
473+
this.#result = abortedTaskResult
472474
const ev = new BenchEvent('abort', this)
473475
this.dispatchEvent(ev)
474476
this.#bench.dispatchEvent(ev)
@@ -477,10 +479,9 @@ export class Task extends EventTarget {
477479

478480
#postWarmup (error: Error | undefined): void {
479481
if (error) {
480-
this.#setTaskResult({
481-
error,
482-
state: 'errored',
483-
})
482+
/* eslint-disable perfectionist/sort-objects */
483+
this.#result = { state: 'errored', error }
484+
/* eslint-enable perfectionist/sort-objects */
484485
const ev = new BenchEvent('error', this, error)
485486
this.dispatchEvent(ev)
486487
this.#bench.dispatchEvent(ev)
@@ -520,28 +521,27 @@ export class Task extends EventTarget {
520521
sortSamples(throughputSamples)
521522
const throughputStatistics = getStatisticsSorted(throughputSamples)
522523

523-
this.#setTaskResult({
524-
period: totalTime / this.runs,
524+
/* eslint-disable perfectionist/sort-objects */
525+
this.#result = {
525526
state: this.#aborted ? 'aborted-with-statistics' : 'completed',
526-
totalTime,
527-
// deprecated statistics included for backward compatibility
528-
...latencyStatistics,
529-
hz: throughputStatistics.mean,
530527
latency: latencyStatistics,
528+
period: totalTime / this.runs,
531529
throughput: throughputStatistics,
532-
})
530+
totalTime,
531+
}
532+
/* eslint-enable perfectionist/sort-objects */
533533
} else if (this.#aborted) {
534534
// If aborted with no samples, still set the aborted flag
535-
this.#setTaskResult({
536-
state: 'aborted',
537-
})
535+
this.#result = abortedTaskResult
538536
}
539537

540538
if (error) {
541-
this.#setTaskResult({
542-
error,
539+
/* eslint-disable perfectionist/sort-objects */
540+
this.#result = {
543541
state: 'errored',
544-
})
542+
error,
543+
}
544+
/* eslint-enable perfectionist/sort-objects */
545545
const ev = new BenchEvent('error', this, error)
546546
this.dispatchEvent(ev)
547547
this.#bench.dispatchEvent(ev)
@@ -556,18 +556,6 @@ export class Task extends EventTarget {
556556
// cycle and complete are equal in Task
557557
this.dispatchEvent(new BenchEvent('complete', this))
558558
}
559-
560-
/**
561-
* set the result object values
562-
* @param result - the task result object to merge with the current result object values
563-
*/
564-
#setTaskResult (result: TaskResult): void {
565-
this.result = Object.freeze({
566-
runtime: this.#bench.runtime,
567-
runtimeVersion: this.#bench.runtimeVersion,
568-
...result,
569-
})
570-
}
571559
}
572560

573561
/**

src/types.ts

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ export interface TaskResultStarted {
382382
state: 'started'
383383
}
384384

385-
export interface TaskResultWithStatistics extends DeprecatedStatistics {
385+
export interface TaskResultWithStatistics {
386386
/**
387387
* the task latency statistics
388388
*/
@@ -403,101 +403,3 @@ export interface TaskResultWithStatistics extends DeprecatedStatistics {
403403
*/
404404
totalTime: number
405405
}
406-
407-
interface DeprecatedStatistics {
408-
/**
409-
* the latency samples critical value
410-
* @deprecated use `.latency.critical` instead
411-
*/
412-
critical: number
413-
414-
/**
415-
* the latency samples degrees of freedom
416-
* @deprecated use `.latency.df` instead
417-
*/
418-
df: number
419-
420-
/**
421-
* the number of operations per second
422-
* @deprecated use `.throughput.mean` instead
423-
*/
424-
hz: number
425-
426-
/**
427-
* the maximum latency samples value
428-
* @deprecated use `.latency.max` instead
429-
*/
430-
max: number
431-
432-
/**
433-
* the latency samples mean/average
434-
* @deprecated use `.latency.mean` instead
435-
*/
436-
mean: number
437-
438-
/**
439-
* the minimum latency samples value
440-
* @deprecated use `.latency.min` instead
441-
*/
442-
min: number
443-
444-
/**
445-
* the latency samples margin of error
446-
* @deprecated use `.latency.moe` instead
447-
*/
448-
moe: number
449-
450-
/**
451-
* the latency samples p75 percentile
452-
* @deprecated use `.latency.p75` instead
453-
*/
454-
p75: number
455-
456-
/**
457-
* the latency samples p99 percentile
458-
* @deprecated use `.latency.p99` instead
459-
*/
460-
p99: number
461-
462-
/**
463-
* the latency samples p995 percentile
464-
* @deprecated use `.latency.p995` instead
465-
*/
466-
p995: number
467-
468-
/**
469-
* the latency samples p999 percentile
470-
* @deprecated use `.latency.p999` instead
471-
*/
472-
p999: number
473-
474-
/**
475-
* the latency samples relative margin of error
476-
* @deprecated use `.latency.rme` instead
477-
*/
478-
rme: number
479-
480-
/**
481-
* latency samples (ms)
482-
* @deprecated use `.latency.samples` instead
483-
*/
484-
samples: number[]
485-
486-
/**
487-
* the latency samples standard deviation
488-
* @deprecated use `.latency.sd` instead
489-
*/
490-
sd: number
491-
492-
/**
493-
* the latency standard error of the mean (a.k.a. the standard deviation of the distribution of the sample mean/average)
494-
* @deprecated use `.latency.sem` instead
495-
*/
496-
sem: number
497-
498-
/**
499-
* the latency samples variance
500-
* @deprecated use `.latency.variance` instead
501-
*/
502-
variance: number
503-
}

0 commit comments

Comments
 (0)