8.9 KiB
Performance Monitoring System
Overview
The Community Rule platform includes a comprehensive performance monitoring system designed to detect performance regressions, maintain performance budgets, and ensure optimal user experience across all components and user interactions.
Architecture
Core Components
-
Performance Monitor Module (
tests/performance/performance-monitor.js)- Base
PerformanceMonitorclass for metric collection and analysis WebPerformanceMonitorfor browser-based performance monitoringPlaywrightPerformanceMonitorfor E2E performance testing
- Base
-
Performance Tests (
tests/e2e/performance.spec.ts)- Comprehensive E2E performance tests using Playwright
- Core Web Vitals monitoring
- Component render performance testing
- Interaction performance testing
-
Lighthouse CI Integration (
lighthouserc.json)- Automated performance audits
- Performance budget enforcement
- Core Web Vitals validation
-
Performance Budgets (
performance-budgets.json)- Resource size limits
- Timing budgets
- Resource count limits
-
Monitoring Script (
scripts/performance-monitor.js)- Standalone performance monitoring
- Regression detection
- Report generation
Performance Budgets
Timing Budgets
| Metric | Budget | Baseline | Description |
|---|---|---|---|
| Page Load Time | 3000ms | 2000ms | Total page load time |
| First Contentful Paint | 2000ms | 1500ms | First content appears |
| Largest Contentful Paint | 2500ms | 2000ms | Largest content element |
| First Input Delay | 100ms | 50ms | First user interaction |
| TTFB | 600ms | 400ms | Time to First Byte |
| Component Render | 500ms | 300ms | Component rendering time |
| Interaction Time | 100ms | 50ms | User interaction response |
| Scroll Performance | 50ms | 30ms | Scroll operation time |
Resource Budgets
| Resource Type | Size Limit | Count Limit | Description |
|---|---|---|---|
| Scripts | 300KB | 10 | JavaScript files |
| Stylesheets | 50KB | 5 | CSS files |
| Images | 100KB | 20 | Image files |
| Fonts | 50KB | 5 | Font files |
| Total | 500KB | 50 | All resources combined |
Usage
Running Performance Tests
# Run all performance tests
npm run e2e:performance
# Run specific performance test
npx playwright test tests/e2e/performance.spec.ts --grep="homepage load performance"
# Run with specific browser
npx playwright test tests/e2e/performance.spec.ts --project=chromium
Running Lighthouse CI
# Run Lighthouse CI with default settings
npm run lhci
# Run with mobile preset
npm run lhci:mobile
# Run with desktop preset
npm run lhci:desktop
# Run with performance budgets
npm run performance:budget
Running Performance Monitoring
# Run comprehensive performance monitoring
npm run performance:monitor
# Run monitoring script directly
node scripts/performance-monitor.js
Performance Metrics
Core Web Vitals
-
Largest Contentful Paint (LCP)
- Measures loading performance
- Target: < 2.5 seconds
- Baseline: < 2.0 seconds
-
First Input Delay (FID)
- Measures interactivity
- Target: < 100ms
- Baseline: < 50ms
-
Cumulative Layout Shift (CLS)
- Measures visual stability
- Target: < 0.1
- Baseline: < 0.05
Navigation Timing
- DNS Lookup: Domain name resolution time
- TCP Connection: Connection establishment time
- TTFB: Time to First Byte
- Download: Resource download time
- DOM Content Loaded: DOM parsing completion
- Load: Full page load completion
Component Performance
- Render Time: Component rendering duration
- Interaction Time: User interaction response time
- Scroll Performance: Smooth scrolling performance
- Memory Usage: JavaScript heap memory consumption
Regression Detection
Automatic Detection
The performance monitoring system automatically detects regressions by:
- Comparing against baselines: Current metrics vs. established baselines
- Threshold monitoring: Real-time threshold violation detection
- Trend analysis: Performance degradation over time
- Statistical analysis: Variance and consistency monitoring
Regression Thresholds
- 20% degradation: Triggers regression warning
- 50% degradation: Triggers regression error
- Consistent degradation: Pattern-based regression detection
Alert System
// Example regression detection output
🚨 Performance regression detected: scroll_performance = 111ms (baseline: 30ms)
⚠️ Performance threshold exceeded: interaction_time = 1368ms (threshold: 100ms)
Performance Reports
Generated Reports
- Console Output: Real-time performance metrics and warnings
- JSON Reports: Structured performance data (
performance-report.json) - Lighthouse Reports: Detailed performance audits
- Playwright Reports: E2E test results with performance data
Report Structure
{
"timestamp": "2024-01-01T12:00:00.000Z",
"summary": {
"totalMetrics": 15,
"regressions": 2,
"warnings": 3
},
"regressions": [
{
"metric": "scroll_performance",
"current": 111,
"baseline": 30,
"regression": "270.0%"
}
],
"warnings": [
"Performance threshold exceeded: interaction_time = 1368ms (threshold: 100ms)"
],
"metrics": {
"page_load_time": {
"latest": 1704,
"average": 1704,
"min": 1704,
"max": 1704,
"count": 1
}
}
}
CI/CD Integration
GitHub Actions Integration
# Example CI workflow
- name: Performance Tests
run: |
npm run e2e:performance
npm run lhci
npm run performance:budget
Performance Gates
- Performance Score: Must be > 90
- Core Web Vitals: All metrics within budgets
- Regression Detection: No significant regressions
- Resource Budgets: All resources within limits
Best Practices
Development Workflow
- Pre-commit Checks: Run performance tests before commits
- Baseline Updates: Update baselines after performance improvements
- Budget Reviews: Regular budget review and adjustment
- Regression Investigation: Immediate investigation of detected regressions
Performance Optimization
- Code Splitting: Implement dynamic imports for better loading
- Image Optimization: Use modern formats and proper sizing
- Caching: Implement effective caching strategies
- Bundle Analysis: Regular bundle size monitoring
Monitoring Strategy
- Continuous Monitoring: Automated performance testing in CI/CD
- Real User Monitoring: Collect performance data from real users
- Alert Thresholds: Set appropriate alert thresholds
- Performance Budgets: Enforce strict performance budgets
Troubleshooting
Common Issues
-
Test Timeouts
- Increase timeout values for slow operations
- Add proper wait conditions
- Check for network issues
-
False Positives
- Adjust baseline values
- Review test environment
- Check for external dependencies
-
Performance Fluctuations
- Run multiple test iterations
- Use statistical analysis
- Consider environmental factors
Debugging Performance Issues
# Enable detailed logging
DEBUG=playwright:* npm run e2e:performance
# Run with specific browser and debugging
npx playwright test tests/e2e/performance.spec.ts --project=chromium --debug
# Generate detailed reports
npm run performance:monitor -- --verbose
Future Enhancements
Planned Features
-
Real User Monitoring (RUM)
- Collect performance data from real users
- User-centric performance metrics
- Geographic performance analysis
-
Advanced Analytics
- Machine learning-based regression detection
- Predictive performance modeling
- Automated performance optimization suggestions
-
Performance Dashboard
- Web-based performance monitoring dashboard
- Real-time performance metrics visualization
- Historical performance trends
-
Integration with APM Tools
- New Relic integration
- DataDog integration
- Custom APM tool integration
Conclusion
The performance monitoring system provides comprehensive coverage of application performance, enabling early detection of regressions and maintaining high performance standards. Regular monitoring and proactive optimization ensure optimal user experience across all platforms and devices.
For questions or issues with the performance monitoring system, please refer to the testing documentation or create an issue in the project repository.