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

Don't use die() but echo to skip tests. #168

Merged
merged 2 commits into from
Dec 3, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Oct 11, 2024

To unlock PHPUnit side-effect-free in-process --SKIPIF-- evaluation, we use regular echo instead of die().
That way PHPunit can detect whether conditions may be evaluated in the main-process to reduce sub-process creation overhead.

Depends on the work-inprogress of sebastianbergmann/phpunit#5974


Running phpunit with the side-effect detecting PR vs. the regular master version, yields the following results in this PR on my windows laptop:

$ hyperfine '/c/tools/php83/php ../phpunit/phpunit tests' '/c/tools/php83/php vendor/bin/phpunit tests' --warmup=3
Benchmark 1: C:/tools/php83/php ../phpunit/phpunit tests
  Time (mean ± σ):      3.312 s ±  0.020 s    [User: 0.184 s, System: 0.510 s]
  Range (min … max):    3.291 s …  3.347 s    10 runs

Benchmark 2: C:/tools/php83/php vendor/bin/phpunit tests
  Time (mean ± σ):      3.555 s ±  0.031 s    [User: 0.220 s, System: 0.599 s]
  Range (min … max):    3.521 s …  3.604 s    10 runs

Summary
  C:/tools/php83/php ../phpunit/phpunit tests ran
    1.07 ± 0.01 times faster than C:/tools/php83/php vendor/bin/phpunit tests

$ /c/tools/php83/php -v
PHP 8.3.11 (cli) (built: Aug 27 2024 21:28:35) (NTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.3.11, Copyright (c) Zend Technologies

-> inline-skip-if evaluation of 7 test-cases - as proposed with this PR - will yield a 7-8% perf improvement.

tests/never-typed.phpt Outdated Show resolved Hide resolved
To unlock PHPUnit side-effect-free in-process --SKIPIF-- evaluation, we
use regular echo instead of die().
That way PHPunit can detect whether conditions may be evaluated in the
main-process to reduce sub-process creation overhead.

see sebastianbergmann/phpunit#5974
@jrfnl
Copy link
Collaborator

jrfnl commented Oct 14, 2024

Out of curiousity: how does PHPUnit decide whether the test should be skipped or not ?
I thought that was related to the exit code of the script run in the SKIPIF process ?

If that's no longer the case, the criteria for how the test skipping is determined in phpt tests need to be clearly outlined and documented.

@staabm
Copy link
Contributor Author

staabm commented Oct 14, 2024

Out of curiousity: how does PHPUnit decide whether the test should be skipped or not ?
I thought that was related to the exit code of the script run in the SKIPIF process ?

in the current stable PHPUnit release a test is skipped, when the executed code of the SKIPIF section output starts-with the string skip
see https://github.com/sebastianbergmann/phpunit/blob/a82cc054802a11b786f3159030a93d106335cb4d/src/Runner/PHPT/PhptTestCase.php#L420-L451

this works for e.g. die("skip because something happened") - because die prints the result on stdout or also regular echo "skip because something happened".


the indicator, when a test will be skipped, does not change with my proposed PR.
only difference between echo and die is, that we can evaluate code - which does not exit/die the current process or does not pollute the main-process scope (e.g. by defining classes/functions/symbols) - in the phpunit main process - and thats the reason why I propose this changes here.

In the PHPUnit PR I am using staabm/side-effects-detector to inspect the code in the SKIPIF section, tokenize it and look for tokens which produce such side-effects. if we don't find any relevant side-effects we can eval the in the main-process, instead of a isolated sub-process.

@staabm staabm changed the title POC: Don't use die() but echo to skip tests. Don't use die() but echo to skip tests. Oct 17, 2024
@staabm staabm marked this pull request as ready for review October 17, 2024 13:51
@staabm
Copy link
Contributor Author

staabm commented Oct 18, 2024

the upstream PHPUnit PR was merged and will be part of the next 11.5.x release

@staabm
Copy link
Contributor Author

staabm commented Oct 29, 2024

a similar change was merged today into symfony with symfony/symfony#58680

Copy link
Collaborator

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

LGTM.

While the performance improvement will only be seen on PHPUnit 11.5, this change doesn't seem to negatively impact runs on older PHPUnit versions, so I see no reason not to merge this.

I also think it would be good to document this feature for PHPT tests, but that's a task for the PHPUnit website (which currently doesn't have any documentation on PHPT tests - see sebastianbergmann/phpunit-documentation-english#302 ).

@antecedent antecedent merged commit 57de00f into antecedent:master Dec 3, 2024
9 checks passed
@staabm staabm deleted the real-world branch December 3, 2024 12:25
@jrfnl jrfnl added this to the 2.2 Next milestone Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants