mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
## Summary This PR implements `perf_hooks.monitorEventLoopDelay()` for Node.js compatibility, enabling monitoring of event loop delays and collection of performance metrics via histograms. Fixes #17650 ## Implementation Details ### JavaScript Layer (`perf_hooks.ts`) - Added `IntervalHistogram` class with: - `enable()` / `disable()` methods with proper state tracking - `reset()` method to clear histogram data - Properties: `min`, `max`, `mean`, `stddev`, `exceeds`, `percentiles` - `percentile(p)` method with validation - Full input validation matching Node.js behavior (TypeError vs RangeError) ### C++ Bindings (`JSNodePerformanceHooksHistogramPrototype.cpp`) - `jsFunction_monitorEventLoopDelay` - Creates histogram for event loop monitoring - `jsFunction_enableEventLoopDelay` - Enables monitoring and starts timer - `jsFunction_disableEventLoopDelay` - Disables monitoring and stops timer - `JSNodePerformanceHooksHistogram_recordDelay` - Records delay measurements ### Zig Implementation (`EventLoopDelayMonitor.zig`) - Embedded `EventLoopTimer` that fires periodically based on resolution - Tracks last fire time and calculates delay between expected vs actual - Records delays > 0 to the histogram - Integrates seamlessly with existing Timer system ## Testing ✅ All tests pass: - Custom test suite with 8 comprehensive tests - Adapted Node.js core test for full compatibility - Tests cover enable/disable behavior, percentiles, error handling, and delay recording ## Test plan - [x] Run `bun test test/js/node/perf_hooks/test-monitorEventLoopDelay.test.js` - [x] Run adapted Node.js test `test/js/node/test/sequential/test-performance-eventloopdelay-adapted.test.js` - [x] Verify proper error handling for invalid arguments - [x] Confirm delay measurements are recorded correctly 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>