forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.ts
More file actions
191 lines (162 loc) · 6.2 KB
/
start.ts
File metadata and controls
191 lines (162 loc) · 6.2 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { once } from 'node:events';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import type { Worker } from 'node:worker_threads';
import {
type CommandLineStringListParameter,
type IRequiredCommandLineStringParameter,
CommandLineParser
} from '@rushstack/ts-command-line';
import { WorkerPool } from '@rushstack/worker-pool';
import type { IMessageFromWorker } from './protocol';
import type { INodeSummary, IProfileSummary } from './types';
/**
* Merges summarized information from multiple profiles into a single collection.
* @param accumulator - The collection to merge the nodes into
* @param values - The nodes to merge
*/
function mergeProfileSummaries(
accumulator: Map<string, INodeSummary>,
values: Iterable<[string, INodeSummary]>
): void {
for (const [nodeId, node] of values) {
const existing: INodeSummary | undefined = accumulator.get(nodeId);
if (!existing) {
accumulator.set(nodeId, node);
} else {
existing.selfTime += node.selfTime;
existing.totalTime += node.totalTime;
}
}
}
/**
* Scans a directory and its subdirectories for CPU profiles.
* @param baseDir - The directory to recursively search for CPU profiles
* @returns All .cpuprofile files found in the directory and its subdirectories
*/
function findProfiles(baseDir: string): string[] {
baseDir = path.resolve(baseDir);
const files: string[] = [];
const directories: string[] = [baseDir];
for (const dir of directories) {
const entries: fs.Dirent[] = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isFile() && entry.name.endsWith('.cpuprofile')) {
files.push(`${dir}/${entry.name}`);
} else if (entry.isDirectory()) {
directories.push(`${dir}/${entry.name}`);
}
}
}
return files;
}
/**
* Processes a set of CPU profiles and aggregates the results.
* Uses a worker pool.
* @param profiles - The set of .cpuprofile files to process
* @returns A summary of the profiles
*/
async function processProfilesAsync(profiles: Set<string>): Promise<IProfileSummary> {
const maxWorkers: number = Math.min(profiles.size, os.availableParallelism());
console.log(`Processing ${profiles.size} profiles using ${maxWorkers} workers...`);
const workerPool: WorkerPool = new WorkerPool({
id: 'cpu-profile-summarizer',
maxWorkers,
workerScriptPath: path.resolve(__dirname, 'worker.js')
});
const summary: IProfileSummary = new Map();
let processed: number = 0;
await Promise.all(
Array.from(profiles, async (profile: string) => {
const worker: Worker = await workerPool.checkoutWorkerAsync(true);
const responsePromise: Promise<IMessageFromWorker[]> = once(worker, 'message');
worker.postMessage(profile);
const { 0: messageFromWorker } = await responsePromise;
if (messageFromWorker.type === 'error') {
console.error(`Error processing ${profile}: ${messageFromWorker.data}`);
} else {
++processed;
console.log(`Processed ${profile} (${processed}/${profiles.size})`);
mergeProfileSummaries(summary, messageFromWorker.data);
}
workerPool.checkinWorker(worker);
})
);
await workerPool.finishAsync();
return summary;
}
function writeSummaryToTsv(tsvPath: string, summary: IProfileSummary): void {
const dir: string = path.dirname(tsvPath);
fs.mkdirSync(dir, { recursive: true });
let tsv: string = `Self Time (seconds)\tTotal Time (seconds)\tFunction Name\tURL\tLine\tColumn`;
for (const { selfTime, totalTime, functionName, url, lineNumber, columnNumber } of summary.values()) {
const selfSeconds: string = (selfTime / 1e6).toFixed(3);
const totalSeconds: string = (totalTime / 1e6).toFixed(3);
tsv += `\n${selfSeconds}\t${totalSeconds}\t${functionName}\t${url}\t${lineNumber}\t${columnNumber}`;
}
fs.writeFileSync(tsvPath, tsv, 'utf8');
console.log(`Wrote summary to ${tsvPath}`);
}
class CpuProfileSummarizerCommandLineParser extends CommandLineParser {
private readonly _inputParameter: CommandLineStringListParameter;
private readonly _outputParameter: IRequiredCommandLineStringParameter;
public constructor() {
super({
toolFilename: 'cpu-profile-summarizer',
toolDescription:
'This tool summarizes the contents of multiple V8 .cpuprofile reports. ' +
'For example, those generated by running `node --cpu-prof`.'
});
this._inputParameter = this.defineStringListParameter({
parameterLongName: '--input',
parameterShortName: '-i',
description: 'The directory containing .cpuprofile files to summarize',
argumentName: 'DIR',
required: true
});
this._outputParameter = this.defineStringParameter({
parameterLongName: '--output',
parameterShortName: '-o',
description: 'The output file to write the summary to',
argumentName: 'TSV_FILE',
required: true
});
}
protected override async onExecuteAsync(): Promise<void> {
const input: readonly string[] = this._inputParameter.values;
const output: string = this._outputParameter.value;
if (input.length === 0) {
throw new Error('No input directories provided');
}
const allProfiles: Set<string> = new Set();
for (const dir of input) {
const resolvedDir: string = path.resolve(dir);
console.log(`Collating CPU profiles from ${resolvedDir}...`);
const profiles: string[] = findProfiles(resolvedDir);
console.log(`Found ${profiles.length} profiles`);
for (const profile of profiles) {
allProfiles.add(profile);
}
}
if (allProfiles.size === 0) {
throw new Error(`No profiles found`);
}
const summary: IProfileSummary = await processProfilesAsync(allProfiles);
writeSummaryToTsv(output, summary);
}
}
process.exitCode = 1;
const parser: CpuProfileSummarizerCommandLineParser = new CpuProfileSummarizerCommandLineParser();
parser
.executeAsync()
.then((success: boolean) => {
if (success) {
process.exitCode = 0;
}
})
.catch((error: Error) => {
console.error(error);
});