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 cache test double declarations #5862

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sebastianbergmann
Copy link
Owner

While profiling the test double code generator today, I noticed that (counter-intuitively) memory consumption increases when test double code declarations are not cached. For instance, for PHPUnit's own test suite from 48 MB to 52 MB.

This is PR is not intended to be merged, but I would like to have a place where the investigation of this issue can be discussed.

@sebastianbergmann sebastianbergmann added the type/performance Issues related to resource consumption (time and memory) label Jun 12, 2024
@sebastianbergmann sebastianbergmann self-assigned this Jun 12, 2024
@sebastianbergmann
Copy link
Owner Author

This might be of interest to @staabm and @lolli42.

Copy link

codecov bot commented Jun 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.34%. Comparing base (778070d) to head (14c4c53).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #5862      +/-   ##
============================================
- Coverage     92.34%   92.34%   -0.01%     
+ Complexity     6554     6553       -1     
============================================
  Files           699      699              
  Lines         19772    19770       -2     
============================================
- Hits          18259    18257       -2     
  Misses         1513     1513              

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

}

return self::$cache[$key];
return $this->generateCodeForTestDoubleClass(
Copy link
Contributor

@staabm staabm Jun 13, 2024

Choose a reason for hiding this comment

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

I noticed that (counter-intuitively) memory consumption increases when test double code declarations are not cached.

sounds like this method is somehow triggering logic which when invoked repeatedly builds up a buffer or uses a ressource which leaks memory or has side-effects.

let me record a memory diff profile

Copy link
Contributor

@staabm staabm Jun 13, 2024

Choose a reason for hiding this comment

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

when running blackfire on 778070d, I don't see generateCodeForTestDoubleClass at all in the profile -> it's using very little memory and therefore blackfire decides it is not worth having information about this method in the profile.

when running blackfire on this PR, we get a bit more information. generateCodeForTestDoubleClass is still using very little memory, but at least it now shows up in the profile.

in this screen you can see how the memory used by generateCodeForTestDoubleClass spreads over calls triggered from this method:

grafik

it looks like the lions share of memory is consumed by reflection and autoloading , which makes sense to me.
both are apis which have side-effects... looking a bit closer it seems the that the caching kills the reflection overhead. autoloading is still visible with a similar amount in the cached variant.

if there is a way to reduce/remove calls to ReflectionClass->getMethods you might be able to remove the caching.


I ran blackfire on the unit test-suite as a whole. in case we could find a cli command in which the test-double generator itself would take a bigger share of the overall memory we could get more precise numbers of the diff between main and this PR (e.g. running phpunit only on a subset of tests which stress the testDouble-Generator more).

* @psalm-var array<non-empty-string, MockClass>
*/
private static array $cache = [];

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

as I have the numbers at hand atm, maybe this is also interessting (even though not related to this PRs topic):

running the test-suite as a whole, these calls are the most memory consuming ones:

grafik

and here you can find the slowest calls:

grafik


recorded 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
    with blackfire v1.92.16~win-x64-non_zts82, https://blackfire.io, by Blackfire

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/performance Issues related to resource consumption (time and memory)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants