DamienHarper / auditor-bundle

The missing audit log library
MIT License
400 stars 121 forks source link

Performance issues with metadatas #319

Closed mblotin-ledger closed 1 year ago

mblotin-ledger commented 2 years ago
Q A
auditor-bundle version 5.0.1
PHP version 7.4.28
Database PostgreSQL

Summary

We have 350 entities at the moment, and on one of our simple APIs (an insert on a non audited entity), it takes a lot of time. After investigation, I believe the issue is within this bundle. It might be because I missed a configuration option, but I wanted to raise the problem we're having.

Current behavior

When doing a simple insert task on the database, the audit bundle loads all metadata and parses all entities to look for auditable entities in order to satisfy the condition "isAudited()" here :

foreach (array_reverse($uow->getScheduledEntityInsertions()) as $entity) {
            if ($this->provider->isAudited($entity)) {

This is particularly heavy on our projects, and a simple "insert" call on a "ping" API takes a lot of time, on an entity that isn't even audited.

How to reproduce

Have a project with many entities, create a non audited Entity, persist it, and monitor how long the flush takes, and especially the load function in DH\Auditor\Provider\Doctrine\Auditing\Annotation\AnnotationLoader, that should make up most of the "flush" time.

public function load(): array
    {
        $configuration = [];
        $metadatas = $this->entityManager->getMetadataFactory()->getAllMetadata();
        foreach ($metadatas as $metadata) {
            $config = $this->getEntityConfiguration($metadata);
            if (null !== $config) {
                $configuration[$metadata->getName()] = $config;
            }
        }
        return $configuration;
    }

Expected behavior

I would expect the metadatas to be cached like the ones from Doctrine if we set up the cache driver. In production mode, they shouldn't change. I'd make a pull request if I was sure that the problem is here and how to fix it "properly". (cache configuration ? load everything only if at least one of our entities is auditable ? ...)

DamienHarper commented 1 year ago

@mblotin-ledger this should be fixed with auditor 2.4.0 release so I close this issue, feel free to reopen if needed.