Skip to content

Dump dependency container and re-use it - 35% faster test-suite#3809

Merged
TomasVotruba merged 31 commits intorectorphp:mainfrom
staabm:dump-container
May 15, 2023
Merged

Dump dependency container and re-use it - 35% faster test-suite#3809
TomasVotruba merged 31 commits intorectorphp:mainfrom
staabm:dump-container

Conversation

@staabm
Copy link
Copy Markdown
Contributor

@staabm staabm commented May 12, 2023

closes rectorphp/rector#7930

Before

$  php vendor/bin/phpunit rules-tests/TypeDeclaration/Rector/
PHPUnit 10.1.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.5
Configuration: C:\dvl\Workspace\rector-src-staabm\phpunit.xml

...............................................................  63 / 417 ( 15%)
............................................................... 126 / 417 ( 30%)
............................................................... 189 / 417 ( 45%)
............................................................... 252 / 417 ( 60%)
............................................................... 315 / 417 ( 75%)
............................................................... 378 / 417 ( 90%)
.......................................                         417 / 417 (100%)

Time: 00:32.574, Memory: 1.13 GB

OK (417 tests, 475 assertions)

After

➜  rector-src git:(dump-container) php vendor/bin/phpunit rules-tests/TypeDeclaration/Rector/
PHPUnit 10.1.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.18
Configuration: /Users/staabm/workspace/rector-src/phpunit.xml

...............................................................  63 / 417 ( 15%)
............................................................... 126 / 417 ( 30%)
............................................................... 189 / 417 ( 45%)
............................................................... 252 / 417 ( 60%)
............................................................... 315 / 417 ( 75%)
............................................................... 378 / 417 ( 90%)
.......................................                         417 / 417 (100%)

Time: 00:08.483, Memory: 1.04 GB

OK (417 tests, 418 assertions)

running the whole test-suite it comes down to

Before php vendor/bin/phpunit 160.22s user 28.24s system 98% cpu 3:11.11 total
After php vendor/bin/phpunit 109.70s user 13.60s system 97% cpu 2:06.40 total

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented May 12, 2023

I wonder why these failling tests depend on ContainerBuilder instead of ContainerInterface. they are not meant to modify the container but just read from it, right?

it seems the PR works for all tests but e2e. I don't have an idea yet why

seems these are a few use cases which require ContainerBuilder because they use some more in-depth methods which are not reqiured for regular rector end-user use-cases

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented May 12, 2023

the failling CI job is unrelated. created a upstream issue composer-unused/composer-unused#514

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented May 12, 2023

its reproducible a lot faster locally then before. In CI it seems it got slower then before.. :-/.. still investigatin

@staabm staabm force-pushed the dump-container branch 2 times, most recently from a975cad to 814a7ca Compare May 12, 2023 14:02
Copy link
Copy Markdown
Member

@samsonasik samsonasik left a comment

Choose a reason for hiding this comment

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

The commit with double quote: Revert "debug infos" should be changed to single quote or no quote to avoid error on build scoped.

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented May 12, 2023

in case this works in the near future I will squash everything together.

atm I don't know why it gets slower on CI, but gets faster on my mac and also my windows laptop :/

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented May 12, 2023

perf wise we reached the goal. we are equal fast in CI and locally. I have updated the benchmark in the description

@staabm staabm changed the title Dump dependency container and re-use it Dump dependency container and re-use it - 35% faster test-suite May 12, 2023
@staabm staabm marked this pull request as ready for review May 12, 2023 16:48
@staabm staabm requested a review from TomasVotruba as a code owner May 12, 2023 16:48
@staabm staabm force-pushed the dump-container branch 2 times, most recently from 5b2b6f0 to 01bee16 Compare May 12, 2023 17:28
@staabm staabm force-pushed the dump-container branch 2 times, most recently from 9ffc4d2 to 9c8656f Compare May 13, 2023 06:44
@staabm staabm marked this pull request as draft May 13, 2023 06:46
/**
* @var string
*/
private const CACHE_KEY = 'kernel-v4';
Copy link
Copy Markdown
Contributor Author

@staabm staabm May 13, 2023

Choose a reason for hiding this comment

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

when changing dependencies of registered services without also changing existing config files - like e.g. in #3817 -, one need to increase this key to invalidate existing permanent compiled DI containers

see e.g. https://github.com/rectorphp/rector-src/actions/runs/4966591084/jobs/8888109775 what happens when you forget to change the cache key

as soon as you have a need to adjust config files, cache will invalidate automatically

Copy link
Copy Markdown
Contributor Author

@staabm staabm May 15, 2023

Choose a reason for hiding this comment

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

with ecf3dc6 I have added a new case, which automatically invalidates the compiled container caches, when exceptions are thrown while getting services out of the container - to improve DX so we don't need to update the RectorKernel::CACHE_KEY for most operations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, I see this note

@staabm staabm marked this pull request as ready for review May 13, 2023 11:29
@staabm staabm marked this pull request as draft May 14, 2023 18:07
@staabm staabm marked this pull request as ready for review May 14, 2023 18:31
Comment on lines +66 to +70
// clear compiled container cache, to trigger re-discovery
RectorKernel::clearCache();

throw $e;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems hacky. What is it need for and when it happens? It might be hiding a bug we have

We can adress it later, to keep this PR merged 👍

return $container;
}

}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Much better, thank you 👍

@TomasVotruba TomasVotruba merged commit 3bdd519 into rectorphp:main May 15, 2023
@TomasVotruba
Copy link
Copy Markdown
Member

Let's ship it... thank you Markus for tremendous work on this one 👏

@staabm staabm deleted the dump-container branch May 15, 2023 15:57
@samsonasik
Copy link
Copy Markdown
Member

@staabm it seems cause error in tests when add new param to __construct, see https://github.com/rectorphp/rector-src/actions/runs/4982896946/jobs/8919207651#step:6:26

/**
* @var string
*/
private const CACHE_KEY = 'v7';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should it be updated when add new param to __construct ? see error https://github.com/rectorphp/rector-src/actions/runs/4982917436/jobs/8919255393#step:6:26


There was 1 error:

1) Rector\Core\Tests\Issues\DowngradeNewInArrayInitializerPromotion\DowngradeNewInArrayInitializerPromotionTest::test with data set #0
ArgumentCountError: Too few arguments to function Rector\Core\PhpParser\Parser\InlineCodeParser::__construct(), 3 passed in /tmp/rector/kernel-v7-b923df1ea94cad9fb5b38891cbfaa566ed8e88d7e8551c1c3c978857f94fc876.php on line 3246 and exactly 4 expected

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented May 15, 2023

It will auto-repair on the next commit

see #3809 (comment)

@samsonasik
Copy link
Copy Markdown
Member

It seems it not auto repair, I need to increase the key b4ad93c

@samsonasik
Copy link
Copy Markdown
Member

@staabm here first commit that error: https://github.com/rectorphp/rector-src/actions/runs/4982896946/jobs/8919207651

here second commit that still error https://github.com/rectorphp/rector-src/actions/runs/4982917436/jobs/8919255393

so need to increate key cache to fix it b4ad93c

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented May 15, 2023

I will have a closer look tomorrow. Thx

@samsonasik
Copy link
Copy Markdown
Member

@staabm it seems also make e2e test on rector/rector repo error, see https://github.com/rectorphp/rector/actions/runs/4982533394/jobs/8918337344

@samsonasik
Copy link
Copy Markdown
Member

samsonasik commented May 15, 2023

@staabm I tried "rector/rector": "dev-main#bd630a0db2db355484ee4d5a99850facb2323f37" in laminas-servicemanager-migration tests and got error as well:

➜  laminas-servicemanager-migration git:(1.13.x) ✗ vendor/bin/phpunit 
PHPUnit 9.6.8 by Sebastian Bergmann and contributors.

EEEEEEEEEEEEEEEEEEE                                               19 / 19 (100%)

Time: 00:00.039, Memory: 16.00 MB

There were 19 errors:

1) LaminasTest\ServiceManager\Migration\Rector\Class_\ImplementsFactoryInterfaceToPsrFactoryRector\AutoImportRenameUseTest::test with data set #0 ('/Users/samsonasik/www/laminas...hp.inc')
Error: Class "RectorPrefix202305\Symplify\SmartFileSystem\SmartFileSystem" not found

/Users/samsonasik/www/laminas/laminas-servicemanager-migration/vendor/rector/rector/src/Kernel/CachedContainerBuilder.php:40
/Users/samsonasik/www/laminas/laminas-servicemanager-migration/vendor/rector/rector/src/Kernel/RectorKernel.php:112
/Users/samsonasik/www/laminas/laminas-servicemanager-migration/vendor/rector/rector/src/Kernel/RectorKernel.php:55
/Users/samsonasik/www/laminas/laminas-servicemanager-migration/vendor/rector/rector/packages/Testing/PHPUnit/AbstractTestCase.php:38
/Users/samsonasik/www/laminas/laminas-servicemanager-migration/vendor/rector/rector/packages/Testing/PHPUnit/AbstractRectorTestCase.php:60

Step to reproduce:

git clone git@github.com:laminas/laminas-servicemanager-migration.git
cd laminas-servicemanager-migration
composer require rector/rector:dev-main#bd630a0db2db355484ee4d5a99850facb2323f37
vendor/bin/phpunit

@samsonasik
Copy link
Copy Markdown
Member

For reference error since rectorphp/rector@bd630a0 build from this PR.

@samsonasik
Copy link
Copy Markdown
Member

I create an issue for that so it tracked rectorphp/rector#7939

@samsonasik
Copy link
Copy Markdown
Member

@staabm I created PR #3865 to fix phpunit error on using rector/rector:dev-main with verify VersionResolver::PACKAGE_VERSION still @package_version@

The increase cache_key is different issue to resolve tho :), I will let you check

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.

Slow rule-tests Testsuite

3 participants