refactor: change benchmark register command and add progress

pull/90/head
moonrailgun 2 years ago
parent 46ca226f96
commit fcc894f4d8

@ -1,18 +1,19 @@
import { CommandModule } from 'yargs';
import fs from 'fs-extra';
import got from 'got';
import ora from 'ora';
export const benchmarkRegisterCommand: CommandModule = {
command: 'register',
describe: 'Create Tailchat temp account and output token',
command: 'register <url>',
describe: 'Create Tailchat temporary account and output token',
builder: (yargs) =>
yargs
.option('url', {
describe: 'Backend Url',
demandOption: true,
type: 'string',
})
.option('accountPath', {
.example(
'$0 benchmark register http://127.0.0.1:11000',
'Register account in local server'
)
.demandOption('url', 'Backend Url')
.option('file', {
describe: 'Account Token Path',
demandOption: true,
type: 'string',
@ -27,7 +28,13 @@ export const benchmarkRegisterCommand: CommandModule = {
async handler(args) {
const count = args.count as number;
const tokens: string[] = [];
const start = Date.now();
const spinner = ora().info(`Register temporary account`).start();
for (let i = 0; i < count; i++) {
spinner.text = `Progress: ${i + 1}/${count}`;
const token = await registerTemporaryAccount(
args.url as string,
`benchUser-${i}`
@ -35,7 +42,11 @@ export const benchmarkRegisterCommand: CommandModule = {
tokens.push(token);
}
await fs.writeFile(args.accountPath as string, tokens.join('\n'));
spinner.info(`Writing tokens into path: ${args.file}`);
await fs.writeFile(args.file as string, tokens.join('\n'));
spinner.succeed(`Register completed! Usage: ${Date.now() - start}ms`);
},
};
@ -49,7 +60,7 @@ async function registerTemporaryAccount(
nickname,
},
})
.json<{ token: string }>();
.json<{ data: { token: string } }>();
return res.token;
return res.data.token;
}

Loading…
Cancel
Save