Skip to content

Commit f56dc0c

Browse files
authored
fix(ui): use execution time from ws reporter (onFinished) (#8975)
1 parent 7c9edff commit f56dc0c

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

packages/ui/client/composables/client/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ export const client = (function createVitestClient() {
4040
explorerTree.resumeRun(packs, events)
4141
testRunState.value = 'running'
4242
},
43-
onFinished(_files, errors) {
44-
explorerTree.endRun()
43+
onSpecsCollected(_specs, startTime) {
44+
explorerTree.startTime = startTime || performance.now()
45+
},
46+
onFinished(_files, errors, _coverage, executionTime) {
47+
explorerTree.endRun(executionTime)
4548
// don't change the testRunState.value here:
4649
// - when saving the file in the codemirror requires explorer tree endRun to finish (multiple microtasks)
4750
// - if we change here the state before the tasks states are updated, the cursor position will be lost

packages/ui/client/composables/explorer/collector.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export function runCollect(
9090
summary: CollectorInfo,
9191
search: string,
9292
filter: Filter,
93+
executionTime: number,
9394
) {
9495
if (start) {
9596
resetCollectorInfo(summary)
@@ -106,7 +107,7 @@ export function runCollect(
106107
})
107108

108109
queueMicrotask(() => {
109-
collectData(summary)
110+
collectData(summary, executionTime)
110111
})
111112

112113
queueMicrotask(() => {
@@ -290,13 +291,16 @@ export function resetCollectorInfo(summary: CollectorInfo) {
290291
summary.failedSnapshotEnabled = false
291292
}
292293

293-
function collectData(summary: CollectorInfo) {
294+
function collectData(
295+
summary: CollectorInfo,
296+
time: number,
297+
) {
294298
const idMap = client.state.idMap
295299
const filesMap = new Map(explorerTree.root.tasks.filter(f => idMap.has(f.id)).map(f => [f.id, f]))
296300
const useFiles = Array.from(filesMap.values()).map(file => [file.id, findById(file.id)] as const)
297301
const data = {
298302
files: filesMap.size,
299-
time: '',
303+
time: time > 1000 ? `${(time / 1000).toFixed(2)}s` : `${Math.round(time)}ms`,
300304
filesFailed: 0,
301305
filesSuccess: 0,
302306
filesIgnore: 0,
@@ -314,27 +318,10 @@ function collectData(summary: CollectorInfo) {
314318
failedSnapshotEnabled: false,
315319
} satisfies CollectorInfo
316320

317-
let time = 0
318-
for (const [id, f] of useFiles) {
321+
for (const [_, f] of useFiles) {
319322
if (!f) {
320323
continue
321324
}
322-
const file = filesMap.get(id)
323-
if (file) {
324-
file.mode = f.mode
325-
file.setupDuration = f.setupDuration
326-
file.prepareDuration = f.prepareDuration
327-
file.environmentLoad = f.environmentLoad
328-
file.collectDuration = f.collectDuration
329-
file.duration = f.result?.duration != null ? Math.round(f.result?.duration) : undefined
330-
file.state = f.result?.state
331-
}
332-
time += Math.max(0, f.collectDuration || 0)
333-
time += Math.max(0, f.setupDuration || 0)
334-
time += Math.max(0, f.result?.duration || 0)
335-
time += Math.max(0, f.environmentLoad || 0)
336-
time += Math.max(0, f.prepareDuration || 0)
337-
data.time = time > 1000 ? `${(time / 1000).toFixed(2)}s` : `${Math.round(time)}ms`
338325
if (f.result?.state === 'fail') {
339326
data.filesFailed++
340327
}

packages/ui/client/composables/explorer/tree.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
export class ExplorerTree {
2121
private rafCollector: ReturnType<typeof useRafFn>
2222
private resumeEndRunId: ReturnType<typeof setTimeout> | undefined
23+
public startTime: number = 0
24+
public executionTime: number = 0
2325
constructor(
2426
public projects: string[] = [],
2527
public colors = new Map<string, string | undefined>(),
@@ -76,6 +78,7 @@ export class ExplorerTree {
7678
}
7779

7880
startRun() {
81+
this.startTime = performance.now()
7982
this.resumeEndRunId = setTimeout(() => this.endRun(), this.resumeEndTimeout)
8083
this.collect(true, false)
8184
}
@@ -100,7 +103,8 @@ export class ExplorerTree {
100103
}
101104
}
102105

103-
endRun() {
106+
endRun(executionTime = performance.now() - this.startTime) {
107+
this.executionTime = executionTime
104108
this.rafCollector.pause()
105109
this.onTaskUpdateCalled = false
106110
this.collect(false, true)
@@ -124,6 +128,7 @@ export class ExplorerTree {
124128
skipped: filter.skipped,
125129
onlyTests: filter.onlyTests,
126130
},
131+
end ? this.executionTime : performance.now() - this.startTime,
127132
)
128133
})
129134
}
@@ -139,6 +144,7 @@ export class ExplorerTree {
139144
skipped: filter.skipped,
140145
onlyTests: filter.onlyTests,
141146
},
147+
end ? this.executionTime : performance.now() - this.startTime,
142148
)
143149
}
144150
}

packages/vitest/src/api/setup.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
WebSocketRPC,
1616
} from './types'
1717
import { existsSync, promises as fs } from 'node:fs'
18+
import { performance } from 'node:perf_hooks'
1819
import { noop } from '@vitest/utils/helpers'
1920
import { createBirpc } from 'birpc'
2021
import { parse, stringify } from 'flatted'
@@ -157,6 +158,8 @@ export function setup(ctx: Vitest, _server?: ViteDevServer): void {
157158
}
158159

159160
export class WebSocketReporter implements Reporter {
161+
private start = 0
162+
private end = 0
160163
constructor(
161164
public ctx: Vitest,
162165
public wss: WebSocketServer,
@@ -178,8 +181,8 @@ export class WebSocketReporter implements Reporter {
178181
return
179182
}
180183

184+
this.start = performance.now()
181185
const serializedSpecs = specifications.map(spec => spec.toJSON())
182-
183186
this.clients.forEach((client) => {
184187
client.onSpecsCollected?.(serializedSpecs)?.catch?.(noop)
185188
})
@@ -205,6 +208,12 @@ export class WebSocketReporter implements Reporter {
205208
})
206209
}
207210

211+
private sum<T>(items: T[], cb: (_next: T) => number | undefined) {
212+
return items.reduce((total, next) => {
213+
return total + Math.max(cb(next) || 0, 0)
214+
}, 0)
215+
}
216+
208217
onTestRunEnd(testModules: ReadonlyArray<TestModule>, unhandledErrors: ReadonlyArray<SerializedError>): void {
209218
if (!this.clients.size) {
210219
return
@@ -213,8 +222,13 @@ export class WebSocketReporter implements Reporter {
213222
const files = testModules.map(testModule => testModule.task)
214223
const errors = [...unhandledErrors]
215224

225+
this.end = performance.now()
226+
const blobs = this.ctx.state.blobs
227+
// Execution time is either sum of all runs of `--merge-reports` or the current run's time
228+
const executionTime = blobs?.executionTimes ? this.sum(blobs.executionTimes, time => time) : this.end - this.start
229+
216230
this.clients.forEach((client) => {
217-
client.onFinished?.(files, errors)?.catch?.(noop)
231+
client.onFinished?.(files, errors, undefined, executionTime)?.catch?.(noop)
218232
})
219233
}
220234

packages/vitest/src/api/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ export interface WebSocketEvents {
5858
files: File[],
5959
errors: unknown[],
6060
coverage?: unknown,
61+
executionTime?: number,
6162
) => Awaitable<void>
6263
onTestAnnotate?: (testId: string, annotation: TestAnnotation) => Awaitable<void>
6364
onTaskUpdate?: (packs: TaskResultPack[], events: TaskEventPack[]) => Awaitable<void>
6465
onUserConsoleLog?: (log: UserConsoleLog) => Awaitable<void>
6566
onPathsCollected?: (paths?: string[]) => Awaitable<void>
66-
onSpecsCollected?: (specs?: SerializedTestSpecification[]) => Awaitable<void>
67+
onSpecsCollected?: (specs?: SerializedTestSpecification[], startTime?: number) => Awaitable<void>
6768
onFinishedReportCoverage: () => void
6869
}
6970

packages/ws-client/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ export function createClient(url: string, options: VitestClientOptions = {}): Vi
5454
onTestAnnotate(testId, annotation) {
5555
handlers.onTestAnnotate?.(testId, annotation)
5656
},
57-
onSpecsCollected(specs) {
57+
onSpecsCollected(specs, startTime) {
5858
specs?.forEach(([config, file]) => {
5959
ctx.state.clearFiles({ config }, [file])
6060
})
61-
handlers.onSpecsCollected?.(specs)
61+
handlers.onSpecsCollected?.(specs, startTime)
6262
},
6363
onPathsCollected(paths) {
6464
ctx.state.collectPaths(paths)
@@ -76,8 +76,8 @@ export function createClient(url: string, options: VitestClientOptions = {}): Vi
7676
ctx.state.updateUserLog(log)
7777
handlers.onUserConsoleLog?.(log)
7878
},
79-
onFinished(files, errors) {
80-
handlers.onFinished?.(files, errors)
79+
onFinished(files, errors, coverage, executionTime) {
80+
handlers.onFinished?.(files, errors, coverage, executionTime)
8181
},
8282
onFinishedReportCoverage() {
8383
handlers.onFinishedReportCoverage?.()

0 commit comments

Comments
 (0)