forked from microsoft/react-native-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfJsonReporter.ts
More file actions
143 lines (130 loc) · 3.58 KB
/
PerfJsonReporter.ts
File metadata and controls
143 lines (130 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* @format
*/
import fs from '@react-native-windows/fs';
import * as path from 'path';
import {SnapshotManager} from '../matchers/snapshotManager';
import type {SnapshotFile} from '../matchers/snapshotManager';
/**
* Summary of a single test suite's perf results.
*/
export interface SuiteResult {
testFilePath: string;
suiteName: string;
snapshots: SnapshotFile;
passed: number;
failed: number;
totalDuration: number;
}
/**
* Full CI perf run results.
*/
export interface CIRunResults {
timestamp: string;
branch: string;
commitSha: string;
suites: SuiteResult[];
summary: {
totalSuites: number;
totalTests: number;
passed: number;
failed: number;
durationMs: number;
};
}
/**
* Custom Jest reporter that collects perf snapshot data
* and writes a JSON results file for CI.
*/
export class PerfJsonReporter {
private readonly outputFile: string;
constructor(
_globalConfig: Record<string, unknown>,
options: {outputFile?: string} = {},
) {
this.outputFile = options.outputFile || '.perf-results/results.json';
}
onRunComplete(
_testContexts: Set<unknown>,
results: {
numTotalTestSuites: number;
numPassedTestSuites: number;
numFailedTestSuites: number;
numTotalTests: number;
numPassedTests: number;
numFailedTests: number;
startTime: number;
testResults: Array<{
testFilePath: string;
testResults: Array<{
ancestorTitles: string[];
title: string;
status: string;
duration?: number;
}>;
perfTimer?: {start: number; end: number};
}>;
},
): void {
const suites: SuiteResult[] = [];
for (const suite of results.testResults) {
// Use live run metrics captured during the test run
const {file: snapshotFilePath} = SnapshotManager.getSnapshotPath(
suite.testFilePath,
);
const snapshots = SnapshotManager.getRunMetrics(snapshotFilePath) ?? {};
const passed = suite.testResults.filter(
t => t.status === 'passed',
).length;
const failed = suite.testResults.filter(
t => t.status === 'failed',
).length;
const totalDuration = suite.testResults.reduce(
(acc, t) => acc + (t.duration || 0),
0,
);
suites.push({
testFilePath: suite.testFilePath,
suiteName: path.basename(suite.testFilePath, '.perf-test.tsx'),
snapshots,
passed,
failed,
totalDuration,
});
}
const ciResults: CIRunResults = {
timestamp: new Date().toISOString(),
branch:
process.env.BUILD_SOURCEBRANCH ||
process.env.GITHUB_HEAD_REF ||
process.env.GITHUB_REF ||
'unknown',
commitSha:
process.env.BUILD_SOURCEVERSION || process.env.GITHUB_SHA || 'unknown',
suites,
summary: {
totalSuites: results.numTotalTestSuites,
totalTests: results.numTotalTests,
passed: results.numPassedTests,
failed: results.numFailedTests,
durationMs: Date.now() - results.startTime,
},
};
// Write results
const outputDir = path.dirname(this.outputFile);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, {recursive: true});
}
fs.writeFileSync(
this.outputFile,
JSON.stringify(ciResults, null, 2) + '\n',
'utf-8',
);
console.log(`\n📊 Perf results written to: ${this.outputFile}`);
}
}
// Default export for Jest reporter compatibility
module.exports = PerfJsonReporter;