feat: add support for benchmarks using phpbench#866
Conversation
|
Hi! thanks for this! I'll handle the PHPStan problem, I have an idea to handle this nicely Correct me if I'm wrong, to run this comparison stuff, you should do this: I think, with this, we will be able to show the output in the CI the ultimate goal being to have the CI fail when a serious performance problem occurs. I've manage to have an assertion check doing the following: My main problem here, is that in the case of your fix on I need to deep dive into the assertion mechanism to understand better the granularity of those assertions |
|
Hi @nikophil!
Yes that is entirely correct! But for CI you want both commands to run with e.g. 5 iterations using
Ah yes, well I included the 1 entity case for verbosity but this is not a realistic case I think. I think 5 would have been a better more realistic test case, which should perform equally or better. It can easily be changed in the "data provider" here. |
|
it seems that in my benchmark, even with 50 entities, performing a I'm totally fine with it since we're talking about ~2ms, but I'm wondering how to not fail the CI (I need to test it with |
e03e4a0 to
56f7318
Compare
|
OK, I think I got something that is ok-ish :) I've added a bench for "random" for ODM + another bench for "create" and "create many" for 1 ODM class, 2 ORM class (on one class, there is a one-to-many being provided as well) I still cannot run the true "baseline" run, because phpbench is still not present on 2.x branch, I'll need another PR for that, I'll work on the assertions in this further PR here is the output of my CI run it's strange how I'd take a review from you before merging 🙏 thanks for your help on this! |
| "test-main": "./phpunit --testsuite main", | ||
| "test-reset-database": "./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php" | ||
| "test-reset-database": "./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php", | ||
| "post-install-cmd": ["@composer bin phpstan install", "@composer bin phpbench install"] |
There was a problem hiding this comment.
this way, when we'll do "composer install", both phpstan and phpbench will be installed, and no more problem with phpstan locally because it would not know about phpbench
| - name: Install PHPBench | ||
| run: composer bin phpbench install |
There was a problem hiding this comment.
same than https://github.com/zenstruck/foundry/pull/866/files#r2036127527 but for CI
| @@ -0,0 +1,170 @@ | |||
| <?php | |||
|
|
|||
| namespace Zenstruck\Foundry\Tests\Benchmark; | |||
There was a problem hiding this comment.
I've moved this class an the other one in the tests directory
|
|
||
| #[BeforeClassMethods(['_resetDatabaseBeforeFirstBench'])] | ||
| #[BeforeMethods(['_bootFoundry', '_resetDatabaseBeforeEachBench'])] | ||
| abstract class PersistentFactoryBench extends KernelBench |
There was a problem hiding this comment.
I had to split into two different base classes, because, performing all these static::factory()->many($params['count'])->create(); in _setup_bench_random() was really time consuming
56f7318 to
12cbf9c
Compare
|
I just don't understand those results for the random method: I've removed all "randomness" and only used deterministic stuff:
and I still have very volatile results, only for |
3874f09 to
2fec28a
Compare
|
I've added the option They warn on their website that this can increase the duration of the run and indeed, without a threshold the duration was 6min and now it is 9min But I think it is worth it... In the doc they advertise that for a "baseline", the threshold should be 5. they actually say :
And this does not affect the rest of the CI... Maybe if we feel that it takes too much time, in the future, I'll run only once the baseline when merging, and I'll store it in cache using I'm merging this PR, since results are now ok-ish don't hesitate to make any remarks even after this has been merged |
|
Looks great guys, I'll have another in-depth look when I have a bit more time ❤️ |
Co-authored-by: Nicolas PHILIPPE <[email protected]>
Co-authored-by: Nicolas PHILIPPE <[email protected]>


This PR adds support for running benchmarks using phpbench as discussed in #612
I have included both a
KernelBenchandUnitBenchclass that can serve as a base for a benchmarkclass.
The
KernelBenchbase class compares to theKernelTestCasefor integration tests where an actual application kernel is booted. Whereas theUnitBenchbase class compares to a regular test case class used in unit tests; it boots foundry but not a whole Symfony kernel.I have included a sample benchmark that uses
KernelBenchand uses the 'reset database' functionality.Unfortunately phpbench does not have a similar system regarding hooks as phpunit, hence I could not solve the 'reset database' functionality with a trait that automatically makes sure some code is being run before the tests. Who knows I might knock on their door next to have that improved as this is wonky at best, but I knew of no other way to fix this.
If you have a
KernelBenchbenchmark that does not require the 'reset database' functionality, you could leave out the class attributes on your benchmark.