New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce --threads=max
option value to automatically detect the number of CPU cores
#1723
Conversation
…mber of CPU cores Implements #1722
…ct the number of CPU cores For infection/infection#1723
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left few comments, mostly nits. The only real deal is LogicException
at the end of provide()
.
My 2 cents: it is not Infection responsibility. We just make our codebase more complicated. |
…tead of throwing an exception
Addressed your comments, initial version was blindly copied from Psalm.
Well, yes and no. IMO it's all about "those tiny things for DX". You set
Yes it's completely not critical and both points can be ignored, but it's a little thing that might be more convenient for someone (which is why this feature request was born from our end user, not me :) ). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is a very dangerous option, yet if people want this I guess that's what we have to add 🤔
@sanmai what makes it dangerous? |
I would seriously object enabling this option by default, or recommending it to new users. |
That means the threads upper limit should be restricted / validated regardless of how you specify it, which further proves the idea Infection should know the upper limit. For example, if max=12, what difference is it if I say --threads=12 or --threads=max, from the user POV this is totally equivalent. If running it like that is "dangerous", infection should disallow or warn the user about it. Adding --threads=max seems unrelated (with using max threads being "dangerous") to me. |
Infection can't know the upper limit. Say, if your test suite is predominantly IO-bound, then it makes sense to run Infection with more threads than you have CPU cores. Do you think there's a sure way what to do in this situation? How can we know if a test suite is CPU-bound or IO-bound to get the best results? User should be responsible for setting the thread number, and if we're setting the number for the user we're setting them up for potentially unpleasant consequences. |
This is knowledge you have, the user doesn't. It sounds like Infection could have named threads calculator strategies which the user can try out and use some which make sense. For example, |
@maks-rafalko what do you think here? |
How should we approach this idea, given Infection is a PHP app? |
@sanmai not sure what you mean here? Since now infection knows the number of CPUs available, I can imagine you could do something like
All names and numbers are illustrational, of course. |
…ct the number of CPU cores For infection/infection#1723
/** | ||
* @internal | ||
*/ | ||
final class CpuCoresCountProvider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of CpuInfo::getCount()
as a name instead?
*/ | ||
public static function provide(): int | ||
{ | ||
if (defined('PHP_WINDOWS_VERSION_MAJOR')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth caching the result? Although I don't expect it to be called twice, you also don't expect the system to change the number of CPU cores available within the same process and it's only a int
value
final class CpuCoresCountProvider | ||
{ | ||
/** | ||
* Copied and adapter from Psalm project: https://github.com/vimeo/psalm/blob/4.x/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php#L1454 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any chance Psalm is willing to extract it to a dedicated package? Otherwise provided they would be interested in a package, I'd be happy to provide one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be interesting as some sort of tiny "system information" HAL package which could be reused across the ecosystem indeed. 👍
Sorry for the late review, great addition! Happy to do the changes myself |
Implements #1722
Doc PR: infection/site#240