Skip to content

Commit

Permalink
feat: show transform time
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 16, 2022
1 parent c663f39 commit b54a13e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/vite-node/src/server.ts
Expand Up @@ -18,6 +18,7 @@ export class ViteNodeServer {
private transformPromiseMap = new Map<string, Promise<TransformResult | null | undefined>>()

fetchCache = new Map<string, {
duration?: number
timestamp: number
result: FetchResult
}>()
Expand Down Expand Up @@ -125,16 +126,20 @@ export class ViteNodeServer {
return cache.result

const externalize = await this.shouldExternalize(filePath)
let duration: number | undefined
if (externalize) {
result = { externalize }
this.debugger?.recordExternalize(id, externalize)
}
else {
const start = performance.now()
const r = await this._transformRequest(id)
duration = performance.now() - start
result = { code: r?.code, map: r?.map as unknown as RawSourceMap }
}

this.fetchCache.set(filePath, {
duration,
timestamp,
result,
})
Expand Down
11 changes: 10 additions & 1 deletion packages/vitest/src/node/reporters/base.ts
Expand Up @@ -218,6 +218,7 @@ export abstract class BaseReporter implements Reporter {
const collectTime = files.reduce((acc, test) => acc + Math.max(0, test.collectDuration || 0), 0)
const setupTime = files.reduce((acc, test) => acc + Math.max(0, test.setupDuration || 0), 0)
const testsTime = files.reduce((acc, test) => acc + Math.max(0, test.result?.duration || 0), 0)
const transformTime = Array.from(this.ctx.vitenode.fetchCache.values()).reduce((a, b) => a + (b?.duration || 0), 0)
const threadTime = collectTime + testsTime + setupTime

const padTitle = (str: string) => c.dim(`${str.padStart(10)} `)
Expand All @@ -227,6 +228,14 @@ export abstract class BaseReporter implements Reporter {
return `${Math.round(time)}ms`
}

// show top 10 costly transform module
// console.log(Array.from(this.ctx.vitenode.fetchCache.entries()).filter(i => i[1].duration)
// .sort((a, b) => b[1].duration! - a[1].duration!)
// .map(i => `${time(i[1].duration!)} ${i[0]}`)
// .slice(0, 10)
// .join('\n'),
// )

const snapshotOutput = renderSnapshotSummary(this.ctx.config.root, this.ctx.snapshot.summary)
if (snapshotOutput.length) {
logger.log(snapshotOutput.map((t, i) => i === 0
Expand All @@ -243,7 +252,7 @@ export abstract class BaseReporter implements Reporter {
if (this.watchFilters)
logger.log(padTitle('Duration'), time(threadTime))
else
logger.log(padTitle('Duration'), time(executionTime) + c.gray(` (setup ${time(setupTime)}, collect ${time(collectTime)}, tests ${time(testsTime)})`))
logger.log(padTitle('Duration'), time(executionTime) + c.dim(` (transform ${time(transformTime)}, setup ${time(setupTime)}, collect ${time(collectTime)}, tests ${time(testsTime)})`))

logger.log()
}
Expand Down

0 comments on commit b54a13e

Please sign in to comment.