Skip to content

Commit

Permalink
[INTERNAL] Use promise inspector API
Browse files Browse the repository at this point in the history
Available since Node v19
  • Loading branch information
RandomByte committed Dec 4, 2024
1 parent b1dae76 commit 269d9bb
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions lib/utils/profile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {writeFile} from "node:fs/promises";
import {Session} from "node:inspector";
import {writeFileSync} from "node:fs";
import {Session} from "node:inspector/promises";

let session;
let processSignals;
Expand All @@ -10,15 +10,10 @@ export async function start() {
}
session = new Session();
session.connect();
await new Promise((resolve) => {
session.post("Profiler.enable", () => {
console.log(`Recording CPU profile...`);
session.post("Profiler.start", () => {
processSignals = registerSigHooks();
resolve();
});
});
});
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) {
Expand All @@ -39,30 +34,22 @@ async function writeProfile(profile) {
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
await writeFile(fileName, JSON.stringify(profile));
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;
}
const profile = await new Promise((resolve) => {
session.post("Profiler.stop", (err, {profile}) => {
if (err) {
resolve(null);
} else {
resolve(profile);
}
});
session = null;
});
if (profile) {
await writeProfile(profile);
}
}

function registerSigHooks() {
Expand Down

0 comments on commit 269d9bb

Please sign in to comment.