@platformatic/flame
🔥 CPU and heap profiling and flamegraph visualization tool built on top of @platformatic/react-pprof.
Features
- Dual Profiling: Captures both CPU and heap profiles concurrently for comprehensive performance insights
- Auto-Start Profiling: Profiling starts immediately when using
flame run (default behavior)
- Automatic Flamegraph Generation: Interactive HTML flamegraphs are created automatically for both CPU and heap profiles on exit
- LLM-Friendly Markdown Analysis: Generates markdown reports with hotspot analysis, ideal for AI-assisted performance debugging
- Sourcemap Support: Automatically translates transpiled code locations back to original source files (TypeScript, bundled JavaScript, etc.)
- Clear File Path Display: Shows exact paths and browser URLs for generated files
- Manual Control: Optional manual mode with signal-based control using
SIGUSR2
- Interactive Visualization: WebGL-based HTML flamegraphs with zoom, search, and filtering
- CLI Interface: Simple command-line tool for profiling and visualization
- Zero Config: Works out of the box with sensible defaults
Installation
npm install -g @platformatic/flame
Quick Start
Profile a Node.js Script (Auto-Start Mode)
flame run server.js
Manual Profiling Mode
flame run --manual server.js
kill -USR2 <PID>
flame toggle
Generate Flamegraph and Markdown
flame generate cpu-profile-2024-01-01T12-00-00-000Z.pb
flame generate -o my-flamegraph.html profile.pb.gz
flame generate --md-format=detailed profile.pb
CLI Usage
flame [options] <command>
Commands:
run <script> Run a script with profiling enabled
generate <pprof-file> Generate HTML flamegraph and markdown from pprof file
toggle Toggle profiling for running flame processes
Options:
-o, --output <file> Output HTML file (for generate command)
-m, --manual Manual profiling mode (require SIGUSR2 to start)
-d, --delay <value> Delay before starting profiler (ms, 'none', or 'until-started')
-s, --sourcemap-dirs <dirs> Directories to search for sourcemaps (colon/semicolon-separated)
--md-format <format> Markdown format: summary (default), detailed, or adaptive
--node-options <options> Node.js CLI options to pass to the profiled process
-h, --help Show help message
-v, --version Show version number
Programmatic API
const { startProfiling, generateFlamegraph, generateMarkdown, parseProfile } = require('@platformatic/flame')
const { pid, toggleProfiler } = startProfiling('server.js', ['--port', '3000'], { autoStart: true })
const { pid, toggleProfiler } = startProfiling('server.js', ['--port', '3000'], { autoStart: false })
console.log(`Started profiling process ${pid}`)
toggleProfiler()
await generateFlamegraph('profile.pb.gz', 'flamegraph.html')
await generateMarkdown('profile.pb', 'analysis.md', { format: 'summary' })
const profile = await parseProfile('profile.pb')
How It Works
- Auto-Start Mode (Default): Both CPU and heap profiling begin immediately when
flame run starts your script
- Auto-Generation on Exit: Profile (.pb) files, interactive HTML flamegraphs, and markdown analysis files are automatically created for both CPU and heap profiles when the process exits
- Manual Mode: Use
--manual flag to require SIGUSR2 signals for start/stop control (no auto-HTML generation)
- Interactive Visualization: The
@platformatic/react-pprof library generates interactive WebGL-based HTML flamegraphs for both profile types
- Markdown Analysis: The
pprof-to-md library generates LLM-friendly markdown reports with hotspot tables for AI-assisted debugging
Sourcemap Support
When profiling transpiled or bundled applications (TypeScript, Webpack, ESBuild, etc.), the flame tool can automatically translate stack traces from generated code back to your original source files using sourcemaps.
Usage
flame run --sourcemap-dirs=dist server.js
flame run --sourcemap-dirs=dist:build:out server.js
How It Works
- The tool searches specified directories for
.map, .js.map, .cjs.map, and .mjs.map files
- During profiling, stack frame locations are automatically translated from generated code to source locations
- The resulting flamegraph shows your original source file paths and line numbers
Programmatic API
const { startProfiling } = require('@platformatic/flame')
startProfiling('dist/server.js', [], {
autoStart: true,
sourcemapDirs: ['dist', 'build']
})
Environment Variables
Sourcemap support can also be controlled via environment variables:
export FLAME_SOURCEMAP_DIRS="dist:build"
flame run server.js
Notes
- Sourcemaps are loaded at startup and applied during profile capture
- The tool excludes
node_modules directories when searching
- If a sourcemap cannot be found or fails to parse, the original generated location is preserved
- Both CPU and heap profiles benefit from sourcemap translation
Profile Files
Profile files are saved with timestamps in the format:
cpu-profile-2024-01-01T12-00-00-000Z.pb # Binary pprof data
cpu-profile-2024-01-01T12-00-00-000Z.html # Interactive flamegraph
cpu-profile-2024-01-01T12-00-00-000Z.md # LLM-friendly markdown analysis
heap-profile-2024-01-01T12-00-00-000Z.pb
heap-profile-2024-01-01T12-00-00-000Z.html
heap-profile-2024-01-01T12-00-00-000Z.md
Both CPU and heap profiles share the same timestamp for easy correlation. The .pb files are compressed Protocol Buffer format compatible with the pprof ecosystem. The .md files contain hotspot analysis tables suitable for AI/LLM-assisted performance debugging.
Markdown Formats
The --md-format option controls the markdown output:
- summary (default): Compact hotspots table, ideal for AI triage and quick overview
- detailed: Comprehensive analysis with full stack traces and detailed statistics
- adaptive: Automatically chooses format based on profile complexity
Integration with Existing Apps
Express.js Example
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.json({ message: 'Hello World' })
})
app.listen(3000)
Auto-Start Mode (Recommended):
flame run server.js
curl http://localhost:3000
curl http://localhost:3000
curl http://localhost:3000
Manual Mode:
flame run --manual server.js
flame toggle
curl http://localhost:3000
curl http://localhost:3000
flame toggle
flame generate cpu-profile-*.pb
Example Applications
Two comprehensive benchmark applications are included for testing profiling overhead and feature Express.js middlewares and Fastify plugins with realistic computation endpoints.
Running Benchmark Apps
node examples/express-benchmark-app.js
node examples/fastify-benchmark-app.js
node examples/run-performance-benchmark.js
Both apps include:
- Health check endpoint (no computation)
- Light computation endpoint (~1k iterations)
- Medium computation endpoint (~10k iterations)
- Heavy computation endpoint (~100k iterations)
- Multiple middleware layers for realistic overhead testing
- Comprehensive test suites
Performance Overhead Results
Based on comprehensive benchmarks with 10 concurrent connections over 10 seconds per endpoint:
Express.js Framework:
| Health Check | Minimal | 13,571 req/s | 13,752 req/s | -1.3% | -6.3% |
| Light Computation | Low | 10,187 req/s | 9,979 req/s | +2.0% | +12.0% |
| Medium Computation | Moderate | 71 req/s | 66 req/s | +6.1% | +6.3% |
| Heavy Computation | High | 295 req/s | 291 req/s | +1.3% | +1.4% |
| Mixed Computation | Very High | 56 req/s | 53 req/s | +5.2% | +5.8% |
Express Summary: Average throughput overhead of 2.7% and latency overhead of 3.9%
Fastify Framework:
| Health Check | Minimal | 41,174 req/s | 38,747 req/s | +5.9% | 0.0% |
| Light Computation | Low | 35,056 req/s | 32,847 req/s | +6.3% | 0.0% |
| Medium Computation | Moderate | 3,235 req/s | 3,126 req/s | +3.4% | +4.2% |
| Heavy Computation | High | 345 req/s | 336 req/s | +2.6% | +2.6% |
| Mixed Computation | Very High | 311 req/s | 304 req/s | +2.3% | +2.3% |
Fastify Summary: Average throughput overhead of 4.1% and latency overhead of 1.8%
CSV data is automatically generated when running node examples/run-performance-benchmark.js for further analysis.
Load Testing Example
You can run load tests manually:
flame run examples/load-test-server.js
npx autocannon -c 10 -d 10 http://localhost:3000
kill -USR2 <PID>
Requirements
- Node.js >= 22.6.0
@datadog/pprof for CPU and heap profiling
react-pprof for flamegraph generation
pprof-to-md for markdown analysis generation
License
MIT