-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '4.4.x' into add-psr6-compatibility
- Loading branch information
Showing
13 changed files
with
278 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\MongoDBBundle\APM; | ||
|
||
use Doctrine\ODM\MongoDB\APM\CommandLoggerInterface; | ||
use MongoDB\Driver\Monitoring\CommandFailedEvent; | ||
use MongoDB\Driver\Monitoring\CommandStartedEvent; | ||
use MongoDB\Driver\Monitoring\CommandSucceededEvent; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
use function MongoDB\Driver\Monitoring\addSubscriber; | ||
use function MongoDB\Driver\Monitoring\removeSubscriber; | ||
use function sprintf; | ||
|
||
final class StopwatchCommandLogger implements CommandLoggerInterface | ||
{ | ||
/** @var bool */ | ||
private $registered = false; | ||
|
||
/** @var Stopwatch|null */ | ||
private $stopwatch; | ||
|
||
public function __construct(?Stopwatch $stopwatch) | ||
{ | ||
$this->stopwatch = $stopwatch; | ||
} | ||
|
||
public function register(): void | ||
{ | ||
if ($this->stopwatch === null || $this->registered) { | ||
return; | ||
} | ||
|
||
$this->registered = true; | ||
addSubscriber($this); | ||
} | ||
|
||
public function unregister(): void | ||
{ | ||
if (! $this->registered) { | ||
return; | ||
} | ||
|
||
removeSubscriber($this); | ||
$this->registered = false; | ||
} | ||
|
||
public function commandStarted(CommandStartedEvent $event) | ||
{ | ||
if (! $this->stopwatch) { | ||
return; | ||
} | ||
|
||
$this->stopwatch->start(sprintf('mongodb_%s', $event->getRequestId()), 'doctrine_mongodb'); | ||
} | ||
|
||
public function commandSucceeded(CommandSucceededEvent $event) | ||
{ | ||
if (! $this->stopwatch) { | ||
return; | ||
} | ||
|
||
$this->stopwatch->stop(sprintf('mongodb_%s', $event->getRequestId())); | ||
} | ||
|
||
public function commandFailed(CommandFailedEvent $event) | ||
{ | ||
if (! $this->stopwatch) { | ||
return; | ||
} | ||
|
||
$this->stopwatch->stop(sprintf('mongodb_%s', $event->getRequestId())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\MongoDBBundle\Tests\APM; | ||
|
||
use Doctrine\Bundle\MongoDBBundle\APM\StopwatchCommandLogger; | ||
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Category; | ||
use Doctrine\Bundle\MongoDBBundle\Tests\TestCase; | ||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
class StopwatchCommandLoggerTest extends TestCase | ||
{ | ||
/** @var StopwatchCommandLogger */ | ||
private $commandLogger; | ||
/** @var Stopwatch */ | ||
private $stopwatch; | ||
/** @var DocumentManager */ | ||
private $dm; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->dm = TestCase::createTestDocumentManager(); | ||
|
||
$this->stopwatch = new Stopwatch(true); | ||
$this->commandLogger = new StopwatchCommandLogger($this->stopwatch); | ||
$this->commandLogger->register(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->commandLogger->unregister(); | ||
|
||
$this->dm->getDocumentCollection(Category::class)->drop(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testItLogsStopwatchEvents(): void | ||
{ | ||
$category = new Category('one'); | ||
|
||
$this->dm->persist($category); | ||
$this->dm->flush(); | ||
|
||
$this->dm->remove($category); | ||
$this->dm->flush(); | ||
|
||
$this->dm->getRepository(Category::class)->findAll(); | ||
$events = $this->stopwatch->getSectionEvents('__root__'); | ||
|
||
self::assertCount(3, $events); | ||
|
||
foreach ($events as $eventName => $stopwatchEvent) { | ||
// @todo replace with assertMatchesRegularExpression() when PHP 7.2 is dropped | ||
self::assertRegExp('/mongodb_\d+/', $eventName); | ||
self::assertGreaterThan(0, $stopwatchEvent->getDuration()); | ||
self::assertSame('doctrine_mongodb', $stopwatchEvent->getCategory()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.