Skip to content

Commit

Permalink
fix(cli): code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ysfscream authored and Red-Asuka committed May 9, 2024
1 parent 98b3c1d commit afb2992
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
23 changes: 7 additions & 16 deletions cli/src/lib/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import delay from '../utils/delay'
import { saveConfig, loadConfig } from '../utils/config'
import { loadSimulator } from '../utils/simulate'
import { serializeProtobufToBuffer } from '../utils/protobuf'
import { readFile, processPath, fileDataSplitter } from '../utils/fileUtils'
import { readFile, processPath, fileDataSplitter, getPublishMessageFromFile } from '../utils/fileUtils'
import convertPayload from '../utils/convertPayload'
import * as Debug from 'debug'
import _ from 'lodash'
Expand Down Expand Up @@ -288,10 +288,8 @@ const multiPub = async (commandType: CommandType, options: BenchPublishOptions |

for (let i = 1; i <= count; i++) {
// Duplicate splited messages array for each connection
let dupSplitedMessageArr: string[] = []
if (split && fileRead && splitedMessageArr.length !== 0) {
dupSplitedMessageArr = _.cloneDeep(splitedMessageArr)
}
const dupSplitedMessageArr: string[] =
split && fileRead && splitedMessageArr.length !== 0 ? _.cloneDeep(splitedMessageArr) : []

;((i: number, connOpts: mqtt.IClientOptions) => {
const opts = { ...connOpts }
Expand Down Expand Up @@ -336,17 +334,10 @@ const multiPub = async (commandType: CommandType, options: BenchPublishOptions |
publishMessage = simulationResult.message
}
if (fileRead) {
if (split) {
if (dupSplitedMessageArr.length === 0) {
await delay(1000)
signale.success(`All ${total} messages from the ${fileRead} have been successfully sent.`)
process.exit(0)
}
const unshiftedMessage = dupSplitedMessageArr.shift()
publishMessage = Buffer.from(unshiftedMessage!)
} else {
publishMessage = fileData
}
publishMessage = await getPublishMessageFromFile(split, dupSplitedMessageArr, fileData, {
total,
fileRead,
})
}
client.publish(publishTopic, publishMessage, pubOpts.opts, (err) => {
inFlightMessageCount -= 1
Expand Down
21 changes: 21 additions & 0 deletions cli/src/utils/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'
import YAML from 'js-yaml'
import signale from 'signale'
import delay from './delay'

const processPath = (savePath: boolean | string, defaultPath?: string) => {
let filePath = ''
Expand Down Expand Up @@ -97,6 +98,25 @@ const fileDataSplitter = (data: string | Buffer, split: string): string[] => {
return stringData.split(splitRegex)
}

const getPublishMessageFromFile = async (
split: string,
dupSplitedMessageArr: string[],
fileData: string | Buffer,
meta: { total: number; fileRead: string },
): Promise<Buffer | string> => {
if (!split) {
return fileData
}
if (dupSplitedMessageArr.length === 0) {
await delay(1000)
signale.success(`All ${meta.total} messages from the ${meta.fileRead} have been successfully sent.`)
process.exit(0)
}

const unshiftedMessage = dupSplitedMessageArr.shift()
return Buffer.from(unshiftedMessage!)
}

export {
processPath,
getPathExtname,
Expand All @@ -109,4 +129,5 @@ export {
appendFile,
createNextNumberedFileName,
fileDataSplitter,
getPublishMessageFromFile,
}

0 comments on commit afb2992

Please sign in to comment.