Skip to content

HabitaNexus/monorepo / skills/system-reliability-engineering/load-testing-performance

load-testing-performance

Unverified

scripts/nodejs/memory-profiler.js

View on GitHub
/**
 * Node.js Memory Profiler
 *
 * Memory profiling and monitoring for Node.js applications.
 *
 * Usage:
 *   node memory-profiler.js
 *   node memory-profiler.js --snapshot
 *   node memory-profiler.js --monitor
 */

const heapdump = require('heapdump');
const path = require('path');

class MemoryProfiler {
  constructor() {
    this.monitoringInterval = null;
  }

  takeSnapshot(filename = null) {
    const timestamp = Date.now();
    const defaultFilename = `heap-${timestamp}.heapsnapshot`;
    const filepath = filename || path.join(process.cwd(), defaultFilename);

    heapdump.writeSnapshot(filepath, (err, filename) => {
      if (err) {
        console.error('Error taking heap snapshot:', err);
      } else {
        console.log(`Heap snapshot saved to ${filename}`);
      }
    });
  }

  monitorMemory(intervalMs = 5000) {
    if (this.monitoringInterval) {
      console.log('Memory monitoring already active');
      return;
    }

    console.log(`Starting memory monitoring (interval: ${intervalMs}ms)`);

    this.monitoringInterval = setInterval(() => {
      const usage = process.memoryUsage();
      console.log({
        timestamp: new Date().toISOString(),
        rss: `${Math.round(usage.rss / 1024 / 1024)} MB`,
        heapTotal: `${Math.round(usage.heapTotal / 1024 / 1024)} MB`,
        heapUsed: `${Math.round(usage.heapUsed / 1024 / 1024)} MB`,
        external: `${Math.round(usage.external / 1024 / 1024)} MB`,
        arrayBuffers: `${Math.round(usage.arrayBuffers / 1024 / 1024)} MB`,
      });
    }, intervalMs);
  }

  stopMonitoring() {
    if (this.monitoringInterval) {
      clearInterval(this.monitoringInterval);
      this.monitoringInterval = null;
      console.log('Memory monitoring stopped');
    }
  }

  getMemoryUsage() {
    const usage = process.memoryUsage();
    return {
      rss: Math.round(usage.rss / 1024 / 1024),
      heapTotal: Math.round(usage.heapTotal / 1024 / 1024),
      heapUsed: Math.round(usage.heapUsed / 1024 / 1024),
      external: Math.round(usage.external / 1024 / 1024),
      arrayBuffers: Math.round(usage.arrayBuffers / 1024 / 1024),
    };
  }
}

// CLI usage
if (require.main === module) {
  const args = process.argv.slice(2);
  const profiler = new MemoryProfiler();

  if (args.includes('--snapshot')) {
    const filename = args.find(arg => arg.startsWith('--filename'))?.split('=')[1];
    profiler.takeSnapshot(filename);
  } else if (args.includes('--monitor')) {
    const interval = parseInt(args.find(arg => arg.startsWith('--interval'))?.split('=')[1] || '5000');
    profiler.monitorMemory(interval);

    // Keep process alive
    process.on('SIGINT', () => {
      profiler.stopMonitoring();
      process.exit(0);
    });
  } else {
    // Default: show current memory usage
    const usage = profiler.getMemoryUsage();
    console.log('Current memory usage:', usage);
  }
}

module.exports = MemoryProfiler;
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall