Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): improve start log #1154

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli/src/lib/conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')

Expand All @@ -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()

Expand Down
22 changes: 11 additions & 11 deletions cli/src/lib/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,14 +36,15 @@ const send = (
}

const multisend = (
config: boolean | string | undefined,
connOpts: IClientOptions,
pubOpts: { topic: string; message: string | Buffer; opts: IClientPublishOptions },
maximunReconnectTimes: number,
) => {
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,
})
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -125,17 +127,17 @@ 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')

const pubOpts = parsePublishOptions(options)

const { username } = connOpts

const { topic, message } = pubOpts
const { message } = pubOpts

let connectedCount = 0

Expand All @@ -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()

Expand Down
18 changes: 7 additions & 11 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')

Expand All @@ -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()

Expand Down Expand Up @@ -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`)
Expand All @@ -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 {
Expand Down
61 changes: 60 additions & 1 deletion cli/src/utils/signale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ const msgLog = (msg: Record<string, unknown>[]) => {
}

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}`),
Expand All @@ -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}`)
},
Expand Down