From 3e19cb1fdd9ed01fa229daa6ebb724fb8b30c79d Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Wed, 9 Feb 2022 08:58:40 +0100 Subject: [PATCH] [FEATURE] Enable creating CPU profiles via UI5_CLI_PROFILE=true --- bin/ui5.cjs | 7 ++++++- lib/cli/base.js | 2 -- lib/cli/cli.js | 19 ++++++++++++++++--- lib/utils/profile.js | 45 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 lib/utils/profile.js diff --git a/bin/ui5.cjs b/bin/ui5.cjs index 1c038497..a789fe88 100755 --- a/bin/ui5.cjs +++ b/bin/ui5.cjs @@ -77,6 +77,10 @@ const ui5 = { }, async invokeCLI(pkg) { + if (process.env.UI5_CLI_PROFILE) { + const profile = await import("../lib/utils/profile.js"); + await profile.start(); + } const {default: cli} = await import("../lib/cli/cli.js"); await cli(pkg); }, @@ -88,7 +92,8 @@ const ui5 = { } else { const localInstallationInvoked = await ui5.invokeLocalInstallation(pkg); if (!localInstallationInvoked) { - await ui5.invokeCLI(pkg); + const exitCode = await ui5.invokeCLI(pkg); + process.exit(exitCode); } } } diff --git a/lib/cli/base.js b/lib/cli/base.js index 7bc4893e..ae7ecbfa 100644 --- a/lib/cli/base.js +++ b/lib/cli/base.js @@ -123,7 +123,5 @@ export default function(cli) { console.log(""); console.log(chalk.dim(`See 'ui5 --help'`)); } - process.exit(1); }); } - diff --git a/lib/cli/cli.js b/lib/cli/cli.js index e46a5193..823a22f2 100644 --- a/lib/cli/cli.js +++ b/lib/cli/cli.js @@ -68,7 +68,20 @@ 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; + let exitCode = 0; + try { + // Call parse to run yargs + await cli.parse(); + } catch (err) { + // Error is already handled via .fail callback in ./base.js + exitCode = 1; + } finally { + // Stop profiling after CLI finished execution + if (process.env.UI5_CLI_PROFILE) { + const profile = await import("../utils/profile.js"); + await profile.stop(); + } + } + + return exitCode; }; diff --git a/lib/utils/profile.js b/lib/utils/profile.js new file mode 100644 index 00000000..a4e3143c --- /dev/null +++ b/lib/utils/profile.js @@ -0,0 +1,45 @@ +import {writeFile} from "node:fs/promises"; +import {Session} from "node:inspector"; + +let session; + +export async function start() { + if (session) { + return; + } + session = new Session(); + session.connect(); + await new Promise((resolve) => { + session.post("Profiler.enable", () => { + session.post("Profiler.start", () => { + resolve(); + }); + }); + }); +} + +async function writeProfile(profile) { + const d = new Date(); + const timestamp = + `${d.getFullYear()}-${d.getMonth()+1}-${d.getDate()}_${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}`; + + await writeFile(`./ui5_${timestamp}.cpuprofile`, JSON.stringify(profile)); +} + +export async function stop() { + if (!session) { + return; + } + const profile = await new Promise((resolve) => { + session.post("Profiler.stop", (err, {profile}) => { + if (err) { + resolve(null); + } else { + resolve(profile); + } + }); + }); + if (profile) { + await writeProfile(profile); + } +}