Adding CPU tracking: if CPU > 80%, ignore position of moving players

This commit is contained in:
David Négrier 2020-09-30 12:50:34 +02:00
parent 27871641aa
commit a87cdc543b
3 changed files with 47 additions and 36 deletions

View File

@ -49,6 +49,7 @@ import Direction = PositionMessage.Direction;
import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils"; import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils";
import {App, HttpRequest, TemplatedApp, WebSocket} from "uWebSockets.js" import {App, HttpRequest, TemplatedApp, WebSocket} from "uWebSockets.js"
import {parse} from "query-string"; import {parse} from "query-string";
import {cpuTracker} from "../Services/CpuTracker";
function emitInBatch(socket: ExSocketInterface, payload: SubMessage): void { function emitInBatch(socket: ExSocketInterface, payload: SubMessage): void {
socket.batchedMessages.addPayload(payload); socket.batchedMessages.addPayload(payload);
@ -90,41 +91,6 @@ export class IoSocketController {
}); });
this.ioConnection(); this.ioConnection();
let time = process.hrtime.bigint()
let usage = process.cpuUsage()
function secNSec2ms(secNSec) {
if (Array.isArray(secNSec)) {
return secNSec[0] * 1000 + secNSec[1] / 1000000;
}
return secNSec / 1000;
}
let oldCpuUsage = process.cpuUsage();
setInterval(() => {
let elapTime = process.hrtime.bigint();
let elapUsage = process.cpuUsage(usage)
usage = process.cpuUsage()
let elapTimeMS = elapTime - time;
let elapUserMS = secNSec2ms(elapUsage.user)
let elapSystMS = secNSec2ms(elapUsage.system)
let cpuPercent = Math.round(100 * (elapUserMS + elapSystMS) / Number(elapTimeMS) * 1000000)
time = elapTime;
//usage = elapUsage;
console.log('elapsed time ms: ', elapTimeMS)
console.log('elapsed user ms: ', elapUserMS)
console.log('elapsed system ms:', elapSystMS)
console.log('cpu percent: ', cpuPercent)
}, 500);
} }
private isValidToken(token: object): token is TokenInterface { private isValidToken(token: object): token is TokenInterface {
@ -452,6 +418,11 @@ export class IoSocketController {
try { try {
const userMoves = userMovesMessage.toObject(); const userMoves = userMovesMessage.toObject();
// If CPU is high, let's drop messages of users moving (we will only dispatch the final position)
if (cpuTracker.getCpuPercent() > 80 && userMoves.position?.moving === true) {
return;
}
const position = userMoves.position; const position = userMoves.position;
if (position === undefined) { if (position === undefined) {
throw new Error('Position not found in message'); throw new Error('Position not found in message');

View File

@ -0,0 +1,40 @@
function secNSec2ms(secNSec: Array<number>|number) {
if (Array.isArray(secNSec)) {
return secNSec[0] * 1000 + secNSec[1] / 1000000;
}
return secNSec / 1000;
}
class CpuTracker {
private cpuPercent: number = 0;
constructor() {
let time = process.hrtime.bigint()
let usage = process.cpuUsage()
setInterval(() => {
let elapTime = process.hrtime.bigint();
let elapUsage = process.cpuUsage(usage)
usage = process.cpuUsage()
let elapTimeMS = elapTime - time;
let elapUserMS = secNSec2ms(elapUsage.user)
let elapSystMS = secNSec2ms(elapUsage.system)
this.cpuPercent = Math.round(100 * (elapUserMS + elapSystMS) / Number(elapTimeMS) * 1000000)
time = elapTime;
/*console.log('elapsed time ms: ', elapTimeMS)
console.log('elapsed user ms: ', elapUserMS)
console.log('elapsed system ms:', elapSystMS)
console.log('cpu percent: ', this.cpuPercent)*/
}, 500);
}
public getCpuPercent(): number {
return this.cpuPercent;
}
}
const cpuTracker = new CpuTracker();
export { cpuTracker };

View File

@ -14,7 +14,7 @@ async function startOneUser(): Promise<void> {
const connection = await connectionManager.connectToRoomSocket(); const connection = await connectionManager.connectToRoomSocket();
connection.emitPlayerDetailsMessage('foo', ['male3']); connection.emitPlayerDetailsMessage('foo', ['male3']);
await connection.joinARoom('global__maps.workadventure.localhost/Floor0/floor0', 783, 170, 'down', false, { await connection.joinARoom('global__maps.workadventure.localhost/Floor0/floor0', 783, 170, 'down', true, {
top: 0, top: 0,
bottom: 200, bottom: 200,
left: 500, left: 500,