mirror of https://github.com/msgbyte/tailchat
feat(cli): add benchmark connections and register command
parent
48145a01ee
commit
9524502d70
@ -0,0 +1,73 @@
|
|||||||
|
import { CommandModule } from 'yargs';
|
||||||
|
import { io, Socket } from 'socket.io-client';
|
||||||
|
import msgpackParser from 'socket.io-msgpack-parser';
|
||||||
|
import fs from 'fs-extra';
|
||||||
|
|
||||||
|
const CLIENT_CREATION_INTERVAL_IN_MS = 5;
|
||||||
|
|
||||||
|
export const benchmarkConnectionsCommand: CommandModule = {
|
||||||
|
command: 'connections',
|
||||||
|
describe: 'Test Tailchat Connections',
|
||||||
|
builder: (yargs) =>
|
||||||
|
yargs
|
||||||
|
.option('url', {
|
||||||
|
describe: 'Url',
|
||||||
|
demandOption: true,
|
||||||
|
type: 'string',
|
||||||
|
})
|
||||||
|
.option('accountPath', {
|
||||||
|
describe: 'Account Token Path',
|
||||||
|
demandOption: true,
|
||||||
|
type: 'string',
|
||||||
|
}),
|
||||||
|
async handler(args) {
|
||||||
|
const account = await fs.readFile(args.accountPath as string, {
|
||||||
|
encoding: 'utf8',
|
||||||
|
});
|
||||||
|
createClients(
|
||||||
|
args.url as string,
|
||||||
|
account.split('\n').map((s) => s.trim())
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
async function createClients(url: string, accountTokens: string[]) {
|
||||||
|
const maxCount = accountTokens.length;
|
||||||
|
|
||||||
|
for (const token of accountTokens) {
|
||||||
|
await sleep(CLIENT_CREATION_INTERVAL_IN_MS);
|
||||||
|
await createClient(url, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`${maxCount} clients has been create.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createClient(url: string, token: string): Promise<Socket> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const socket = io(url, {
|
||||||
|
transports: ['websocket'],
|
||||||
|
auth: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
forceNew: true,
|
||||||
|
parser: msgpackParser,
|
||||||
|
});
|
||||||
|
socket.once('connect', () => {
|
||||||
|
// 连接成功
|
||||||
|
resolve(socket);
|
||||||
|
});
|
||||||
|
socket.once('error', () => {
|
||||||
|
reject();
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('disconnect', (reason) => {
|
||||||
|
console.log(`disconnect due to ${reason}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sleep(milliseconds: number): Promise<void> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
setTimeout(resolve, milliseconds);
|
||||||
|
});
|
||||||
|
}
|
@ -1,11 +1,18 @@
|
|||||||
import { CommandModule } from 'yargs';
|
import { CommandModule } from 'yargs';
|
||||||
import { benchMessageCommand } from './message';
|
import { benchmarkConnectionsCommand } from './connections';
|
||||||
|
import { benchmarkMessageCommand } from './message';
|
||||||
|
import { benchmarkRegisterCommand } from './register';
|
||||||
|
|
||||||
// https://docs.docker.com/engine/api/v1.41/
|
// https://docs.docker.com/engine/api/v1.41/
|
||||||
|
|
||||||
export const benchmarkCommand: CommandModule = {
|
export const benchmarkCommand: CommandModule = {
|
||||||
command: 'benchmark',
|
command: 'benchmark',
|
||||||
describe: 'Tailchat Benchmark Test',
|
describe: 'Tailchat Benchmark Test',
|
||||||
builder: (yargs) => yargs.command(benchMessageCommand).demandCommand(),
|
builder: (yargs) =>
|
||||||
|
yargs
|
||||||
|
.command(benchmarkMessageCommand)
|
||||||
|
.command(benchmarkConnectionsCommand)
|
||||||
|
.command(benchmarkRegisterCommand)
|
||||||
|
.demandCommand(),
|
||||||
handler(args) {},
|
handler(args) {},
|
||||||
};
|
};
|
||||||
|
@ -0,0 +1,55 @@
|
|||||||
|
import { CommandModule } from 'yargs';
|
||||||
|
import fs from 'fs-extra';
|
||||||
|
import got from 'got';
|
||||||
|
|
||||||
|
export const benchmarkRegisterCommand: CommandModule = {
|
||||||
|
command: 'register',
|
||||||
|
describe: 'Create Tailchat temp account and output token',
|
||||||
|
builder: (yargs) =>
|
||||||
|
yargs
|
||||||
|
.option('url', {
|
||||||
|
describe: 'Backend Url',
|
||||||
|
demandOption: true,
|
||||||
|
type: 'string',
|
||||||
|
})
|
||||||
|
.option('accountPath', {
|
||||||
|
describe: 'Account Token Path',
|
||||||
|
demandOption: true,
|
||||||
|
type: 'string',
|
||||||
|
default: './accounts',
|
||||||
|
})
|
||||||
|
.option('count', {
|
||||||
|
describe: 'Register Count',
|
||||||
|
demandOption: true,
|
||||||
|
type: 'number',
|
||||||
|
default: 100,
|
||||||
|
}),
|
||||||
|
async handler(args) {
|
||||||
|
const count = args.count as number;
|
||||||
|
const tokens: string[] = [];
|
||||||
|
for (let i = 0; i < count; i++) {
|
||||||
|
const token = await registerTemporaryAccount(
|
||||||
|
args.url as string,
|
||||||
|
`benchUser-${i}`
|
||||||
|
);
|
||||||
|
tokens.push(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
await fs.writeFile(args.accountPath as string, tokens.join('\n'));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
async function registerTemporaryAccount(
|
||||||
|
url: string,
|
||||||
|
nickname: string
|
||||||
|
): Promise<string> {
|
||||||
|
const res = await got
|
||||||
|
.post(`${url}/api/user/createTemporaryUser`, {
|
||||||
|
json: {
|
||||||
|
nickname,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.json<{ token: string }>();
|
||||||
|
|
||||||
|
return res.token;
|
||||||
|
}
|
Loading…
Reference in New Issue