|
| 1 | +const { spawn } = require('child_process'); |
| 2 | +import * as fs from 'fs'; |
| 3 | + |
| 4 | +const child = spawn('npm', ['run', 'start'], { |
| 5 | + stdio: 'inherit', |
| 6 | + shell: true |
| 7 | +}); |
| 8 | + |
| 9 | + |
| 10 | +function getTimestamp() { |
| 11 | + const now = new Date(); |
| 12 | + return `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}-${now.getHours().toString().padStart(2, '0')}-${now.getMinutes().toString().padStart(2, '0')}-${now.getSeconds().toString().padStart(2, '0')}`; |
| 13 | +} |
| 14 | + |
| 15 | +interface MemoryUsage { |
| 16 | + rss: number; |
| 17 | + heapTotal: number; |
| 18 | + heapUsed: number; |
| 19 | + external: number; |
| 20 | +} |
| 21 | + |
| 22 | +function logCPUMemoryUsage(logFile: string) { |
| 23 | + const cpuUsage = process.cpuUsage(); |
| 24 | + const memoryUsage: MemoryUsage = process.memoryUsage(); |
| 25 | + const timestamp = Date.now(); |
| 26 | + const logLine = `${timestamp},${cpuUsage.user},${cpuUsage.system},${memoryUsage.rss},${memoryUsage.heapTotal},${memoryUsage.heapUsed},${memoryUsage.external}\n`; |
| 27 | + fs.appendFileSync(logFile, logLine); |
| 28 | +} |
| 29 | + |
| 30 | +const timestamp = getTimestamp(); |
| 31 | +const logFile = `notifications-stream-aggregator-resource-usage-${timestamp}.csv`; |
| 32 | +fs.writeFileSync(logFile, 'timestamp,cpu_user,cpu_system,memory_rss,memory_heapTotal,memory_heapUsed,memory_external\n'); |
| 33 | + |
| 34 | +setInterval(() => { |
| 35 | + logCPUMemoryUsage(logFile); |
| 36 | +}, 500); |
| 37 | + |
| 38 | +child.on('exit', (code: any) => { |
| 39 | + console.log(`Child process exited with code ${code}`); |
| 40 | + process.exit(code); |
| 41 | +}); |
| 42 | + |
| 43 | +child.on('error', (err: any) => { |
| 44 | + console.log(`Child process error: ${err}`); |
| 45 | + process.exit(1); |
| 46 | +}); |
0 commit comments