I got trouble developing a Symfony 6 / PHP 8 project. I use AJAX lots of time on my page, and from the start of development the slightest request took several seconds. I use to got a response anyways, so i just continue my project without more explantion. But now some requests are becoming more complex at the controller level. I get to a point where clicking on a button takes more than 30 seconds to receive a response, and sometimes I don’t get one at all because the AJAX request stops before reaching the end:
Here the 3rd request doesn’t even have a profile because it didn’t go through to the end.
I don’t feel like I’m asking for the moon. The controller is mainly made up of queries to retrieve an object from the database, or sometimes calculte / update or render… Nothing really hard.
For example, for the 2nd request which lasts 789ms, there is only one call to the database to retrieve an object and send it to me, that’s all! And the parameters are sent in GET, I’m not even talking about a text in POST method. I find that 789ms is already excessive. Here is this piece of code for the 2sd request :
#[Route('/info/personnage/{id}', name: 'info_personnage')]
public function info($id, PersonnageRepository $personnageRepository, SerializerInterface $serializer):Response
{
$encoder = new JsonEncoder();
$defaultContext = [
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function (object $object, string $format, array $context): string {
return $object->getId();
},
];
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext);
$serializer = new Serializer([$normalizer], [$encoder]);
$personnage = $personnageRepository->findOneBy(['id' => $id]);
$response = $serializer->serialize($personnage, 'json');
return new Response($response);
}
And it’s this little piece of code that takes time according to the performance tab of the profile:
So if each request like this takes almost 1s, it will always be a problem. My previous 22s request which was unsuccessful should ideally be done in less than 2s… And in addition the requests to display images or dialogues arrive after this one.
I work in local env with MAMP, I don’t know if this is also an option to take into account. But if it’s so slow locally, I can’t imagine it on a remote server.
Do you have any ideas to give me to improve this response time with this information?