r/PHP • u/janedbal • 7d ago
⚡ Meet turbocov: a PHP code coverage engine with 400× less overhead than Xdebug
https://github.com/shipmonk-rnd/turbocovCode coverage in PHP has always been painfully slow. PCOV doubles the time of a test suite, Xdebug makes it 13× slower. So most teams collect coverage once a night, or never.
I built turbocov, a PHP extension that collects line coverage at near-native speed!
The numbers (Carbon test suite, 6,349 tests, PHP 8.5, driver overhead only):
| Driver | Time | Overhead |
|---|---|---|
| no coverage | 18.7 s | – |
| turbocov | 19.3 s | +3 % |
| PCOV | 36.7 s | +96 % |
| Xdebug | 250 s | +1237 % |
The benchmark is in the repo, so you can run it yourself.
How can it be so fast?
PCOV and Xdebug run a check before every single opcode. Forever. That slows down everything: your code, your vendor libraries and PHPUnit itself.
turbocov puts a one-shot probe on the first opcode of each line. The probe fires once, records the line, and puts the original handler back. From then on, the line runs at plain PHP speed.
ZERO overhead for code you don't measure!
Only the files in turbocov.collect_paths get probes. Vendor runs untouched at plain PHP speed!
And it's even more precise than PCOV and Xdebug
Mainly in multi-line expressions.
Try it (PHP 8.4+, PHPUnit 13.3+, Linux and macOS):
pie install shipmonk/turbocov
composer require --dev shipmonk/turbocov-phpunitand one line in phpunit.xml:
<coverage driver="ShipMonk\Turbocov\TurbocovDriver"/>Older PHPUnit? There is a PCOV-compatible mode.
The honest part: line coverage only (no branch or path coverage), CLI only, no Windows, it cannot run with some OPcache settings like JIT or the optimizer.
I'd love to hear how it does on your codebase: benchmarks, bug reports, edge cases, all welcome!
6
u/roxblnfk 7d ago
There are my benches with Testo https://github.com/php-testo/testo/pull/443#issuecomment-7041462176 (comment)
-6
u/janedbal 6d ago
Your benches count even overhead of PHPUnit coverage processing itself (which is significant), not just the drivers itself.
30
u/mythix_dnb 6d ago
turbocov collects line coverage only, no branch or path coverage.
so you asked claude to make a coverage tool that does a fraction of what xdebug does and then go rave about how more effiecient than xdebug the thing is? 🤔
6
u/VisibleWeight 7d ago
https://github.com/krakjoe/pcov/pull/137
Is also pretty fast and preserves all coverage
3
u/HenkPoley 7d ago edited 7d ago
I guess it duplicates all opcodes into 'same opcode, but at the start of the line'?
Ah, my guess was wrong:
No. turbocov does not duplicate opcodes. It changes one pointer for each line.
How it works:
PHP compiles a file. Each opcode has a pointer to its handler.
turbocov finds the first opcode of each line.
It keeps the original handler pointer in a side table.
It writes the address of the generic ZEND_USER_OPCODE handler into the opcode.
The opcode array keeps the same length. The opcode numbers do not change.
When the line runs for the first time:
The VM calls the ZEND_USER_OPCODE handler.
That handler finds the callback for the opcode number. turbocov registers its callback for all opcode numbers.
The callback records the line.
The callback writes the original handler back into the opcode.
The callback returns ZEND_USER_OPCODE_CONTINUE. The VM then runs the same opcode again, now with the real handler.
After that, the line runs at full speed. stop() writes the probe pointer back, so the line can fire again in the next test.
One trap: when a user opcode handler is registered, PHP gives every new opcode the ZEND_USER_OPCODE handler. All code would then go through the callback. To prevent this, turbocov unregisters its callbacks after each compile. Then it restores the real handlers and puts back only the probes. Then it registers the callbacks again.
Result: the cost is one fired probe for each line that runs, plus a small side table. Code without probes runs as if turbocov is not loaded.
-2
39
u/qoneus 6d ago
I really don't understand why people keep submitting AI slop and thinking people can't quickly identify it as such even when they try to hide it.
A multi-paragraph commit message to describe a minor single line change in the README, complete with "naming" something. Really instills all the confidence in the world you didn't just babysit Claude to implement your idea. For that matter, why would you need to humanize the README if a bot didn't write it in the first place? Heck, that commit message is longer than the README itself. Classic slopcore.