Skip to content

Commit

Permalink
Profiler Extension (#19)
Browse files Browse the repository at this point in the history
* initial work on a profiler extension

* sort service definition alphabetically

* fix php code style

* fixed type annotations

* removed obsolete ProfilerDisabler

* added test for FixtureReferenceResolver

* cs fix

* Apply suggestions from code review

Co-authored-by: Jacob Dreesen <[email protected]>

* load profiler extension only when in dev env

---------

Co-authored-by: Luka Dschaak <[email protected]>
Co-authored-by: Jacob Dreesen <[email protected]>
  • Loading branch information
3 people authored Jul 2, 2024
1 parent 5c080fa commit e49bd1e
Show file tree
Hide file tree
Showing 17 changed files with 629 additions and 53 deletions.
21 changes: 13 additions & 8 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ services:
Neusta\Pimcore\FixtureBundle\FixtureLoader\:
resource: '../src/FixtureLoader/*'

Neusta\Pimcore\FixtureBundle\FixtureLoader\FixtureLoader:
arguments:
$fixtureLocator: '@Neusta\Pimcore\FixtureBundle\Locator\AllFixturesLocator'

Neusta\Pimcore\FixtureBundle\FixtureLoader\SelectiveFixtureLoader:
arguments:
$fixtureLocator: '@Neusta\Pimcore\FixtureBundle\Locator\NamedFixtureLocator'

Neusta\Pimcore\FixtureBundle\Helper\AssetHelper:
arguments:
$prefix: !abstract defined in extension
Expand All @@ -31,15 +39,12 @@ services:

Neusta\Pimcore\FixtureBundle\Locator\:
resource: '../src/Locator/*'

Neusta\Pimcore\FixtureBundle\FixtureLoader\FixtureLoader:
arguments:
$fixtureLocator: '@Neusta\Pimcore\FixtureBundle\Locator\AllFixturesLocator'

Neusta\Pimcore\FixtureBundle\FixtureLoader\SelectiveFixtureLoader:
arguments:
$fixtureLocator: '@Neusta\Pimcore\FixtureBundle\Locator\NamedFixtureLocator'
public: true

Neusta\Pimcore\FixtureBundle\ReferenceRepository\:
resource: '../src/ReferenceRepository/*'

when@dev:
services:
Neusta\Pimcore\FixtureBundle\Profiler\:
resource: '../src/Profiler/*'
7 changes: 0 additions & 7 deletions src/EventListener/PimcoreLoadOptimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ final class PimcoreLoadOptimization implements EventSubscriberInterface
private bool $versionEnabled;
private bool $cacheEnabled;

public function __construct(
private readonly ProfilerDisabler $profilerDisabler,
) {
}

public static function getSubscribedEvents(): array
{
return [
Expand All @@ -41,8 +36,6 @@ public function beforeCommand(): void

$this->originalSqlLogger = Db::getConnection()->getConfiguration()->getSQLLogger();
Db::getConnection()->getConfiguration()->setSQLLogger(null);

$this->profilerDisabler->disable();
}

public function afterCommand(): void
Expand Down
38 changes: 0 additions & 38 deletions src/EventListener/ProfilerDisabler.php

This file was deleted.

56 changes: 56 additions & 0 deletions src/Profiler/DataCollector/FixtureLoaderCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

namespace Neusta\Pimcore\FixtureBundle\Profiler\DataCollector;

use Neusta\Pimcore\FixtureBundle\Profiler\FixtureReference\FixtureReference;
use Neusta\Pimcore\FixtureBundle\Profiler\FixtureReference\FixtureReferenceResolver;
use Neusta\Pimcore\FixtureBundle\Profiler\PerformanceInfo\PerformanceInfo;
use Neusta\Pimcore\FixtureBundle\Profiler\Timing\TimingCollector;
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @internal
*/
final class FixtureLoaderCollector extends AbstractDataCollector
{
public function __construct(
private readonly FixtureReferenceResolver $fixtureReferenceResolver,
private readonly TimingCollector $timingCollector,
) {
}

public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
$this->data['timings'] = $this->timingCollector->getAll();

$this->data['references'] = $this->fixtureReferenceResolver->getRootReferences();
}

public static function getTemplate(): ?string
{
return '@NeustaPimcoreFixture/data_collector/template.html.twig';
}

/**
* @return list<FixtureReference>
*/
public function getDependencyFreeFixtures(): array
{
return $this->data['references'];
}

public function getTiming(FixtureReference $fixtureReference): PerformanceInfo
{
return $this->data['timings'][$fixtureReference->getName()];
}

/**
* @return array<class-string, PerformanceInfo>
*/
public function getTimings(): array
{
return $this->data['timings'];
}
}
41 changes: 41 additions & 0 deletions src/Profiler/EventListener/FixtureDependencyGraphSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace Neusta\Pimcore\FixtureBundle\Profiler\EventListener;

use Neusta\Pimcore\FixtureBundle\Event\AfterLoadFixtures;
use Neusta\Pimcore\FixtureBundle\Event\BeforeExecuteFixture;
use Neusta\Pimcore\FixtureBundle\Fixture\Fixture;
use Neusta\Pimcore\FixtureBundle\Profiler\FixtureReference\FixtureReferenceResolver;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @internal
*/
final class FixtureDependencyGraphSubscriber implements EventSubscriberInterface
{
/** @var list<Fixture> */
private array $loadedFixtures = [];

public function __construct(
private readonly FixtureReferenceResolver $fixtureReferenceResolver,
) {
}

public static function getSubscribedEvents(): array
{
return [
BeforeExecuteFixture::class => ['beforeExecuteFixture', 10],
AfterLoadFixtures::class => 'afterLoadFixtures',
];
}

public function beforeExecuteFixture(BeforeExecuteFixture $event): void
{
$this->loadedFixtures[] = $event->fixture;
}

public function afterLoadFixtures(): void
{
$this->fixtureReferenceResolver->setFixtures($this->loadedFixtures);
}
}
41 changes: 41 additions & 0 deletions src/Profiler/EventListener/TimingSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace Neusta\Pimcore\FixtureBundle\Profiler\EventListener;

use Neusta\Pimcore\FixtureBundle\Event\AfterExecuteFixture;
use Neusta\Pimcore\FixtureBundle\Event\BeforeExecuteFixture;
use Neusta\Pimcore\FixtureBundle\Profiler\Timing\Timing;
use Neusta\Pimcore\FixtureBundle\Profiler\Timing\TimingCollector;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @internal
*/
final class TimingSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly Timing $timing,
private readonly TimingCollector $timingCollector,
) {
}

public static function getSubscribedEvents(): array
{
return [
BeforeExecuteFixture::class => 'beforeExecuteFixture',
AfterExecuteFixture::class => 'afterExecuteFixture',
];
}

public function beforeExecuteFixture(BeforeExecuteFixture $event): void
{
$this->timing->beforeExecuteFixture($event->fixture);
}

public function afterExecuteFixture(AfterExecuteFixture $event): void
{
$this->timing->afterExecuteFixture($event->fixture);

$this->timingCollector->add($event->fixture, $this->timing->getPerformanceInfo($event->fixture));
}
}
69 changes: 69 additions & 0 deletions src/Profiler/FixtureReference/FixtureReference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types=1);

namespace Neusta\Pimcore\FixtureBundle\Profiler\FixtureReference;

use Neusta\Pimcore\FixtureBundle\Fixture\Fixture;

/**
* @internal
*/
final class FixtureReference
{
/** @var class-string */
private string $fixtureFqcn;

/** @var list<FixtureReference> */
private array $dependencies = [];

/** @var list<FixtureReference> */
private array $dependants = [];

public function __construct(Fixture $fixture)
{
$this->fixtureFqcn = $fixture::class;
}

/**
* @return class-string
*/
public function getName(): string
{
return $this->fixtureFqcn;
}

public function addDependencyReference(self $dependency): void
{
$this->dependencies[] = $dependency;
}

public function addDependantReference(self $fixtureReference): void
{
$this->dependants[] = $fixtureReference;
}

/**
* @return list<FixtureReference>
*/
public function getDependencies(): array
{
return $this->dependencies;
}

public function hasDependencies(): bool
{
return !empty($this->dependencies);
}

/**
* @return list<FixtureReference>
*/
public function getDependants(): array
{
return $this->dependants;
}

public function hasDependants(): bool
{
return !empty($this->dependants);
}
}
Loading

0 comments on commit e49bd1e

Please sign in to comment.