|
|
|
@ -4,6 +4,7 @@ import msgpackParser from 'socket.io-msgpack-parser';
|
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
|
import ora from 'ora';
|
|
|
|
|
import randomString from 'crypto-random-string';
|
|
|
|
|
import pMap from 'p-map';
|
|
|
|
|
|
|
|
|
|
const CLIENT_CREATION_INTERVAL_IN_MS = 5;
|
|
|
|
|
|
|
|
|
@ -19,6 +20,11 @@ export const benchmarkConnectionsCommand: CommandModule = {
|
|
|
|
|
type: 'string',
|
|
|
|
|
default: './accounts',
|
|
|
|
|
})
|
|
|
|
|
.option('concurrency', {
|
|
|
|
|
describe: 'Concurrency when create connection',
|
|
|
|
|
type: 'number',
|
|
|
|
|
default: 1,
|
|
|
|
|
})
|
|
|
|
|
.option('groupId', {
|
|
|
|
|
describe: 'Group Id which send Message',
|
|
|
|
|
type: 'string',
|
|
|
|
@ -26,12 +32,19 @@ export const benchmarkConnectionsCommand: CommandModule = {
|
|
|
|
|
.option('converseId', {
|
|
|
|
|
describe: 'Converse Id which send Message',
|
|
|
|
|
type: 'string',
|
|
|
|
|
})
|
|
|
|
|
.option('messageNum', {
|
|
|
|
|
describe: 'Times which send Message',
|
|
|
|
|
type: 'number',
|
|
|
|
|
default: 1,
|
|
|
|
|
}),
|
|
|
|
|
async handler(args) {
|
|
|
|
|
const url = args.url as string;
|
|
|
|
|
const file = args.file as string;
|
|
|
|
|
const groupId = args.groupId as string;
|
|
|
|
|
const converseId = args.converseId as string;
|
|
|
|
|
const messageNum = args.messageNum as number;
|
|
|
|
|
const concurrency = args.concurrency as number;
|
|
|
|
|
|
|
|
|
|
console.log('Reading account tokens from', file);
|
|
|
|
|
const account = await fs.readFile(file as string, {
|
|
|
|
@ -39,64 +52,42 @@ export const benchmarkConnectionsCommand: CommandModule = {
|
|
|
|
|
});
|
|
|
|
|
const sockets = await createClients(
|
|
|
|
|
url as string,
|
|
|
|
|
account.split('\n').map((s) => s.trim())
|
|
|
|
|
account.split('\n').map((s) => s.trim()),
|
|
|
|
|
concurrency
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (groupId && converseId) {
|
|
|
|
|
// send message test
|
|
|
|
|
const randomMessage = randomString({ length: 16 });
|
|
|
|
|
const spinner = ora()
|
|
|
|
|
.info(`Start message receive test, message: ${randomMessage}`)
|
|
|
|
|
.start();
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
let receiveCount = 0;
|
|
|
|
|
const len = sockets.length;
|
|
|
|
|
|
|
|
|
|
function receivedCallback() {
|
|
|
|
|
receiveCount += 1;
|
|
|
|
|
spinner.text = `Receive: ${receiveCount}/${len}`;
|
|
|
|
|
|
|
|
|
|
if (receiveCount === len) {
|
|
|
|
|
spinner.succeed(
|
|
|
|
|
`All client received, usage: ${Date.now() - start}ms`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
for (let i = 0; i < messageNum; i++) {
|
|
|
|
|
console.log('Start send message test:', i + 1);
|
|
|
|
|
await sendMessage(sockets, groupId, converseId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sockets.forEach((socket) => {
|
|
|
|
|
socket.on('notify:chat.message.add', (message) => {
|
|
|
|
|
const content = message.content;
|
|
|
|
|
|
|
|
|
|
if (message.converseId === converseId && randomMessage === content) {
|
|
|
|
|
receivedCallback();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sockets[0].emit('chat.message.sendMessage', {
|
|
|
|
|
groupId,
|
|
|
|
|
converseId,
|
|
|
|
|
content: randomMessage,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function createClients(
|
|
|
|
|
url: string,
|
|
|
|
|
accountTokens: string[]
|
|
|
|
|
accountTokens: string[],
|
|
|
|
|
concurrency: number
|
|
|
|
|
): Promise<Socket[]> {
|
|
|
|
|
const maxCount = accountTokens.length;
|
|
|
|
|
const spinner = ora().info(`Create Client Connection to ${url}`).start();
|
|
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
|
const sockets: Socket[] = [];
|
|
|
|
|
for (const token of accountTokens) {
|
|
|
|
|
await sleep(CLIENT_CREATION_INTERVAL_IN_MS);
|
|
|
|
|
spinner.text = `Progress: ${++i}/${maxCount}`;
|
|
|
|
|
const socket = await createClient(url, token);
|
|
|
|
|
sockets.push(socket);
|
|
|
|
|
}
|
|
|
|
|
await pMap(
|
|
|
|
|
accountTokens,
|
|
|
|
|
async (token) => {
|
|
|
|
|
await sleep(CLIENT_CREATION_INTERVAL_IN_MS);
|
|
|
|
|
const socket = await createClient(url, token);
|
|
|
|
|
spinner.text = `Progress: ${++i}/${maxCount}`;
|
|
|
|
|
sockets.push(socket);
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
concurrency,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
spinner.succeed(`${maxCount} clients has been create.`);
|
|
|
|
|
|
|
|
|
@ -131,7 +122,51 @@ function createClient(url: string, token: string): Promise<Socket> {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function sleep(milliseconds: number): Promise<void> {
|
|
|
|
|
async function sendMessage(
|
|
|
|
|
sockets: Socket[],
|
|
|
|
|
groupId: string,
|
|
|
|
|
converseId: string
|
|
|
|
|
) {
|
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
|
const randomMessage = randomString({ length: 16 });
|
|
|
|
|
const spinner = ora()
|
|
|
|
|
.info(`Start message receive test, message: ${randomMessage}`)
|
|
|
|
|
.start();
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
let receiveCount = 0;
|
|
|
|
|
const len = sockets.length;
|
|
|
|
|
|
|
|
|
|
function receivedCallback() {
|
|
|
|
|
receiveCount += 1;
|
|
|
|
|
spinner.text = `Receive: ${receiveCount}/${len}`;
|
|
|
|
|
|
|
|
|
|
if (receiveCount === len) {
|
|
|
|
|
spinner.succeed(`All client received, usage: ${Date.now() - start}ms`);
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sockets.forEach((socket) => {
|
|
|
|
|
socket.on('notify:chat.message.add', (message) => {
|
|
|
|
|
const content = message.content;
|
|
|
|
|
|
|
|
|
|
if (message.converseId === converseId && randomMessage === content) {
|
|
|
|
|
socket.off('notify:chat.message.add');
|
|
|
|
|
|
|
|
|
|
receivedCallback();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sockets[0].emit('chat.message.sendMessage', {
|
|
|
|
|
groupId,
|
|
|
|
|
converseId,
|
|
|
|
|
content: randomMessage,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sleep(milliseconds: number): Promise<void> {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
setTimeout(resolve, milliseconds);
|
|
|
|
|
});
|
|
|
|
|