diff --git a/src/profiler/memoryReporting.ts b/src/profiler/memoryReporting.ts index 3429809a..c01dd0e2 100644 --- a/src/profiler/memoryReporting.ts +++ b/src/profiler/memoryReporting.ts @@ -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 interface CounterNode { @@ -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`) @@ -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 { diff --git a/src/profiler/nestedCounters.ts b/src/profiler/nestedCounters.ts index 7f86c203..f5d09d6d 100644 --- a/src/profiler/nestedCounters.ts +++ b/src/profiler/nestedCounters.ts @@ -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 interface CounterNode { @@ -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 { diff --git a/src/profiler/profiler.ts b/src/profiler/profiler.ts index e74fe1bd..7f226560 100644 --- a/src/profiler/profiler.ts +++ b/src/profiler/profiler.ts @@ -1,3 +1,4 @@ +import { isDebugMiddleware } from '../DebugMode' import { nestedCountersInstance } from './nestedCounters' import * as fastify from 'fastify' @@ -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 {