perf(cli): 优化bench命令对于请求超时(失败)的处理

pull/70/head
moonrailgun 2 years ago
parent 749c2f7961
commit 8aa944e7e5

@ -8,6 +8,7 @@ import pAll from 'p-all';
import pSeries from 'p-series'; import pSeries from 'p-series';
import ora from 'ora'; import ora from 'ora';
import prettyMs from 'pretty-ms'; import prettyMs from 'pretty-ms';
import filesize from 'filesize';
export const benchCommand: CommandModule = { export const benchCommand: CommandModule = {
command: 'bench', command: 'bench',
@ -16,7 +17,7 @@ export const benchCommand: CommandModule = {
yargs yargs
.command( .command(
'message', 'message',
'通过内网请求进行压力测试(适用于纯业务测试)', '通过Tailchat网络请求进行压力测试(适用于纯业务测试)',
(yargs) => (yargs) =>
yargs yargs
.option('groupId', { .option('groupId', {
@ -43,6 +44,11 @@ export const benchCommand: CommandModule = {
describe: '是否并发', describe: '是否并发',
type: 'boolean', type: 'boolean',
default: false, default: false,
})
.option('parallelLimit', {
describe: '并发上限',
type: 'number',
default: Infinity,
}), }),
async (args) => { async (args) => {
@ -61,6 +67,7 @@ export const benchCommand: CommandModule = {
await startBenchmark<number>({ await startBenchmark<number>({
parallel: args.parallel, parallel: args.parallel,
parallelLimit: args.parallelLimit,
number: args.num, number: args.num,
task: async (i) => { task: async (i) => {
const start = process.hrtime(); const start = process.hrtime();
@ -104,7 +111,7 @@ function printSystemInfo() {
console.log(`系统: \t${os.type()} - ${os.release()}`); console.log(`系统: \t${os.type()} - ${os.release()}`);
console.log(`架构: \t${os.arch()} - ${os.version()}`); console.log(`架构: \t${os.arch()} - ${os.version()}`);
console.log(`CPU: \t${os.cpus().length}`); console.log(`CPU: \t${os.cpus().length}`);
console.log(`内存: \t${os.totalmem()}`); console.log(`内存: \t${filesize(os.totalmem(), { base: 2 })}`);
} }
function calcUsage(startTime: [number, number]) { function calcUsage(startTime: [number, number]) {
@ -142,23 +149,34 @@ async function startBenchmark<T>(options: BenchmarkOptions<T>) {
spinner.start('正在执行基准测试...'); spinner.start('正在执行基准测试...');
try { try {
const startTime = process.hrtime(); const startTime = process.hrtime();
let res: T[] = []; let res: (T | false)[] = [];
if (parallel) { if (parallel) {
res = await pAll<T>( res = await pAll<T | false>(
[...Array.from({ length: number }).map((_, i) => () => task(i))], [
...Array.from({ length: number }).map(
(_, i) => () => task(i).catch(() => false as const)
),
],
{ {
concurrency: parallelLimit, concurrency: parallelLimit,
} }
); );
} else { } else {
res = await pSeries<T>([ res = await pSeries<T | false>([
...Array.from({ length: number }).map((_, i) => () => task(i)), ...Array.from({ length: number }).map(
(_, i) => () => task(i).catch(() => false as const)
),
]); ]);
} }
spinner.succeed(`基准测试完毕, 用时 ${prettyMs(calcUsage(startTime))}`); const allUsage = calcUsage(startTime);
const succeed = res.filter((i): i is T => Boolean(i));
const failed = res.filter((i) => !Boolean(i));
spinner.succeed(`基准测试完毕, 用时: ${prettyMs(allUsage)}`);
console.log(`成功/失败: ${succeed.length}/${failed.length}`);
console.log(`TPS: ${res.length / allUsage}`);
onCompleted(res); onCompleted(succeed);
} catch (err) { } catch (err) {
console.error(err); console.error(err);
spinner.fail(`基准测试出现问题`).stop(); spinner.fail(`基准测试出现问题`).stop();

Loading…
Cancel
Save