summary refs log tree commit diff
path: root/scripts/stress/identify.js
blob: 7dea5e4de6fc6a997afc3517f151523dbc22319d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* eslint-env node */

require("dotenv").config();
const { OPCODES } = require("../../dist/gateway/util/Constants.js");
const WebSocket = require("ws");
const ENDPOINT = `ws://localhost:3002?v=9&encoding=json`;
const TOKEN =
	"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEwOTMxMTgwMjgzNjA1MzYxMDYiLCJpYXQiOjE2ODA2OTE5MDB9.9ByCqDvC4mIutW8nM7WhVCtGuKW08UimPnmBeNw-K0E";
const TOTAL_ITERATIONS = 500;

const doTimedIdentify = () =>
	new Promise((resolve) => {
		let start;
		const ws = new WebSocket(ENDPOINT);
		ws.on("message", (data) => {
			const parsed = JSON.parse(data);

			switch (parsed.op) {
				case OPCODES.Hello:
					// send identify
					start = performance.now();
					ws.send(
						JSON.stringify({
							op: OPCODES.Identify,
							d: {
								token: TOKEN,
								properties: {},
							},
						}),
					);
					break;
				case OPCODES.Dispatch:
					if (parsed.t == "READY") {
						ws.close();
						return resolve(performance.now() - start);
					}

					break;
			}
		});
	});

(async () => {
	const perfs = [];
	while (perfs.length < TOTAL_ITERATIONS) {
		const ret = await doTimedIdentify();
		perfs.push(ret);
		// console.log(`${perfs.length}/${TOTAL_ITERATIONS} - this: ${Math.floor(ret)}ms`)
	}

	const avg = perfs.reduce((prev, curr) => prev + curr) / (perfs.length - 1);
	console.log(`Average identify time: ${Math.floor(avg * 100) / 100}ms`);
})();