refactor(cli): move the benchmarkd command to folder

pull/90/head
moonrailgun 2 years ago
parent 3f6c3b08d8
commit 48145a01ee

@ -0,0 +1,11 @@
import { CommandModule } from 'yargs';
import { benchMessageCommand } from './message';
// https://docs.docker.com/engine/api/v1.41/
export const benchmarkCommand: CommandModule = {
command: 'benchmark',
describe: 'Tailchat Benchmark Test',
builder: (yargs) => yargs.command(benchMessageCommand).demandCommand(),
handler(args) {},
};

@ -10,97 +10,89 @@ import ora from 'ora';
import prettyMs from 'pretty-ms'; import prettyMs from 'pretty-ms';
import filesize from 'filesize'; import filesize from 'filesize';
export const benchCommand: CommandModule = { export const benchMessageCommand: CommandModule = {
command: 'bench', command: 'message',
describe: 'Benchmark', describe:
'Stress testing through Tailchat network requests (suitable for pure business testing)',
builder: (yargs) => builder: (yargs) =>
yargs yargs
.command( .option('groupId', {
'message', describe: 'Group ID',
'Stress testing through Tailchat network requests (suitable for pure business testing)', demandOption: true,
(yargs) => type: 'string',
yargs })
.option('groupId', { .option('converseId', {
describe: 'Group ID', describe: 'Converse ID',
demandOption: true, demandOption: true,
type: 'string', type: 'string',
}) })
.option('converseId', { .option('userId', {
describe: 'Converse ID', describe: 'User ID',
demandOption: true, demandOption: true,
type: 'string', type: 'string',
}) })
.option('userId', { .option('num', {
describe: 'User ID', describe: 'Test Num',
demandOption: true, type: 'number',
type: 'string', default: 100,
}) })
.option('num', { .option('parallel', {
describe: 'Test Num', describe: 'Is Parallel',
type: 'number', type: 'boolean',
default: 100, default: false,
}) })
.option('parallel', { .option('parallelLimit', {
describe: 'Is Parallel', describe: 'Parallel Limit',
type: 'boolean', type: 'number',
default: false, default: Infinity,
}) }),
.option('parallelLimit', { async handler(args) {
describe: 'Parallel Limit', config(); // 加载环境变量
type: 'number',
default: Infinity, const broker = new TcBroker({
}), ...defaultBrokerConfig,
transporter: process.env.TRANSPORTER,
async (args) => { logger: false,
config(); // 加载环境变量 });
await broker.start();
const broker = new TcBroker({
...defaultBrokerConfig, printSystemInfo();
transporter: process.env.TRANSPORTER,
logger: false, console.log('===============');
});
await broker.start(); await startBenchmark<number>({
parallel: args.parallel as boolean,
printSystemInfo(); parallelLimit: args.parallelLimit as number,
number: args.num as number,
console.log('==============='); task: async (i) => {
const start = process.hrtime();
await startBenchmark<number>({ await broker.call(
parallel: args.parallel, 'chat.message.sendMessage',
parallelLimit: args.parallelLimit, {
number: args.num, converseId: args.converseId,
task: async (i) => { groupId: args.groupId,
const start = process.hrtime(); content: `benchmessage ${i + 1}`,
await broker.call( },
'chat.message.sendMessage', {
{ meta: {
converseId: args.converseId, userId: args.userId,
groupId: args.groupId,
content: `benchmessage ${i + 1}`,
},
{
meta: {
userId: args.userId,
},
}
);
const usage = calcUsage(start);
return usage;
},
onCompleted: (res) => {
console.log(`Test Num: \t${res.length}`);
console.log(`Max Usage: \t${prettyMs(Math.max(...res, 0))}`);
console.log(`Min Usage: \t${prettyMs(Math.min(...res, 0))}`);
console.log(`Average time: \t${prettyMs(_.mean(res))}`);
}, },
}); }
);
await broker.stop(); const usage = calcUsage(start);
}
) return usage;
.demandCommand(), },
handler() {}, onCompleted: (res) => {
console.log(`Test Num: \t${res.length}`);
console.log(`Max Usage: \t${prettyMs(Math.max(...res, 0))}`);
console.log(`Min Usage: \t${prettyMs(Math.min(...res, 0))}`);
console.log(`Average time: \t${prettyMs(_.mean(res))}`);
},
});
await broker.stop();
},
}; };
/** /**

@ -4,7 +4,7 @@ import { createCommand } from './commands/create';
import { connectCommand } from './commands/connect'; import { connectCommand } from './commands/connect';
import { appCommand } from './commands/app'; import { appCommand } from './commands/app';
import { declarationCommand } from './commands/declaration'; import { declarationCommand } from './commands/declaration';
import { benchCommand } from './commands/bench'; import { benchmarkCommand } from './commands/benchmark';
import { dockerCommand } from './commands/docker'; import { dockerCommand } from './commands/docker';
import { usageCommand } from './commands/usage'; import { usageCommand } from './commands/usage';
import { registryCommand } from './commands/registry'; import { registryCommand } from './commands/registry';
@ -15,7 +15,7 @@ yargs
.command(createCommand) .command(createCommand)
.command(connectCommand) .command(connectCommand)
.command(appCommand) .command(appCommand)
.command(benchCommand) .command(benchmarkCommand)
.command(declarationCommand) .command(declarationCommand)
.command(dockerCommand) .command(dockerCommand)
.command(registryCommand) .command(registryCommand)

Loading…
Cancel
Save