From 0def924f3faca1498004a67923b7073d16a8c98a Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Fri, 3 Feb 2023 17:03:28 +0000 Subject: [PATCH 1/4] Add start/end performance markers around significant lifecycle events --- packages/jest-core/src/cli/index.ts | 11 +++++++++++ packages/jest-core/src/runJest.ts | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index b3e7df8c5e12..b9ac488c3278 100644 --- a/packages/jest-core/src/cli/index.ts +++ b/packages/jest-core/src/cli/index.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import {performance} from 'perf_hooks'; import chalk = require('chalk'); import exit = require('exit'); import * as fs from 'graceful-fs'; @@ -41,6 +42,7 @@ export async function runCLI( results: AggregatedResult; globalConfig: Config.GlobalConfig; }> { + performance.mark('jest/runCLI:start'); let results: AggregatedResult | undefined; // If we output a JSON object, we can't write anything to stdout, since @@ -133,6 +135,7 @@ export async function runCLI( console.error(message); } + performance.mark('jest/runCLI:end'); return {globalConfig, results}; } @@ -173,6 +176,12 @@ const _run10000 = async ( // Queries to hg/git can take a while, so we need to start the process // as soon as possible, so by the time we need the result it's already there. const changedFilesPromise = getChangedFilesPromise(globalConfig, configs); + if (changedFilesPromise) { + performance.mark('jest/getChangedFiles:start'); + changedFilesPromise.finally(() => { + performance.mark('jest/getChangedFiles:end'); + }); + } // Filter may need to do an HTTP call or something similar to setup. // We will wait on an async response from this before using the filter. @@ -204,11 +213,13 @@ const _run10000 = async ( }; } + performance.mark('jest/buildContextsAndHasteMaps:start'); const {contexts, hasteMapInstances} = await buildContextsAndHasteMaps( configs, globalConfig, outputStream, ); + performance.mark('jest/buildContextsAndHasteMaps:end'); globalConfig.watch || globalConfig.watchAll ? await runWatch( diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index 9aa77a4346ec..640407a0f6b1 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -6,6 +6,7 @@ */ import * as path from 'path'; +import {performance} from 'perf_hooks'; import chalk = require('chalk'); import exit = require('exit'); import * as fs from 'graceful-fs'; @@ -179,6 +180,7 @@ export default async function runJest({ const searchSources = contexts.map(context => new SearchSource(context)); + performance.mark('jest/getTestPaths:start'); const testRunData: TestRunData = await Promise.all( contexts.map(async (context, index) => { const searchSource = searchSources[index]; @@ -195,6 +197,7 @@ export default async function runJest({ return {context, matches}; }), ); + performance.mark('jest/getTestPaths:end'); if (globalConfig.shard) { if (typeof sequencer.shard !== 'function') { @@ -260,7 +263,9 @@ export default async function runJest({ } if (hasTests) { + performance.mark('jest/globalSetup:start'); await runGlobalHook({allTests, globalConfig, moduleName: 'globalSetup'}); + performance.mark('jest/globalSetup:end'); } if (changedFilesPromise) { @@ -289,14 +294,24 @@ export default async function runJest({ ...testSchedulerContext, }); + // @ts-expect-error - second arg is unsupported (but harmless) in Node 14 + performance.mark('jest/scheduleAndRun:start', { + detail: {numTests: allTests.length}, + }); const results = await scheduler.scheduleTests(allTests, testWatcher); + performance.mark('jest/scheduleAndRun:start'); + performance.mark('jest/cacheResults:start'); sequencer.cacheResults(allTests, results); + performance.mark('jest/cacheResults:end'); if (hasTests) { + performance.mark('jest/globalTeardown:start'); await runGlobalHook({allTests, globalConfig, moduleName: 'globalTeardown'}); + performance.mark('jest/globalTeardown:end'); } + performance.mark('jest/processResults:start'); await processResults(results, { collectHandles, json: globalConfig.json, @@ -305,4 +320,5 @@ export default async function runJest({ outputStream, testResultsProcessor: globalConfig.testResultsProcessor, }); + performance.mark('jest/processResults:end'); } From 3514392fb7821239da986c7c0e6e3f79ba3c34d3 Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Fri, 3 Feb 2023 17:15:31 +0000 Subject: [PATCH 2/4] Changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbfa3ec54860..0a8473f9e303 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ ## main ### Features +- `[@jest/core]` Instrument significant lifecycle events with [`performance.mark()`](https://nodejs.org/docs/latest-v16.x/api/perf_hooks.html#performancemarkname-options) ([#13859]((https://github.com/facebook/jest/pull/13859))) -### Fixes +- ### Fixes - `[expect, @jest/expect]` Provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions ([#13848](https://github.com/facebook/jest/pull/13848)) - `[jest-circus]` Added explicit mention of test failing because `done()` is not being called in error message ([#13847](https://github.com/facebook/jest/pull/13847)) From 3b731dfa6b21f40e0bf54ce30e4bfc4f392587cf Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Fri, 3 Feb 2023 17:26:10 +0000 Subject: [PATCH 3/4] Typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a8473f9e303..25ac1b5c3e73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ### Features - `[@jest/core]` Instrument significant lifecycle events with [`performance.mark()`](https://nodejs.org/docs/latest-v16.x/api/perf_hooks.html#performancemarkname-options) ([#13859]((https://github.com/facebook/jest/pull/13859))) -- ### Fixes +### Fixes - `[expect, @jest/expect]` Provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions ([#13848](https://github.com/facebook/jest/pull/13848)) - `[jest-circus]` Added explicit mention of test failing because `done()` is not being called in error message ([#13847](https://github.com/facebook/jest/pull/13847)) From b5cb25e6b2c944637af107db14728ca60fca85ed Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Fri, 3 Feb 2023 17:32:47 +0000 Subject: [PATCH 4/4] Prettier --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25ac1b5c3e73..8ac006ab844a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ ## main ### Features -- `[@jest/core]` Instrument significant lifecycle events with [`performance.mark()`](https://nodejs.org/docs/latest-v16.x/api/perf_hooks.html#performancemarkname-options) ([#13859]((https://github.com/facebook/jest/pull/13859))) + +- `[@jest/core]` Instrument significant lifecycle events with [`performance.mark()`](https://nodejs.org/docs/latest-v16.x/api/perf_hooks.html#performancemarkname-options) ([#13859](https://github.com/facebook/jest/pull/13859)) ### Fixes