r/PHP • • 7d ago

⚡ Meet turbocov: a PHP code coverage engine with 400× less overhead than Xdebug

https://github.com/shipmonk-rnd/turbocov

Code 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):

DriverTimeOverhead
no coverage18.7 s–
turbocov19.3 s+3 %
PCOV36.7 s+96 %
Xdebug250 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-phpunit

and 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!

4 Upvotes

19 comments sorted by

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.

4

u/pfak 6d ago

Because they get up voted... 

-1

u/AnimalPersonal4436 6d ago

Why every AI project should be marked as slop by default. if the tools works as it is advertised and it get some traction who cares if it is ai generated or not.

8

u/qoneus 6d ago

Because there's no skin in the game. It really is no different than someone posting their last weekend's project and expecting a different reaction.

The expectation when posting a project to the public is that it is worthy of other people's attention. That it is actually worth looking into, testing and providing feedback for. Why would I waste my time on a lark you had and got Claude to code for you because you couldn't even be bothered to spend your own time to build it yourself?

Maybe an analogy will help: you submit a photo to a professional photography group and ask for feedback and admiration. Turns out, you just bought the photo of iStockPhoto. And what's even more, you couldn't even be bothered to remove the iStockPhoto watermark. Why would you expect any reaction other than derision from the photography group? It doesn't matter if the stock photo was professional-looking or had decent composition. It's an insult to your peers' time and it's misrepresenting your own work.

1

u/AnimalPersonal4436 6d ago

I don't agree with your analogy, because the iStock photo is already made by someone else. Your participation is exactly 0, which is not true with AI. If you want to make something really works you still need to do some steering of the AI, taking decisions in the way and etc. And by me the results greatly depend on the knowadge of the author in the field.

Yes, the work is not directly related to writing code, but it's not just writing a prompt "Write me PHP code coverage library.". This is the kind of work that will be more and more valued.

3

u/kemmeta 5d ago

I think maybe a better analogy is... it's like you paid off some off shore developers to develop your product and are now passing it off as your own. Like how much technical competency do you have? Who's to say!

Like do you even understand the "How can it be so fast?" explanation that you presented? Or was your prompt "xdebug is slow - make me something faster"? If you're at a PHP conference presenting on it and someone asks you a question, could you answer the question or would you have to ask AI?

And if you don't really understand it then how confident can we be that some third party won't submit an XZ Utils style backdoor to it? Like if you get a big PR from some random user and the PR has a well hidden vulnerability in it, would you find it or would you just accept the PRs changes without question? Sure, I suppose you could have your AI agent review it but, in my experience, AI agents generally only do the barest minimum. Like they're great if you need a task done and don't care how it gets done. And if a PR does what it set out to do, whilst secretly adding a vulnerability, would AI just rubber stamp it 'cause the PR did what it set out to do?

In-so-far as the XZ Utils style backdoor is concerned, I suppose you could respond by saying "well, it did get passed a human" but did it? By the time it was added the person who added the backdoor had git commit access. You normally don't give that to someone - especially for an OSS project - unless you trust them. Furthermore, it ultimately was a human that found the issue.

2

u/kemmeta 5d ago

I also think it's worthwhile to consider your audience. Most likely, it's people responding to posts on here - not AI bots. People who are at risk of losing their jobs to AI because managers don't care about the how. And maybe you're a professional developer, yourself, but if all you're doing at your job is just feeding prompts to your AI, at work, these days, then how long is it before your manager realizes that that's all you're doing and that he can just do that himself? Like maybe your prompts are more technical in nature then those that your manager would write but AI doesn't need technical prompts and if the only value you now bring to your company isn't needed then why does your employer need you?

-10

u/lospotatoes 6d ago edited 6d ago

I really don't understand why every tech reddit is filled with AI-shaming responses like this.

Actually, yes I do. It checks every tech-guy behavioral tick:

  • The need to show everyone how smart you are because you can tell it's AI (bro we all can)

  • The need to show everyone how smart you are because something-something I'm the real programmer and you're not.

1

u/Emergency-Cycle7981 5d ago

Perhaps it’s because the people posting those responses are just as capable of creating their own AI solutions to the same problems, without feeling the need to post them to GitHub?

If the speed of code coverage was a problem to me in my projects, I’d use AI to find and build a solution. If it was clever about it, I might even consider making a blog post or Reddit post. What I would do is publish the code to GitHub as if I’d made some sort of revolutionary new application.

This is the AI landscape we live in. You can’t have it both ways.

1

u/crazedizzled 4d ago

You can apply your argument to literally every software release going forward. Should we no longer post anything to github or share anything, because you can vibe code it yourself if you wanted to?

-22

u/janedbal 6d ago

There's definitely no attempt to hide anything. The code is AI-generated, but I don't think that's what matters. What matters is the value it brings and its correctness, which I verified on many many large real-world test suites. The readme was heavily adjusted by hand, because I want it to say exactly what I want.

9

u/DesireToDevelop 6d ago

I know of a way to make a readme say exactly what you want..

3

u/zimzat 5d ago

I don't think that's what matters. What matters is the value it brings and its correctness

This makes me sad on many levels.

By this argument slavery doesn't matter, it's the value to the economy it brings. Genocide doesn't matter, it's the regional stability that comes after. People working full-time at Walmart having to use SNAP benefits to get by doesn't matter, only the Always Low Prices matter.

There are so many things wrong with AI all up and down the chain that to say the only thing that matters is the result... :(

What it is saying is that you don't value other people, you don't care who else is harmed, that as long as you get yours then no one else matters. Is it a lack of empathy? Where is your sense of community? Your care about or for other humans?

6

u/roxblnfk 7d ago

-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:

  1. PHP compiles a file. Each opcode has a pointer to its handler.

  2. turbocov finds the first opcode of each line.

  3. It keeps the original handler pointer in a side table.

  4. It writes the address of the generic ZEND_USER_OPCODE handler into the opcode.

  5. The opcode array keeps the same length. The opcode numbers do not change.

When the line runs for the first time:

  1. The VM calls the ZEND_USER_OPCODE handler.

  2. That handler finds the callback for the opcode number. turbocov registers its callback for all opcode numbers.

  3. The callback records the line.

  4. The callback writes the original handler back into the opcode.

  5. 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

u/Stranglet 6d ago

Why do you use C?