dev: measure throughput

This commit is contained in:
cha0s 2024-08-06 00:40:43 -05:00
parent 8a3ef7842c
commit ac11545937
4 changed files with 17 additions and 1 deletions

View File

@ -12,6 +12,7 @@ export default class LocalClient extends Client {
this.worker = undefined; this.worker = undefined;
return; return;
} }
this.throughput.$$down += event.data.byteLength;
this.accept(event.data); this.accept(event.data);
}); });
} }
@ -19,6 +20,7 @@ export default class LocalClient extends Client {
this.worker.postMessage(0); this.worker.postMessage(0);
} }
transmit(packed) { transmit(packed) {
this.throughput.$$up += packed.byteLength;
this.worker.postMessage(packed); this.worker.postMessage(packed);
} }
} }

View File

@ -21,6 +21,7 @@ export default class RemoteClient extends Client {
); );
this.worker.postMessage({host}); this.worker.postMessage({host});
this.worker.onmessage = (event) => { this.worker.onmessage = (event) => {
this.throughput.$$down += event.data.byteLength;
this.accept(event.data); this.accept(event.data);
}; };
} }
@ -29,6 +30,7 @@ export default class RemoteClient extends Client {
this.socket = new WebSocket(url.href); this.socket = new WebSocket(url.href);
this.socket.binaryType = 'arraybuffer'; this.socket.binaryType = 'arraybuffer';
const onMessage = (event) => { const onMessage = (event) => {
this.throughput.$$down += event.data.byteLength;
this.accept(event.data); this.accept(event.data);
} }
const {promise, resolve} = withResolvers(); const {promise, resolve} = withResolvers();
@ -54,6 +56,7 @@ export default class RemoteClient extends Client {
} }
} }
transmit(packed) { transmit(packed) {
this.throughput.$$up += packed.byteLength;
if (CLIENT_PREDICTION) { if (CLIENT_PREDICTION) {
this.worker.postMessage(packed); this.worker.postMessage(packed);
} }

View File

@ -2,8 +2,15 @@ import {CLIENT_LATENCY} from '@/util/constants.js';
import EventEmitter from '@/util/event-emitter.js'; import EventEmitter from '@/util/event-emitter.js';
export default class Client { export default class Client {
emitter = new EventEmitter();
throughput = {$$down: 0, down: 0, $$up: 0, up: 0};
constructor() { constructor() {
this.emitter = new EventEmitter(); setInterval(() => {
const {throughput} = this;
throughput.down = throughput.$$down * 4;
throughput.up = throughput.$$up * 4;
throughput.$$down = throughput.$$up = 0;
}, 250);
} }
accept(packet) { accept(packet) {
this.emitter.invoke(packet.type, packet.payload); this.emitter.invoke(packet.type, packet.payload);

View File

@ -2,6 +2,7 @@ import {useCallback, useState} from 'react';
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'; import {Tab, Tabs, TabList, TabPanel} from 'react-tabs';
import 'react-tabs/style/react-tabs.css'; import 'react-tabs/style/react-tabs.css';
import {useClient} from '@/react/context/client.js';
import {useEcsTick} from '@/react/context/ecs.js'; import {useEcsTick} from '@/react/context/ecs.js';
import {useMainEntity} from '@/react/context/main-entity.js'; import {useMainEntity} from '@/react/context/main-entity.js';
@ -12,6 +13,7 @@ import Tiles from './devtools/tiles.jsx';
export default function Devtools({ export default function Devtools({
eventsChannel, eventsChannel,
}) { }) {
const client = useClient();
const [mainEntity] = useMainEntity(); const [mainEntity] = useMainEntity();
const [mainEntityJson, setMainEntityJson] = useState(''); const [mainEntityJson, setMainEntityJson] = useState('');
const onEcsTick = useCallback((payload, ecs) => { const onEcsTick = useCallback((payload, ecs) => {
@ -32,6 +34,8 @@ export default function Devtools({
<div className={styles.dashboard}> <div className={styles.dashboard}>
<form> <form>
<div className={styles.engineBar}> <div className={styles.engineBar}>
<div>{Math.round(((client.throughput.down * 8) / 1024) * 10) / 10}kb/s</div>
<div>{Math.round(((client.throughput.up * 8) / 1024) * 10) / 10}kb/s</div>
</div> </div>
</form> </form>
<pre><code><small>{mainEntityJson}</small></code></pre> <pre><code><small>{mainEntityJson}</small></code></pre>