Skip to content

Commit

Permalink
Added distress statistics parser and visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
opengs committed Sep 24, 2023
1 parent eae61bd commit 0c556cc
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions lib/module/distress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,72 @@ export class Distress extends Module<Config> {
}

const handler = await this.startExecutable(filename, args)

// Process statistics
let lastStatisticsEvent = null as Date | null
let statisticsBuffer = ''
handler.stdout.on('data', (data: Buffer) => {
statisticsBuffer += data.toString()

const lines = statisticsBuffer.trimEnd().split('\n')
if (statisticsBuffer.endsWith('\n')) {
statisticsBuffer = ''
} else {
statisticsBuffer = lines.pop() as string
}

for (const line of lines) {
try {
const lineJSON = JSON.parse(line)
const msg = lineJSON["msg"] as string

if (!msg.includes("active connections") || !msg.includes("bps") || !msg.includes("bytes")) {
continue
}

let bytesSend = 0
let currentSendBitrate = 0

const convertToBytes = (value: string): number => {
value = value.toLowerCase()

if (value.includes("kb")) {
return Number(value.split("kb")[0]) * 1024
} else if (value.includes("mb")) {
return Number(value.split("mb")[0]) * 1024 * 1024
} else if (value.includes("gb")) {
return Number(value.split("gb")[0]) * 1024 * 1024 * 1024
} else if (value.includes("tb")) {
return Number(value.split("tb")[0]) * 1024 * 1024 * 1024 * 1024
} else if (value.includes("pb")) {
return Number(value.split("pb")[0]) * 1024 * 1024 * 1024 * 1024 * 1024
} else if (value.includes("eb")) {
return Number(value.split("eb")[0]) * 1024 * 1024 * 1024 * 1024 * 1024 * 1024
} else {
return Number(value.split("b")[0])
}
}

const parameters = msg.split(",").map((parameter) => parameter.trim())
for (const parameter of parameters) {
if (parameter.includes("bytes")) {
bytesSend = convertToBytes(parameter.split("=")[1])
} else if (parameter.includes("bps")) {
currentSendBitrate = convertToBytes(parameter.split("=")[1])
}
}

this.emit('execution:statistics', {
type: 'execution:statistics',
bytesSend: Number(bytesSend),
currentSendBitrate,
timestamp: new Date().getTime()
})
} catch (e) {
console.error(String(e) + '\n' + line)
}
}
})
}
override async stop (): Promise<void> {
this.stopExecutable()
Expand Down

0 comments on commit 0c556cc

Please sign in to comment.