Skip to content

Commit

Permalink
Bitrate statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
opengs committed Sep 13, 2023
1 parent f008050 commit 14c6020
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 69 deletions.
27 changes: 27 additions & 0 deletions lib/module/db1000n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class DB1000N extends Module<Config> {
const handler = await this.startExecutable(executableName, args)

// Process statistics
let lastStatisticsEvent = null as Date | null
let statisticsBuffer = ''
handler.stderr.on('data', (data: Buffer) => {
statisticsBuffer += data.toString()
Expand All @@ -82,6 +83,32 @@ export class DB1000N extends Module<Config> {
for (const line of lines) {
try {
const lineJSON = JSON.parse(line)
if (lineJSON["msg"] !== "stats") {
continue
}

const bytesSend = lineJSON["total"]["bytes_sent"]
if (bytesSend === undefined) {
continue
}
let currentSendBitrate = 0
if (lastStatisticsEvent == null) {
lastStatisticsEvent = new Date()
} else {
const now = new Date()
const timeDiff = (now.getTime() - lastStatisticsEvent.getTime()) / 1000.0
if (timeDiff > 0) {
currentSendBitrate = bytesSend * 1.0 / timeDiff
}
lastStatisticsEvent = now
}

this.emit('execution:statistics', {
type: 'execution:statistics',
bytesSend: Number(bytesSend),
currentSendBitrate,
timestamp: new Date().getTime()
})
//TODO: extract statistics
} catch (e) {
console.error(String(e) + '\n' + line)
Expand Down
16 changes: 11 additions & 5 deletions lib/module/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChildProcessWithoutNullStreams, spawn } from 'child_process'
import { EventEmitter } from 'stream'
import internal, { EventEmitter } from 'stream'
import path from 'path'
import { app } from 'electron'
import fs from 'fs'
Expand Down Expand Up @@ -56,7 +56,12 @@ export interface BaseConfig {
export type ModuleExecutionEvent = 'execution:statistics' | 'execution:stdout' | 'execution:stderr' | 'execution:error' | 'execution:started' | 'execution:stopped'
export interface ModuleExecutionStatisticsEventData {
type: 'execution:statistics';
reqeustsMade: number;
// Total number of bytes currently sending per second
currentSendBitrate: number
// Number of bytes sent since last event
bytesSend: number
// When the statistics were collected
timestamp: number
}
export interface ModuleExecutionStdoutEventData {
type: 'execution:stdout';
Expand Down Expand Up @@ -222,16 +227,17 @@ export abstract class Module<ConfigType extends BaseConfig> {
if (response.body == null) {
throw new Error('Response body is null')
}
response.body.pipe(fileStream)
const body = response.body as internal.Readable
body.pipe(fileStream)

const e = new EventEmitter()

response.body.on('data', (chunk) => {
body.on('data', (chunk) => {
downloadedBytes += chunk.length
e.emit('progress', downloadedBytes / contentLength * 100)
})

response.body.on('error', (err: any) => {
body.on('error', (err: any) => {
e.emit('err', err)
})

Expand Down
20 changes: 14 additions & 6 deletions src-electron/electron-preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import { Config as DistressConfig } from 'app/lib/module/distress'
import { Config as MHDDOSProxyConfig } from 'app/lib/module/mhddosproxy'
import { Config as DB1000NConfig } from 'app/lib/module/db1000n'
import { InstallProgress, ModuleName, Version } from 'app/lib/module/module'
import { InstallProgress, ModuleExecutionStatisticsEventData, ModuleName, Version } from 'app/lib/module/module'
import { IpcRendererEvent, contextBridge, ipcRenderer } from 'electron'

declare global {
Expand Down Expand Up @@ -70,7 +70,7 @@ const modulesAPI = {
}
contextBridge.exposeInMainWorld('modulesAPI', modulesAPI)

import { Config as ExecutionEngineConfig, ExecutionLogEntry } from './handlers/engine'
import { State as ExecutionEngineState, ExecutionLogEntry } from './handlers/engine'

declare global {
interface Window {
Expand All @@ -85,11 +85,11 @@ const executionEngineAPI = {
async stopModule (): Promise<void> {
return await ipcRenderer.invoke('executionEngine:stopModule')
},
async getConfig (): Promise<ExecutionEngineConfig> {
return await ipcRenderer.invoke('executionEngine:getConfig')
async getState (): Promise<ExecutionEngineState> {
return await ipcRenderer.invoke('executionEngine:getState')
},
async setConfig (config: ExecutionEngineConfig): Promise<void> {
return await ipcRenderer.invoke('executionEngine:setConfig', config)
async setModuleToRun (module?: ModuleName): Promise<void> {
return await ipcRenderer.invoke('executionEngine:setModuleToRun', module)
},
async listenForExecutionLog (callback: (_e: IpcRendererEvent, data: ExecutionLogEntry) => void): Promise<void> {
await ipcRenderer.invoke('executionEngine:listenForExecutionLog')
Expand All @@ -114,6 +114,14 @@ const executionEngineAPI = {
async stopListeningForStdErr (callback: (_e: IpcRendererEvent, data: string) => void): Promise<void> {
await ipcRenderer.invoke('executionEngine:stopListeningForStdErr')
ipcRenderer.off('executionEngine:stderr', callback)
},
async listenForStatistics (callback: (_e: IpcRendererEvent, data: ModuleExecutionStatisticsEventData) => void): Promise<void> {
await ipcRenderer.invoke('executionEngine:listenForStatistics')
ipcRenderer.on('executionEngine:statistics', callback)
},
async stopListeningForStatistics (callback: (_e: IpcRendererEvent, data: ModuleExecutionStatisticsEventData) => void): Promise<void> {
await ipcRenderer.invoke('executionEngine:stopListeningForStatistics')
ipcRenderer.off('executionEngine:statistics', callback)
}
}

Expand Down
Loading

0 comments on commit 14c6020

Please sign in to comment.