Skip to content

Commit

Permalink
feat: add ProfilingService and update webpack target to node (#2238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove authored Dec 2, 2024
1 parent 8e91318 commit 8072e19
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
4 changes: 3 additions & 1 deletion skymp5-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { BlockedAnimationsService } from "./services/services/blockedAnimationsS
import { WorldView } from "./view/worldView";
import { KeyboardEventsService } from "./services/services/keyboardEventsService";
import { MagicSyncService } from "./services/services/magicSyncService";
import { ProfilingService } from "./services/services/profilingService";

once("update", () => {
Utility.setINIBool("bAlwaysActive:General", true);
Expand Down Expand Up @@ -103,7 +104,8 @@ const main = () => {
new BlockedAnimationsService(sp, controller),
new WorldView(sp, controller),
new KeyboardEventsService(sp, controller),
new MagicSyncService(sp, controller)
new MagicSyncService(sp, controller),
new ProfilingService(sp, controller)
];
SpApiInteractor.setup(listeners);
}
Expand Down
76 changes: 76 additions & 0 deletions skymp5-client/src/services/services/profilingService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { logTrace } from "../../logging";
import { ClientListener, CombinedController, Sp } from "./clientListener";
import { Session } from 'inspector';
import * as fs from "fs";

export class ProfilingService extends ClientListener {
constructor(private sp: Sp, private controller: CombinedController) {
super();
const settings = sp.settings["skymp5-client"];

if (!settings["enableProfiling"]) {
logTrace(this, "ProfilingService: disabled");
return;
}

const profilingDurationMs = this.getInteger(settings["profilingDurationMs"]) || 10000;

const session = new Session();
session.connect();

logTrace(this, "ProfilingService: start");

this.startProfiling(session, profilingDurationMs);
}

private async startProfiling(session: Session, profilingDurationMs: number) {
await new Promise((resolve, reject) => {
session.post('Profiler.enable', (err: Error | null) => {
if (err) {
reject(err);
}
else {
resolve(undefined);
}
});
});

await new Promise((resolve, reject) => {
session.post('Profiler.start', (err: Error | null) => {
if (err) {
reject(err);
}
else {
resolve(undefined);
}
});
});

await new Promise((resolve) => {
setTimeout(resolve, profilingDurationMs);
});

logTrace(this, "ProfilingService: stop");

const { profile } = await new Promise<any>((resolve, reject) => {
session.post('Profiler.stop', (err: Error | null, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});

fs.writeFileSync('./profile.cpuprofile', JSON.stringify(profile));
}

private getInteger(value: unknown) {
if (typeof value === 'number') {
if (Number.isInteger(value)) {
return value;
}
}
return undefined;
}
}
1 change: 1 addition & 0 deletions skymp5-client/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ if (process.env['DEPLOY_PLUGIN']?.includes('true')) {
}

module.exports = {
target: "node",
plugins,
mode: 'development',
devtool: 'inline-source-map',
Expand Down

0 comments on commit 8072e19

Please sign in to comment.