Skip to content

Commit de4ee1b

Browse files
committed
[FEATURE] Enable creating a CPU profile
1 parent 19aa17b commit de4ee1b

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

bin/ui5.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if (
3838
console.log("=====================================================================");
3939
}
4040
// Timeout is required to log info when importing from local installation
41-
setTimeout(() => {
41+
setTimeout(async () => {
4242
if (!process.env.UI5_CLI_NO_LOCAL) {
4343
const importLocal = require("import-local");
4444
// Prefer a local installation of @ui5/cli.
@@ -57,6 +57,10 @@ if (
5757
}
5858
}
5959

60+
if (process.env.UI5_CLI_PROFILE) {
61+
await (require("../lib/utils/profile").start());
62+
}
63+
6064
const updateNotifier = require("update-notifier");
6165
updateNotifier({
6266
pkg,

lib/cli/commands/base.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@ cli.usage("Usage: ui5 <command> [options]")
6969
console.log(chalk.dim(`See 'ui5 --help' or 'ui5 build --help' for help`));
7070
}
7171
process.exit(1);
72+
}).onFinishCommand(async () => {
73+
if (process.env.UI5_CLI_PROFILE) {
74+
await (require("../../utils/profile").stop());
75+
}
7276
});

lib/utils/profile.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
let session;
2+
const profile = {
3+
start() {
4+
return new Promise((resolve) => {
5+
if (session) {
6+
resolve();
7+
return;
8+
}
9+
const inspector = require("inspector");
10+
session = new inspector.Session();
11+
session.connect();
12+
session.post("Profiler.enable", () => {
13+
session.post("Profiler.start", () => {
14+
resolve();
15+
});
16+
});
17+
});
18+
},
19+
stop() {
20+
if (!session) {
21+
return;
22+
}
23+
session.post("Profiler.stop", (err, {profile}) => {
24+
if (!err) {
25+
require("fs").writeFileSync(`./ui5_${Date.now().toString(36)}.cpuprofile`, JSON.stringify(profile));
26+
}
27+
});
28+
}
29+
};
30+
31+
module.exports = profile;

0 commit comments

Comments
 (0)