Records performance metrics for a single HTTP request.
Tracks response time, error status, and identifies slow requests based on the configured threshold (1000ms). Automatically logs warnings for slow requests and maintains a rolling window of recent request times for memory efficiency.
Request response time in milliseconds
Optional
isError: boolean = falseWhether the request resulted in an error (HTTP status >= 400)
Retrieves comprehensive metrics about tracked HTTP requests.
Calculates and returns aggregated performance statistics including total request count, average response time, error rate, slow request count, and peak/fastest response times. All calculations are based on the current rolling window of tracked requests.
Object containing comprehensive request performance metrics
const metrics = performanceMonitor.getRequestMetrics();
console.log(`Processed ${metrics.totalRequests} requests`);
console.log(`Average response time: ${metrics.averageResponseTime}ms`);
console.log(`Error rate: ${metrics.errorRate}%`);
console.log(`Slow requests: ${metrics.slowRequests}`);
console.log(`Peak response time: ${metrics.peakResponseTime}ms`);
Retrieves current system resource metrics from the Node.js process.
Collects real-time information about system uptime, memory usage, CPU usage, and active connections. These metrics are essential for monitoring system health and identifying resource bottlenecks in production environments.
Object containing current system resource metrics
Analyzes performance metrics and generates actionable optimization recommendations.
Evaluates request performance, system resources, cache efficiency, and database health to provide specific, actionable recommendations for improving application performance. Recommendations are prioritized based on impact and include specific thresholds and suggested solutions.
Complete performance report containing all system metrics
Array of actionable performance optimization recommendations
Generates a comprehensive performance report including all system components.
Aggregates metrics from request tracking, system resources, database health, and cache performance into a unified report. Includes intelligent recommendations based on current performance patterns and thresholds. This method is used by health check endpoints and monitoring dashboards.
Complete performance report with recommendations
const report = await performanceMonitor.generateReport();
console.log('System Status:', report.system.uptime);
console.log('Request Metrics:', report.requests);
console.log('Database Status:', report.database.status);
console.log('Cache Performance:', report.cache);
console.log('Recommendations:', report.recommendations);
// Use in health check endpoint
app.get('/metrics', async (req, res) => {
const report = await performanceMonitor.generateReport();
res.json(report);
});
Resets all tracked performance metrics to initial state.
Clears request times, counters, and error tracking. Useful for testing scenarios, periodic metric resets, or when starting fresh monitoring periods. This operation is logged for audit purposes.
Core performance monitoring class that tracks HTTP request metrics and generates system reports.
This class maintains in-memory statistics about request performance, including response times, error rates, and slow request detection. It provides comprehensive reporting capabilities with actionable recommendations for performance optimization. The monitor automatically manages memory usage by maintaining a rolling window of the most recent request times.
PerformanceMonitor
Example