Skip to content

Commit

Permalink
fix: add debug endpoints behind debug middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
akirapham authored and jairajdev committed May 2, 2024
1 parent de4fed0 commit 199f869
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 76 deletions.
117 changes: 71 additions & 46 deletions src/profiler/memoryReporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { resourceUsage } from 'process'
import { getActiveNodeCount } from '../NodeList'
import { spawn } from 'child_process'
import * as process from 'process'
import { isDebugMiddleware } from '../DebugMode'

type CounterMap = Map<string, CounterNode>
interface CounterNode {
Expand Down Expand Up @@ -38,21 +39,29 @@ class MemoryReporting {
}

registerEndpoints(): void {
this.server.get('/memory', (req, res) => {
const toMB = 1 / 1000000
const report = process.memoryUsage()
let outputStr = ''
outputStr += `System Memory Report. Timestamp: ${Date.now()}\n`
outputStr += `rss: ${(report.rss * toMB).toFixed(2)} MB\n`
outputStr += `heapTotal: ${(report.heapTotal * toMB).toFixed(2)} MB\n`
outputStr += `heapUsed: ${(report.heapUsed * toMB).toFixed(2)} MB\n`
outputStr += `external: ${(report.external * toMB).toFixed(2)} MB\n`
outputStr += `arrayBuffers: ${(report.arrayBuffers * toMB).toFixed(2)} MB\n\n\n`

this.gatherReport()
outputStr = this.reportToStream(this.report, outputStr)
res.send(outputStr)
})
this.server.get(
'/memory',
{
preHandler: async (_request, reply) => {
isDebugMiddleware(_request, reply)
},
},
(req, res) => {
const toMB = 1 / 1000000
const report = process.memoryUsage()
let outputStr = ''
outputStr += `System Memory Report. Timestamp: ${Date.now()}\n`
outputStr += `rss: ${(report.rss * toMB).toFixed(2)} MB\n`
outputStr += `heapTotal: ${(report.heapTotal * toMB).toFixed(2)} MB\n`
outputStr += `heapUsed: ${(report.heapUsed * toMB).toFixed(2)} MB\n`
outputStr += `external: ${(report.external * toMB).toFixed(2)} MB\n`
outputStr += `arrayBuffers: ${(report.arrayBuffers * toMB).toFixed(2)} MB\n\n\n`

this.gatherReport()
outputStr = this.reportToStream(this.report, outputStr)
res.send(outputStr)
}
)

// this.server.get('memory-gc', (req, res) => {
// res.write(`System Memory Report. Timestamp: ${Date.now()}\n`)
Expand All @@ -69,37 +78,53 @@ class MemoryReporting {
// res.end()
// })

this.server.get('/top', (req, res) => {
const top = spawn('top', ['-n', '10'])
top.stdout.on('data', (dataBuffer) => {
res.send(dataBuffer.toString())
top.kill()
})
top.on('close', (code) => {
console.log(`child process exited with code ${code}`)
})
top.stderr.on('data', (data) => {
console.log('top command error', data)
res.send('top command error')
top.kill()
})
})

this.server.get('/df', (req, res) => {
const df = spawn('df')
df.stdout.on('data', (dataBuffer) => {
res.send(dataBuffer.toString())
df.kill()
})
df.on('close', (code) => {
console.log(`child process exited with code ${code}`)
})
df.stderr.on('data', (data) => {
console.log('df command error', data)
res.send('df command error')
df.kill()
})
})
this.server.get(
'/top',
{
preHandler: async (_request, reply) => {
isDebugMiddleware(_request, reply)
},
},
(req, res) => {
const top = spawn('top', ['-n', '10'])
top.stdout.on('data', (dataBuffer) => {
res.send(dataBuffer.toString())
top.kill()
})
top.on('close', (code) => {
console.log(`child process exited with code ${code}`)
})
top.stderr.on('data', (data) => {
console.log('top command error', data)
res.send('top command error')
top.kill()
})
}
)

this.server.get(
'/df',
{
preHandler: async (_request, reply) => {
isDebugMiddleware(_request, reply)
},
},
(req, res) => {
const df = spawn('df')
df.stdout.on('data', (dataBuffer) => {
res.send(dataBuffer.toString())
df.kill()
})
df.on('close', (code) => {
console.log(`child process exited with code ${code}`)
})
df.stderr.on('data', (data) => {
console.log('df command error', data)
res.send('df command error')
df.kill()
})
}
)
}

updateCpuPercent(): void {
Expand Down
85 changes: 59 additions & 26 deletions src/profiler/nestedCounters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fastify from 'fastify'
import { stringifyReduce } from './StringifyReduce'
import * as core from '@shardus/crypto-utils'
import { isDebugMiddleware } from '../DebugMode'

type CounterMap = Map<string, CounterNode>
interface CounterNode {
Expand Down Expand Up @@ -35,33 +36,65 @@ class NestedCounters {
}

registerEndpoints(): void {
this.server.get('/counts', (req, res) => {
let outputStr = ''
const arrayReport = this.arrayitizeAndSort(this.eventCounters)
outputStr += `${Date.now()}\n`
outputStr = this.printArrayReport(arrayReport, outputStr, 0)
res.send(outputStr)
})
this.server.get('/counts-reset', (req, res) => {
this.eventCounters = new Map()
res.send(`counts reset ${Date.now()}`)
})

this.server.get('/debug-inf-loop', (req, res) => {
res.send('starting inf loop, goodbye')
this.infLoopDebug = true
while (this.infLoopDebug) {
const s = 'asdf'
const s2 = stringifyReduce({ test: [s, s, s, s, s, s, s] })
const s3 = stringifyReduce({ test: [s2, s2, s2, s2, s2, s2, s2] })
core.hash(s3)
this.server.get(
'/counts',
{
preHandler: async (_request, reply) => {
isDebugMiddleware(_request, reply)
},
},
(req, res) => {
let outputStr = ''
const arrayReport = this.arrayitizeAndSort(this.eventCounters)
outputStr += `${Date.now()}\n`
outputStr = this.printArrayReport(arrayReport, outputStr, 0)
res.send(outputStr)
}
})

this.server.get('/debug-inf-loop-off', (req, res) => {
this.infLoopDebug = false
res.send('stopping inf loop, who knows if this is possible')
})
)
this.server.get(
'/counts-reset',
{
preHandler: async (_request, reply) => {
isDebugMiddleware(_request, reply)
},
},
(req, res) => {
this.eventCounters = new Map()
res.send(`counts reset ${Date.now()}`)
}
)

this.server.get(
'/debug-inf-loop',
{
preHandler: async (_request, reply) => {
isDebugMiddleware(_request, reply)
},
},
(req, res) => {
res.send('starting inf loop, goodbye')
this.infLoopDebug = true
while (this.infLoopDebug) {
const s = 'asdf'
const s2 = stringifyReduce({ test: [s, s, s, s, s, s, s] })
const s3 = stringifyReduce({ test: [s2, s2, s2, s2, s2, s2, s2] })
core.hash(s3)
}
}
)

this.server.get(
'/debug-inf-loop-off',
{
preHandler: async (_request, reply) => {
isDebugMiddleware(_request, reply)
},
},
(req, res) => {
this.infLoopDebug = false
res.send('stopping inf loop, who knows if this is possible')
}
)
}

countEvent(category1: string, category2: string, count = 1): void {
Expand Down
17 changes: 13 additions & 4 deletions src/profiler/profiler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDebugMiddleware } from '../DebugMode'
import { nestedCountersInstance } from './nestedCounters'
import * as fastify from 'fastify'

Expand Down Expand Up @@ -35,10 +36,18 @@ class Profiler {
}

registerEndpoints(): void {
this.server.get('/perf', (req, res) => {
const result = this.printAndClearReport()
res.send(result)
})
this.server.get(
'/perf',
{
preHandler: async (_request, reply) => {
isDebugMiddleware(_request, reply)
},
},
(req, res) => {
const result = this.printAndClearReport()
res.send(result)
}
)
}

profileSectionStart(sectionName: string, internal = false): void {
Expand Down

0 comments on commit 199f869

Please sign in to comment.