Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak fix: release source map info after processed and minor optimizations #8234

Merged
merged 6 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- `[jest-core]` Improve performance of SearchSource.findMatchingTests by 15% ([#8184](https://github.com/facebook/jest/pull/8184))
- `[jest-resolve]` Optimize internal cache lookup performance ([#8183](https://github.com/facebook/jest/pull/8183))
- `[jest-core]` Dramatically improve watch mode performance ([#8201](https://github.com/facebook/jest/pull/8201))
- `[jest-reporters]` Fix memory leak of source map info and minor performance improvements ([#8234](https://github.com/facebook/jest/pull/8234))

## 24.5.0

Expand Down
16 changes: 11 additions & 5 deletions packages/jest-reporters/src/coverage_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,35 @@ export default class CoverageReporter extends BaseReporter {
testResult: TestResult,
_aggregatedResults: AggregatedResult,
) {
// Coverage and source maps are only appended to the test result for this
// handler. Delete when processed to preserve memory.

if (testResult.coverage) {
this._coverageMap.merge(testResult.coverage);
// Remove coverage data to free up some memory.
delete testResult.coverage;
testResult.coverage = undefined;
}

Object.keys(testResult.sourceMaps).forEach(sourcePath => {
const sourceMaps = testResult.sourceMaps;
if (sourceMaps) {
Object.keys(sourceMaps).forEach(sourcePath => {
let inputSourceMap: RawSourceMap | undefined;
try {
const coverage: FileCoverage = this._coverageMap.fileCoverageFor(
sourcePath,
);
({inputSourceMap} = coverage.toJSON() as any);
inputSourceMap = (coverage.toJSON() as any).inputSourceMap;
} finally {
if (inputSourceMap) {
this._sourceMapStore.registerMap(sourcePath, inputSourceMap);
} else {
this._sourceMapStore.registerURL(
sourcePath,
testResult.sourceMaps[sourcePath],
sourceMaps[sourcePath],
);
}
}
});
testResult.sourceMaps = undefined;
}
}

Expand Down
13 changes: 9 additions & 4 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,19 @@ async function runTestInternal(

result.perfStats = {end: Date.now(), start};
result.testFilePath = path;
result.coverage = runtime.getAllCoverageInfoCopy();
result.sourceMaps = runtime.getSourceMapInfo(
new Set(Object.keys(result.coverage || {})),
);
result.console = testConsole.getBuffer();
result.skipped = testCount === result.numPendingTests;
result.displayName = config.displayName;

const coverage = runtime.getAllCoverageInfoCopy();
if (coverage) {
const coverageKeys = Object.keys(coverage);
if (coverageKeys.length) {
result.coverage = coverage;
result.sourceMaps = runtime.getSourceMapInfo(new Set(coverageKeys));
}
}

if (globalConfig.logHeapUsage) {
if (global.gc) {
global.gc();
Expand Down
64 changes: 31 additions & 33 deletions packages/jest-test-result/src/formatTestResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,41 @@ import {
TestResult,
} from './types';

const formatResult = (
const formatTestResult = (
testResult: TestResult,
codeCoverageFormatter: CodeCoverageFormatter,
reporter: CodeCoverageReporter,
codeCoverageFormatter?: CodeCoverageFormatter,
reporter?: CodeCoverageReporter,
): FormattedTestResult => {
const now = Date.now();
const output: FormattedTestResult = {
assertionResults: [],
coverage: {},
endTime: now,
message: '',
name: testResult.testFilePath,
startTime: now,
status: 'failed',
summary: '',
};

const assertionResults = testResult.testResults.map(formatTestAssertion);
if (testResult.testExecError) {
output.message = testResult.testExecError.message;
output.coverage = {};
const now = Date.now();
return {
assertionResults,
coverage: {},
endTime: now,
message: testResult.failureMessage
? testResult.failureMessage
: testResult.testExecError.message,
name: testResult.testFilePath,
startTime: now,
status: 'failed',
summary: '',
};
} else {
const allTestsPassed = testResult.numFailingTests === 0;
output.status = allTestsPassed ? 'passed' : 'failed';
output.startTime = testResult.perfStats.start;
output.endTime = testResult.perfStats.end;
output.coverage = codeCoverageFormatter(testResult.coverage, reporter);
}

output.assertionResults = testResult.testResults.map(formatTestAssertion);

if (testResult.failureMessage) {
output.message = testResult.failureMessage;
return {
assertionResults,
coverage: codeCoverageFormatter
? codeCoverageFormatter(testResult.coverage, reporter)
: testResult.coverage,
endTime: testResult.perfStats.end,
message: testResult.failureMessage || '',
name: testResult.testFilePath,
startTime: testResult.perfStats.start,
status: allTestsPassed ? 'passed' : 'failed',
summary: '',
};
}

return output;
};

function formatTestAssertion(
Expand All @@ -72,13 +72,11 @@ function formatTestAssertion(

export default function formatTestResults(
results: AggregatedResult,
codeCoverageFormatter?: CodeCoverageFormatter | null,
codeCoverageFormatter?: CodeCoverageFormatter,
reporter?: CodeCoverageReporter,
): FormattedTestResults {
const formatter = codeCoverageFormatter || (coverage => coverage);

const testResults = results.testResults.map(testResult =>
formatResult(testResult, formatter, reporter),
formatTestResult(testResult, codeCoverageFormatter, reporter),
);

return {...results, testResults};
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-test-result/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export type TestResult = {
unmatched: number;
updated: number;
};
sourceMaps: {
sourceMaps?: {
[sourcePath: string]: string;
};
testExecError?: SerializableError;
Expand Down