diff --git a/cli/src/lib/conn.ts b/cli/src/lib/conn.ts index fa5788d47..19622529a 100644 --- a/cli/src/lib/conn.ts +++ b/cli/src/lib/conn.ts @@ -19,7 +19,7 @@ const conn = (options: ConnectOptions) => { let retryTimes = 0 - basicLog.connecting() + basicLog.connecting(config, connOpts.hostname!, connOpts.port) client.on('connect', () => { basicLog.connected() @@ -54,7 +54,7 @@ const benchConn = async (options: BenchConnectOptions) => { save && saveConfig('benchConn', options) - const { count, interval, clientId, maximunReconnectTimes } = options + const { count, interval, hostname, port, clientId, maximunReconnectTimes } = options const connOpts = parseConnectOptions(options, 'conn') @@ -66,7 +66,7 @@ const benchConn = async (options: BenchConnectOptions) => { const interactive = new Signale({ interactive: true }) - signale.info(`Start the connect benchmarking, connections: ${count}, req interval: ${interval}ms`) + benchLog.start.conn(config, count, interval, hostname, port) const start = Date.now() diff --git a/cli/src/lib/pub.ts b/cli/src/lib/pub.ts index 586559ed2..9fbc79583 100644 --- a/cli/src/lib/pub.ts +++ b/cli/src/lib/pub.ts @@ -10,11 +10,12 @@ import delay from '../utils/delay' import { saveConfig, loadConfig } from '../utils/config' const send = ( + config: boolean | string | undefined, connOpts: IClientOptions, pubOpts: { topic: string; message: string | Buffer; opts: IClientPublishOptions }, ) => { const client = mqtt.connect(connOpts) - basicLog.connecting() + basicLog.connecting(config, connOpts.hostname!, connOpts.port, pubOpts.topic, pubOpts.message.toString()) client.on('connect', () => { basicLog.connected() const { topic, message } = pubOpts @@ -35,6 +36,7 @@ const send = ( } const multisend = ( + config: boolean | string | undefined, connOpts: IClientOptions, pubOpts: { topic: string; message: string | Buffer; opts: IClientPublishOptions }, maximunReconnectTimes: number, @@ -42,7 +44,7 @@ const multisend = ( let isNewConnection = true let retryTimes = 0 const client = mqtt.connect(connOpts) - basicLog.connecting() + basicLog.connecting(config, connOpts.hostname!, connOpts.port, pubOpts.topic) const sender = new Writable({ objectMode: true, }) @@ -104,17 +106,17 @@ const pub = (options: PublishOptions) => { if (options.stdin) { if (options.multiline) { - multisend(connOpts, pubOpts, options.maximunReconnectTimes) + multisend(config, connOpts, pubOpts, options.maximunReconnectTimes) } else { process.stdin.pipe( concat((data) => { pubOpts.message = data - send(connOpts, pubOpts) + send(config, connOpts, pubOpts) }), ) } } else { - send(connOpts, pubOpts) + send(config, connOpts, pubOpts) } } @@ -125,9 +127,9 @@ const benchPub = async (options: BenchPublishOptions) => { save && saveConfig('benchPub', options) - checkTopicExists(options.topic, 'benchPub') + const { count, interval, messageInterval, hostname, port, topic, clientId, verbose, maximunReconnectTimes } = options - const { count, interval, messageInterval, clientId, verbose, maximunReconnectTimes } = options + checkTopicExists(topic, 'benchPub') const connOpts = parseConnectOptions(options, 'pub') @@ -135,7 +137,7 @@ const benchPub = async (options: BenchPublishOptions) => { const { username } = connOpts - const { topic, message } = pubOpts + const { message } = pubOpts let connectedCount = 0 @@ -146,9 +148,7 @@ const benchPub = async (options: BenchPublishOptions) => { const interactive = new Signale({ interactive: true }) const simpleInteractive = new Signale({ interactive: true, config: { displayLabel: false, displayTimestamp: true } }) - signale.info( - `Start the publish benchmarking, connections: ${count}, req interval: ${interval}ms, message interval: ${messageInterval}ms`, - ) + benchLog.start.pub(config, count, interval, messageInterval, hostname, port, topic, message.toString()) const connStart = Date.now() diff --git a/cli/src/lib/sub.ts b/cli/src/lib/sub.ts index 3c7df85b5..ff5c547be 100644 --- a/cli/src/lib/sub.ts +++ b/cli/src/lib/sub.ts @@ -22,7 +22,7 @@ const sub = (options: SubscribeOptions) => { let retryTimes = 0 - basicLog.connecting() + basicLog.connecting(config, connOpts.hostname!, connOpts.port, options.topic.join(', ')) client.on('connect', () => { basicLog.connected() @@ -101,9 +101,9 @@ const benchSub = async (options: BenchSubscribeOptions) => { save && saveConfig('benchSub', options) - checkTopicExists(options.topic, 'benchSub') + const { count, interval, topic, hostname, port, clientId, verbose, maximunReconnectTimes } = options - const { count, interval, topic, clientId, verbose, maximunReconnectTimes } = options + checkTopicExists(topic, 'benchSub') const connOpts = parseConnectOptions(options, 'sub') @@ -118,9 +118,7 @@ const benchSub = async (options: BenchSubscribeOptions) => { const interactive = new Signale({ interactive: true }) const simpleInteractive = new Signale({ interactive: true, config: { displayLabel: false, displayTimestamp: true } }) - signale.info( - `Start the subscribe benchmarking, connections: ${count}, req interval: ${interval}ms, topic: ${topic.join(',')}`, - ) + benchLog.start.sub(config, count, interval, hostname, port, topic.join(', ')) const connStart = Date.now() @@ -170,7 +168,7 @@ const benchSub = async (options: BenchSubscribeOptions) => { } }) - if (i === count) { + if (i === count && topic[topic.length - 1] === t) { const connEnd = Date.now() signale.info(`Created ${count} connections in ${(connEnd - connStart) / 1000}s`) @@ -179,10 +177,8 @@ const benchSub = async (options: BenchSubscribeOptions) => { if (!verbose) { setInterval(() => { - if (total > oldTotal) { - const rate = total - oldTotal - simpleInteractive.info(`Received total: ${total}, rate: ${rate}/s`) - } + const rate = total - oldTotal + simpleInteractive.info(`Received total: ${total}, rate: ${rate}/s`) oldTotal = total }, 1000) } else { diff --git a/cli/src/utils/signale.ts b/cli/src/utils/signale.ts index 329ff7ddf..77a7e36c8 100644 --- a/cli/src/utils/signale.ts +++ b/cli/src/utils/signale.ts @@ -24,7 +24,17 @@ const msgLog = (msg: Record[]) => { } const basicLog = { - connecting: () => signale.await('Connecting...'), + connecting: (config: boolean | string | undefined, host: string, port = 1883, topic?: string, message?: string) => { + if (!config) { + signale.await('Connecting...') + } else { + signale.await( + `Connecting using configuration file, host: ${host}, port: ${port}${topic ? `, topic: ${topic}` : ''}${ + message ? `, message: ${message}` : '' + }`, + ) + } + }, connected: () => signale.success('Connected'), subscribing: (t: string) => signale.await(`Subscribing to ${t}...`), subscribed: (t: string) => signale.success(`Subscribed to ${t}`), @@ -40,6 +50,55 @@ const basicLog = { } const benchLog = { + start: { + conn: (config: boolean | string | undefined, count: number, interval: number, host: string, port = 1883) => { + if (!config) { + signale.info(`Start the connect benchmarking, connections: ${count}, req interval: ${interval}ms`) + } else { + signale.info( + `Start the connect benchmarking, connections: ${count}, req interval: ${interval}ms, host: ${host}, port: ${port}`, + ) + } + }, + sub: ( + config: boolean | string | undefined, + count: number, + interval: number, + host: string, + port = 1883, + topic: string, + ) => { + if (!config) { + signale.info( + `Start the subscribe benchmarking, connections: ${count}, req interval: ${interval}ms, topic: ${topic}`, + ) + } else { + signale.info( + `Start the subscribe benchmarking, connections: ${count}, req interval: ${interval}ms, host: ${host}, port: ${port}, topic: ${topic}`, + ) + } + }, + pub: ( + config: boolean | string | undefined, + count: number, + interval: number, + messageInterval: number, + host: string, + port = 1883, + topic: string, + message: string, + ) => { + if (!config) { + signale.info( + `Start the publish benchmarking, connections: ${count}, req interval: ${interval}ms, message interval: ${messageInterval}ms`, + ) + } else { + signale.info( + `Start the publish benchmarking, connections: ${count}, req interval: ${interval}ms, message interval: ${messageInterval}ms, host: ${host}, port: ${port}, topic: ${topic}, message: ${message}`, + ) + } + }, + }, error: (count: number, total: number, id: string, err: Error) => { signale.error(`[${count}/${total}] - Client ID: ${id}, ${err}`) },