foundry: Performance Problem with random
While diagnosing why some of our tests are so slow, I discovered the culprit for us is with random(). We have a function:
public static function randomApiUser(): ApiUser
{
$user = self::random()->object();
return new ApiUser($user);
}
And it was making tests take five times as long as when we didn’t use it.
After poking around, I noticed that rather than getting X random records, it’s getting ALL of them and then getting random ones from that.
public function randomRange(int $min, int $max, array $attributes = []): array
{
if ($min < 0) {
throw new \InvalidArgumentException(\sprintf('$min must be positive (%d given).', $min));
}
if ($max < $min) {
throw new \InvalidArgumentException(\sprintf('$max (%d) cannot be less than $min (%d).', $max, $min));
}
$all = \array_values($this->findBy($attributes));
\shuffle($all);
if (\count($all) < $max) {
throw new \RuntimeException(\sprintf('At least %d "%s" object(s) must have been persisted (%d persisted).', $max, $this->getClassName(), \count($all)));
}
return \array_slice($all, 0, \random_int($min, $max)); // @phpstan-ignore-line
}
I think it would be more performant if the code used something like this to just get random records from the database (with the appropriate attribute filters):
SELECT *
FROM table_name
ORDER BY RAND()
LIMIT 1;
I am willing to do a PR for this if you would like.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 23 (22 by maintainers)
Alright, got some time tonight to do it 👍🏻
There we go, I created two PRs. I see that at least one of them has some psalm issues but one of the reasons being that phpbench is not installed there. But alright, maybe best to discuss that there and not pollute this issue.
about
randomRange(): I’d really like that we keep only relying ondoctrine/persistence.Based on this constraint, I can only see half solutions, both being trade-offs:
random()and perform only one query, and change the offset… This would be kinda fake random, but I think this is ok-ish for testing purposeHi @mdeboer
this is very cool! We were actually talking with @kbond the we would like to add a CI check about performances, using PHPBench, maybe based on this example
Would you mind to create two PRs? one which just fixes the random stuff, and another one with your work on PHPBench?
If you’re willing to introduce the GitHub Action, this is super cool, but we can also handle this ourselves, based on your work
We can work on
randomRange()another time.makes totally sense.
thanks!
Alright, so fixing this for
random()(one random entity) is easy and yields a nice improvement. But I haven’t spend much time working onrandomSet()andrandomRange(), they seem a bit harder to implement at first glance.Most time went to setting up phpbench but I got it working. Writing benchmarks is pretty similar to writing either unit or integration tests now.
Anyway, here are the results for
random()I got with ORM so far. Comparing the original implementation in the2.xrepository with my implementation I explained before (using the random offset):Depending on the number of items in the repository, performance is increased by 30 to 90%! 😍 Also the stability in speed is improved considerable (50-75%).
You can find my changes here: https://github.com/mdeboer/foundry/tree/feat/speedup-random
If you like I can make a PR 👍🏻
Another thing to consider is that for example we use this in our tests and we usually seed the RNG to get random yet consistent results. This would not be possible with
ORDER BY RAND()as far as I know.I’m pulling up the code now to have a look at
random()andrandomRange()😃I just saw your edit: isn’t
doctrine/persistence+ dbal helps with this? here is the prototype ofObjectRepositroy::findBy():it actually has limit + offset parameters, I think your approach is ok for any DBs!
@nikophil I will have a look this weekend! 👍🏻
@nikophil - OK. I will pull the 2.0 branch and play locally. Hoping I can find a way to do it in a database agnostic way.