Skip to content
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

feat: server profiling #228

Merged
merged 4 commits into from
Jan 18, 2025
Merged
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
21 changes: 20 additions & 1 deletion src/client/core/CommandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,33 @@ export class CommandManager {

this.commands.push(
new Command('prettyText', (args: string[]): string => {
if (args[1] == null) return 'prettyText is currently ' + SettingsManager.settings.doPrettyText;
if (args[1] == null) {
SettingsManager.settings.doPrettyText = !SettingsManager.settings.doPrettyText;
SettingsManager.write();
return 'toggled prettyText to ' + SettingsManager.settings.doPrettyText;
}
if (args[1] == 'true') SettingsManager.settings.doPrettyText = true;
else if (args[1] == 'false') SettingsManager.settings.doPrettyText = false;
else return 'invalid input (true/false)';
SettingsManager.write();
return 'prettyText set to ' + args[1];
}),
);

this.commands.push(
new Command('debug', (args: string[]): string => {
if (args[1] == null) {
SettingsManager.settings.developerMode = !SettingsManager.settings.developerMode;
SettingsManager.write();
return 'toggled developer mode to ' + SettingsManager.settings.developerMode;
}
if (args[1] == 'true') SettingsManager.settings.developerMode = true;
else if (args[1] == 'false') SettingsManager.settings.developerMode = false;
else return 'invalid input (true/false)';
SettingsManager.write();
return 'developer mode set to ' + args[1];
}),
);
}

public runCmd(cmd: string): boolean {
Expand Down
4 changes: 4 additions & 0 deletions src/client/core/Networking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface ServerInfo {
gameMode: string;
playerMaxHealth: number;
skyColor: string;
tickComputeTime: number;
cleanupComputeTime: number;
}

interface LastUploadedLocalPlayer {
Expand Down Expand Up @@ -70,6 +72,8 @@ export class Networking {
gameMode: '',
playerMaxHealth: 0,
skyColor: '#000000',
tickComputeTime: 0,
cleanupComputeTime: 0,
};

this.setupSocketListeners();
Expand Down
2 changes: 2 additions & 0 deletions src/client/core/SettingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class SettingsManager {
crosshairType: 1,
viewBobbingStrength: 1,
doPrettyText: false,
developerMode: false,
};
}

Expand All @@ -36,4 +37,5 @@ interface Settings {
crosshairType: number;
viewBobbingStrength: number;
doPrettyText: boolean;
developerMode: boolean;
}
47 changes: 32 additions & 15 deletions src/client/ui/ChatOverlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,31 +407,48 @@ export class ChatOverlay {
const framerate = this.renderer.getFramerate();

if (this.localPlayer.latency >= 999) {
linesToRender.push('disconnected :(');
linesToRender.push('&cdisconnected :(');
}

//const playerX = Math.round(this.localPlayer.position.x);

linesToRender.push(
'candiru ' + this.localPlayer.gameVersion + ' @ ' + Math.round(framerate) + 'fps, ' +
Math.round(this.localPlayer.latency) + 'ms',
);
//linesToRender.push('connected to: ' + this.networking.getServerInfo().name);
//linesToRender.push('players: ' + this.networking.getServerInfo().currentPlayers + '/' + this.networking.getServerInfo().maxPlayers);
//linesToRender.push('map: ' + this.networking.getServerInfo().mapName);
//linesToRender.push('mode: ' + this.networking.getServerInfo().gameMode);
//linesToRender.push('serverVersion: ' + this.networking.getServerInfo().version);
//linesToRender.push('tickRate: ' + this.networking.getServerInfo().tickRate);
//linesToRender.push('playerMaxHealth: ' + this.networking.getServerInfo().playerMaxHealth);
//linesToRender.push('health: ' + this.localPlayer.health);
//linesToRender.push('pos:' +this.localPlayer.position.x.toFixed(2) + ',' + this.localPlayer.position.y.toFixed(2) + ',' +this.localPlayer.position.z.toFixed(2),);

//const playerX = Math.round(this.localPlayer.position.x);
if (SettingsManager.settings.developerMode) {
linesToRender.push(
this.networking.getServerInfo().name + ' (' + this.networking.getServerInfo().currentPlayers + '/' +
this.networking.getServerInfo().maxPlayers + ')',
);
linesToRender.push(
'map: ' + this.networking.getServerInfo().mapName + ', mode: ' + this.networking.getServerInfo().gameMode +
', v' +
this.networking.getServerInfo().version,
);

linesToRender.push('tps: ' + this.networking.getServerInfo().tickRate);
linesToRender.push('maxHealth: ' + this.networking.getServerInfo().playerMaxHealth);
linesToRender.push('health: ' + this.localPlayer.health);
linesToRender.push(
'pos:' + this.localPlayer.position.x.toFixed(2) + ',' + this.localPlayer.position.y.toFixed(2) + ',' +
this.localPlayer.position.z.toFixed(2),
);
const tickTimeMs = this.networking.getServerInfo().tickComputeTime * 1000;
const cleanupTimeMs = this.networking.getServerInfo().cleanupComputeTime * 1000;
const tickSpeedMs = 1 / this.networking.getServerInfo().tickRate * 1000;
const tickTimePercent = (tickTimeMs / tickSpeedMs) * 100;

linesToRender.push(
'tickTime: ' + tickTimeMs.toFixed(2) + '/' + tickSpeedMs.toFixed(2) + 'ms (' + tickTimePercent.toFixed(2) +
'%)',
);
linesToRender.push('cleanupTime: ' + cleanupTimeMs.toFixed(2) + 'ms');
}

for (const msg of this.localPlayer.gameMsgs2) {
linesToRender.push(msg);
}

//linesToRender.push('routineTime: ' + this.lastRoutineMs + 'ms');

for (let i = 0; i < linesToRender.length; i++) {
this.renderPixelText(linesToRender[i], 2, 7 + 7 * i, 'teal');
}
Expand Down
21 changes: 21 additions & 0 deletions src/server/GameEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class GameEngine {
public serverInfo: ServerInfo = new ServerInfo();
public gamemode: Gamemode | false = false;

private tickProfileSamples: number = 0;
private tickProfileTime: number = 0;
private cleanupProfileSamples: number = 0;
private cleanupProfileTime: number = 0;

constructor(
public playerManager: PlayerManager,
public itemManager: ItemManager,
Expand Down Expand Up @@ -62,6 +67,14 @@ export class GameEngine {
console.error('⚠ error emitting item data:', err);
}
}

this.tickProfileSamples++;
this.tickProfileTime += Date.now() / 1000 - currentTime;
if (this.tickProfileSamples >= 100) {
this.serverInfo.tickComputeTime = this.tickProfileTime / this.tickProfileSamples;
this.tickProfileSamples = 0;
this.tickProfileTime = 0;
}
} catch (error) {
console.error('⚠ error in serverTick:', error);
}
Expand Down Expand Up @@ -119,6 +132,14 @@ export class GameEngine {
});

if (this.gamemode) this.gamemode.onPeriodicCleanup();

this.cleanupProfileSamples++;
this.cleanupProfileTime += Date.now() / 1000 - currentTime;
if (this.cleanupProfileSamples >= 10) {
this.serverInfo.cleanupComputeTime = this.cleanupProfileTime / this.cleanupProfileSamples;
this.cleanupProfileSamples = 0;
this.cleanupProfileTime = 0;
}
} catch (error) {
console.error('⚠ error in periodicCleanup:', error);
}
Expand Down
4 changes: 4 additions & 0 deletions src/server/models/ServerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class ServerInfo {
public gameMode: string;
public playerMaxHealth: number;
public skyColor: string;
public tickComputeTime: number = 0;
public cleanupComputeTime: number = 0;
constructor() {
this.name = config.server.name;
this.maxPlayers = config.game.maxPlayers;
Expand All @@ -18,6 +20,8 @@ export class ServerInfo {
this.gameMode = config.game.mode;
this.playerMaxHealth = config.player.maxHealth;
this.skyColor = '#000000';
this.tickComputeTime = 0;
this.cleanupComputeTime = 0;
}
toJSON() {
return {
Expand Down
Loading