Skip to content

Commit

Permalink
feat: basic profiling stats
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacThoman committed Jan 17, 2025
1 parent eeae418 commit 1281af9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
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
3 changes: 3 additions & 0 deletions src/client/ui/ChatOverlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ export class ChatOverlay {
//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),);

linesToRender.push('tickTime: ' + (this.networking.getServerInfo().tickComputeTime * 1000).toFixed(2) + 'ms');
linesToRender.push('cleanupTime: ' + (this.networking.getServerInfo().cleanupComputeTime * 1000).toFixed(2) + 'ms');

for (const msg of this.localPlayer.gameMsgs2) {
linesToRender.push(msg);
}
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

0 comments on commit 1281af9

Please sign in to comment.