Skip to content
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

Do not use a shell in proc_open() if not really needed #5766

Merged
merged 2 commits into from
Mar 28, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Mar 23, 2024

refs #5749 (comment)

in my testing I can confirm on PHP 8.3 the process starts 1-2ms faster.
a hello world process isolation test on my machine runs in 46-47ms, so 1-2ms faster is not too bad actually.

requires sebastianbergmann/environment#72

@staabm staabm force-pushed the no-shell branch 2 times, most recently from 7f2ab0b to 9c65505 Compare March 23, 2024 07:50
@sebastianbergmann sebastianbergmann added type/performance Issues related to resource consumption (time and memory) feature/process-isolation Issues related to running tests in separate PHP processes labels Mar 23, 2024
@sebastianbergmann sebastianbergmann changed the title Do not use a shell in proc_open if not really needed Do not use a shell in proc_open() if not really needed Mar 23, 2024
@staabm
Copy link
Contributor Author

staabm commented Mar 23, 2024

@MasonM could you please double check whether this PR does not introduce regressions? could you run it against your existing test-suite (on non windows machines; I have a look into windows support when I get back to a win laptop)?

@crrodriguez
Copy link

Note that any advantage in performance is highly environment dependant. you might not see any difference with older PHP versions, old OS versions or any combination thereof.

Copy link

@MasonM MasonM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staabm Okay, I manually applied this patch to our codebase. It works, but the performance improvement is indistinguishable from noise (which is going to be the case for basically anything that isn't a 5%+ improvement).

Before:

$ uname -a
Linux 485b0600476b 6.6.16-linuxkit #1 SMP Fri Feb 16 11:54:02 UTC 2024 aarch64 GNU/Linux

$ php -v
PHP 8.2.10 (cli) (built: Sep  4 2023 08:13:17) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.10, Copyright (c), by Zend Technologies
    with Xdebug v3.2.1, Copyright (c) 2002-2023, by Derick Rethans

$ phpunit -c ./phpunit.xml-dist
PHPUnit 9.5.23 #StandWithUkraine

...........................................................    59 / 20179 (  0%)
<SNIP>
.                                                           20179 / 20179 (100%)

Time: 07:03.880, Memory: 776.00 MB

OK (20179 tests, 40817 assertions)

After:

$ phpunit -c ./phpunit.xml-dist
OK
PHPUnit 9.5.23 #StandWithUkraine

...........................................................    59 / 20179 (  0%)
<SNIP>
.                                                           20179 / 20179 (100%)

Time: 06:59.617, Memory: 776.00 MB

OK (20179 tests, 40817 assertions)

@staabm
Copy link
Contributor Author

staabm commented Mar 25, 2024

Thanks for testing @MasonM .

Since we are just improving frmework overhead even this few ms are nice as it summs up across all projects beeing run on CI with phpunit.

The forking change should improve a lot more but will take more work and time.

These changes cannot make a slow test fast. 😅

Thanks again

@crrodriguez
Copy link

@staabm Okay, I manually applied this patch to our codebase. It works, but the performance improvement is indistinguishable from noise (which is going to be the case for basically anything that isn't a 5%+ improvement).

Before:

$ uname -a
Linux 485b0600476b 6.6.16-linuxkit #1 SMP Fri Feb 16 11:54:02 UTC 2024 aarch64 GNU/Linux

$ php -v
PHP 8.2.10 (cli) (built: Sep  4 2023 08:13:17) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.10, Copyright (c), by Zend Technologies
    with Xdebug v3.2.1, Copyright (c) 2002-2023, by Derick Rethans

$ phpunit -c ./phpunit.xml-dist
PHPUnit 9.5.23 #StandWithUkraine

...........................................................    59 / 20179 (  0%)
<SNIP>
.                                                           20179 / 20179 (100%)

Time: 07:03.880, Memory: 776.00 MB

OK (20179 tests, 40817 assertions)

After:

$ phpunit -c ./phpunit.xml-dist
OK
PHPUnit 9.5.23 #StandWithUkraine

...........................................................    59 / 20179 (  0%)
<SNIP>
.                                                           20179 / 20179 (100%)

Time: 06:59.617, Memory: 776.00 MB

OK (20179 tests, 40817 assertions)

That PHP is too old. most gains will be with 8.3 vs older releases. again it is highly environment dependant.

@staabm
Copy link
Contributor Author

staabm commented Mar 26, 2024

interessting find.. as soon as I add a sleep after $this->process() the failling tests start to succeed on windows:

diff --git a/src/Util/PHP/DefaultPhpProcess.php b/src/Util/PHP/DefaultPhpProcess.php
index b85e31048..df228024a 100644
--- a/src/Util/PHP/DefaultPhpProcess.php
+++ b/src/Util/PHP/DefaultPhpProcess.php
@@ -115,6 +115,7 @@ protected function runProcess(string $job, array $settings): array

         if ($job) {
             $this->process($pipes[0], $job);
+            sleep(1);
         }

         fclose($pipes[0]);

@crrodriguez
Copy link

interessting find.. as soon as I add a sleep after $this->process() the failling tests start to succeed on windows:

diff --git a/src/Util/PHP/DefaultPhpProcess.php b/src/Util/PHP/DefaultPhpProcess.php
index b85e31048..df228024a 100644
--- a/src/Util/PHP/DefaultPhpProcess.php
+++ b/src/Util/PHP/DefaultPhpProcess.php
@@ -115,6 +115,7 @@ protected function runProcess(string $job, array $settings): array

         if ($job) {
             $this->process($pipes[0], $job);
+            sleep(1);
         }

         fclose($pipes[0]);

so, there is a race condition.. output comes but not fast enough.. unfortunately at least according to the manual you can't stream_select on things returned by proc_open on windows.. :-|

@staabm staabm marked this pull request as ready for review March 27, 2024 08:04
@staabm
Copy link
Contributor Author

staabm commented Mar 27, 2024

my local testing suggests on windows we are now having 25-30ms less overhead per isolated-test:
php ./phpunit tests/end-to-end/regression/5764/5764.phpt --configuration tests/end-to-end/regression/5764/phpunit.xml --do-not-cache-result

overall end-to-end this means

after

mstaab@NB-COMPLEX-61 MINGW64 /c/dvl/Workspace/phpunit (no-shell)
$ php ./phpunit --testsuite end-to-end
PHPUnit 10.5.15-11-gfb8de2dcb by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12
Configuration: C:\dvl\Workspace\phpunit\phpunit.xml

...............................................................  63 / 468 ( 13%)
...........SSSSSSS........................S..................S. 126 / 468 ( 26%)
.............................................S................. 189 / 468 ( 40%)
.............................S................................. 252 / 468 ( 53%)
............................................................... 315 / 468 ( 67%)
.............................................................S. 378 / 468 ( 80%)
...................................S.......S................... 441 / 468 ( 94%)
...S.......................                                     468 / 468 (100%)

Time: 01:39.220, Memory: 8.00 MB

OK, but some tests were skipped!
Tests: 468, Assertions: 453, Skipped: 15.

before

mstaab@NB-COMPLEX-61 MINGW64 /c/dvl/Workspace/phpunit (10.5)
$ php ./phpunit --testsuite end-to-end
PHPUnit 10.5.15-10-g406a2b48d by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12
Configuration: C:\dvl\Workspace\phpunit\phpunit.xml

...............................................................  63 / 468 ( 13%)
...........SSSSSSS........................S..................S. 126 / 468 ( 26%)
.............................................S................. 189 / 468 ( 40%)
.............................S................................. 252 / 468 ( 53%)
............................................................... 315 / 468 ( 67%)
........................................................S....S. 378 / 468 ( 80%)
...................................S.......S................... 441 / 468 ( 94%)
...S.......................                                     468 / 468 (100%)

Time: 01:57.334, Memory: 8.00 MB

OK, but some tests were skipped!
Tests: 468, Assertions: 452, Skipped: 16.

=> ~25% faster on windows 🚀

$ php -v
PHP 8.2.12 (cli) (built: Oct 24 2023 21:15:35) (NTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.2.12, Copyright (c) Zend Technologies

as windows is no longer using a tempfile after this PR, we could now remove the whole tempfile logic which simplifies the implementation further. this could be done here in a separate commit or in a separate PR

@staabm staabm marked this pull request as draft March 27, 2024 10:13
Copy link

codecov bot commented Mar 28, 2024

Codecov Report

Attention: Patch coverage is 61.90476% with 8 lines in your changes are missing coverage. Please review.

Project coverage is 90.14%. Comparing base (f8941b6) to head (5c2cc5b).

❗ Current head 5c2cc5b differs from pull request most recent head eeddef5. Consider uploading reports for the commit eeddef5 to get more accurate results

Files Patch % Lines
src/Util/PHP/AbstractPhpProcess.php 53.33% 7 Missing ⚠️
src/Util/PHP/WindowsPhpProcess.php 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               10.5    #5766      +/-   ##
============================================
+ Coverage     90.09%   90.14%   +0.04%     
+ Complexity     6447     6443       -4     
============================================
  Files           680      680              
  Lines         19557    19543      -14     
============================================
- Hits          17620    17617       -3     
+ Misses         1937     1926      -11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@staabm staabm marked this pull request as ready for review March 28, 2024 08:42
@staabm
Copy link
Contributor Author

staabm commented Mar 28, 2024

did some more testing. I locally applied this PRs changes to the main-branch and used it to run tests of Roave/BetterReflection

I can see a 8% end-to-end improvement when running the test-suite.


I also used this same patched phpunit@main and run the phpunit 11 tests several times.
I can see the phpunit 11 testsuite is running ~15 seconds faster then before (so a simliar improvemt I already outlined a few comments above for the phpunit 10.x suite):

with this PR

$ php ./phpunit --testsuite end-to-end
PHPUnit 11.1-gba8e3adc4 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12
Configuration: C:\dvl\Workspace\phpunit\phpunit.xml

...............................................................  63 / 498 ( 12%)
...............................S.........................S..... 126 / 498 ( 25%)
.............S..............................................S.. 189 / 498 ( 37%)
.................................................S............. 252 / 498 ( 50%)
............................................................... 315 / 498 ( 63%)
............................................................... 378 / 498 ( 75%)
...................S....................................S...... 441 / 498 ( 88%)
.S...............................S.......................       498 / 498 (100%)

Time: 01:50.388, Memory: 8.00 MB

OK, but some tests were skipped!
Tests: 498, Assertions: 489, Skipped: 9.

before

mstaab@NB-COMPLEX-61 MINGW64 /c/dvl/Workspace/phpunit (main)
$ php ./phpunit --testsuite end-to-end
PHPUnit 11.1-gb4a469305 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12
Configuration: C:\dvl\Workspace\phpunit\phpunit.xml

...............................................................  63 / 498 ( 12%)
...............................S.........................S..... 126 / 498 ( 25%)
.............S..............................................S.. 189 / 498 ( 37%)
.................................................S............. 252 / 498 ( 50%)
............................................................... 315 / 498 ( 63%)
............................................................... 378 / 498 ( 75%)
..............S....S....................................S...... 441 / 498 ( 88%)
.S...............................S.......................       498 / 498 (100%)

Time: 02:06.768, Memory: 8.00 MB

OK, but some tests were skipped!
Tests: 498, Assertions: 488, Skipped: 10.

all testing done on windows11 with

$ php -v
PHP 8.2.12 (cli) (built: Oct 24 2023 21:15:35) (NTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.2.12, Copyright (c) Zend Technologies

@staabm
Copy link
Contributor Author

staabm commented Mar 28, 2024

as windows is no longer using a tempfile after this PR, we could now remove the whole tempfile logic which simplifies the implementation further. this could be done here in a separate commit or in a separate PR

I would suggest removing the now obsolete stuff in a separate PR, which I can open immediately after this is merged

@sebastianbergmann sebastianbergmann merged commit 9320731 into sebastianbergmann:10.5 Mar 28, 2024
29 checks passed
@sebastianbergmann
Copy link
Owner

Thanks!

@staabm staabm deleted the no-shell branch March 28, 2024 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/process-isolation Issues related to running tests in separate PHP processes type/performance Issues related to resource consumption (time and memory)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants