Skip to content

[FEATURE][REWORKED] Enable creating CPU profiles via UI5_CLI_PROFILE=true #638

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion bin/ui5.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,19 @@ const ui5 = {
},

async invokeCLI(pkg) {
let profile;
if (process.env.UI5_CLI_PROFILE) {
profile = await import("../lib/utils/profile.js");
await profile.start();
}
const {default: cli} = await import("../lib/cli/cli.js");
await cli(pkg);
const {command} = await cli(pkg);

// Stop profiling after CLI finished execution
// Except for "serve" command, which continues running and only stops on sigint (see profile.js)
if (profile && command !== "serve") {
await profile.stop();
}
},

async main() {
Expand Down
1 change: 0 additions & 1 deletion lib/cli/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,3 @@ export default function(cli) {
process.exit(1);
});
}

7 changes: 4 additions & 3 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export default async (pkg) => {
// Format terminal output to full available width
cli.wrap(cli.terminalWidth());

// yargs registers a get method on the argv property.
// The property needs to be accessed to initialize everything.
cli.argv;
const {_} = await cli.argv;
return {
command: _[0]
};
};
82 changes: 82 additions & 0 deletions lib/utils/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {writeFileSync} from "node:fs";
import {Session} from "node:inspector/promises";

let session;
let processSignals;

export async function start() {
if (session) {
return;
}
session = new Session();
session.connect();
await session.post("Profiler.enable");
await session.post("Profiler.start");
console.log(`Recording CPU profile...`);

Check failure on line 15 in lib/utils/profile.js

View workflow job for this annotation

GitHub Actions / General checks, tests and coverage reporting

Unexpected console statement
processSignals = registerSigHooks();
}

async function writeProfile(profile) {
const formatter = new Intl.DateTimeFormat("en-GB", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
const dateParts = Object.create(null);
const parts = formatter.formatToParts(new Date());
parts.forEach((p) => {
dateParts[p.type] = p.value;
});

const fileName = `./ui5_${dateParts.year}-${dateParts.month}-${dateParts.day}_` +
`${dateParts.hour}-${dateParts.minute}-${dateParts.second}.cpuprofile`;
console.log(`\nSaving CPU profile to ${fileName}...`);

Check failure on line 36 in lib/utils/profile.js

View workflow job for this annotation

GitHub Actions / General checks, tests and coverage reporting

Unexpected console statement
writeFileSync(fileName, JSON.stringify(profile));
}

export async function stop() {
if (!session) {
return;
}
const {profile} = await session.post("Profiler.stop");
session = null;
if (profile) {
await writeProfile(profile);
}
if (processSignals) {
deregisterSigHooks(processSignals);
processSignals = null;
}
}

function registerSigHooks() {
function createListener(exitCode) {
return function() {
// Gracefully end profiling, then exit
stop().then(() => {
process.exit(exitCode);
});
};
}

const processSignals = {
"SIGHUP": createListener(128 + 1),
"SIGINT": createListener(128 + 2),
"SIGTERM": createListener(128 + 15),
"SIGBREAK": createListener(128 + 21)
};

for (const signal of Object.keys(processSignals)) {
process.on(signal, processSignals[signal]);
}
return processSignals;
}

function deregisterSigHooks(signals) {
for (const signal of Object.keys(signals)) {
process.removeListener(signal, signals[signal]);
}
}
Loading