Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic collector for ZDT (using a simple DBAL SQLLogger)
- Loading branch information
Showing
7 changed files
with
294 additions
and
5 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
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,115 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace DoctrineORMModule\Collector; | ||
|
||
use ZendDeveloperTools\Collector\CollectorInterface; | ||
use ZendDeveloperTools\Collector\AutoHideInterface; | ||
|
||
use Zend\Mvc\MvcEvent; | ||
|
||
use Doctrine\DBAL\Logging\DebugStack; | ||
|
||
/** | ||
* Collector to be used in ZendDeveloperTools to record and display SQL queries | ||
* | ||
* @license MIT | ||
* @link www.doctrine-project.org | ||
* @author Marco Pivetta <ocramius@gmail.com> | ||
*/ | ||
class SQLLoggerCollector implements CollectorInterface, AutoHideInterface | ||
{ | ||
/** | ||
* Collector priority | ||
*/ | ||
const PRIORITY = 10; | ||
|
||
/** | ||
* @var DebugStack | ||
*/ | ||
protected $sqlLogger; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* @param DebugStack $logger | ||
* @param string $name | ||
*/ | ||
public function __construct(DebugStack $sqlLogger, $name) | ||
{ | ||
$this->sqlLogger = $sqlLogger; | ||
$this->name = (string) $name; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getPriority() | ||
{ | ||
return static::PRIORITY; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function collect(MvcEvent $mvcEvent) | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function canHide() | ||
{ | ||
return empty($this->sqlLogger->queries); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getQueryCount() | ||
{ | ||
return count($this->sqlLogger->queries); | ||
} | ||
|
||
/** | ||
* @return float | ||
*/ | ||
public function getQueryTime() | ||
{ | ||
$time = 0.0; | ||
|
||
foreach ($this->sqlLogger->queries as $query) { | ||
$time += $query['executionMS']; | ||
} | ||
|
||
return $time; | ||
} | ||
} |
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
102 changes: 102 additions & 0 deletions
102
tests/DoctrineORMModuleTest/Collector/SQLLoggerCollectorTest.php
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,102 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace DoctrineORMModuleTest\Paginator; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use DoctrineORMModule\Collector\SQLLoggerCollector; | ||
use Doctrine\DBAL\Logging\DebugStack; | ||
use Zend\Mvc\MvcEvent; | ||
|
||
|
||
class SQLLoggerCollectorTest extends TestCase | ||
{ | ||
/** | ||
* @var DebugStack | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $name = 'test-collector-name'; | ||
|
||
/** | ||
* @var SQLLoggerCollector | ||
*/ | ||
protected $collector; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->logger = new DebugStack(); | ||
$this->collector = new SQLLoggerCollector($this->logger, $this->name); | ||
} | ||
|
||
public function testHasCorrectName() | ||
{ | ||
$this->assertSame($this->name, $this->collector->getName()); | ||
} | ||
|
||
public function testGetPriority() | ||
{ | ||
$this->assertInternalType('int', $this->collector->getPriority()); | ||
} | ||
|
||
public function testCollect() | ||
{ | ||
$this->collector->collect(new MvcEvent()); | ||
} | ||
|
||
public function testCanHide() | ||
{ | ||
$this->assertTrue($this->collector->canHide()); | ||
$this->logger->startQuery('some sql'); | ||
$this->assertFalse($this->collector->canHide()); | ||
} | ||
|
||
public function testGetQueryCount() | ||
{ | ||
$this->assertSame(0, $this->collector->getQueryCount()); | ||
$this->logger->startQuery('some sql'); | ||
$this->assertSame(1, $this->collector->getQueryCount()); | ||
$this->logger->startQuery('some more sql'); | ||
$this->assertSame(2, $this->collector->getQueryCount()); | ||
} | ||
|
||
public function testGetQueryTime() | ||
{ | ||
$start = microtime(true); | ||
$this->assertEquals(0, $this->collector->getQueryTime()); | ||
|
||
$this->logger->startQuery('some sql'); | ||
$this->logger->stopQuery(); | ||
$time = microtime(true) - $start; | ||
$time1 = $this->collector->getQueryTime(); | ||
$this->assertGreaterThan(0, $time1); | ||
$this->assertLessThan($time, $time1); | ||
|
||
$this->logger->startQuery('some more sql'); | ||
$this->logger->stopQuery(); | ||
$time = microtime(true) - $start; | ||
$time2 = $this->collector->getQueryTime(); | ||
$this->assertGreaterThan($time1, $time2); | ||
$this->assertLessThan($time, $time1); | ||
} | ||
} |
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,27 @@ | ||
<div class="zdt-toolbar-entry"> | ||
<div class="zdt-toolbar-preview"> | ||
<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6NzU1MkQ0QURDODkyMTFFMUE4RDU5MDZBOEM2RTM5QjYiIHhtcE1NOkRvY3VtZW50S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ZOBcAacm4DZnT96PxWLJF9n9/f3vWKRnJqfApepMBclUlFTb8BcXF8Xg6oJGozn1LwWhWFtbO2hra/sE3cMODQ3JQajVbDYbMEb5VVVVcsykKDs7m/3TlOzu7obQcr6xsbFdaHtPT88hikSY4eHh9xiZS36//2lHR8fy9PR0eSAQUGMUZYhcAM6SteAmhsFebHt7OwyqPKWlpZ+bmpqCRqPxDujRmUwmhsUmU1hYSNCDD1tbW2kKbmzO+Xy+yc3NTS8chPh8Pg/2BP3rYExlSqVSKxQK9XjgRorXSCSSrCyLkYlyVU6JGgfUiI5Q/E7S7hD6KNU88DeJRj2kB/4HfX19X5OO4cxptVrVaNQcCF8mk2WftLoY2xA43+ns7HTabLYn8LfEjI+Pk5qaGiEW15B+FQi+jCKJ8EcWw3kWUmGOp4biRcFtEL87v9PpXMEIvoP5NffLIz8FGAAqeUQbl180BwAAAABJRU5ErkJggg==" alt="Database (Zend\Db)"> | ||
<span class="zdt-toolbar-info"> | ||
<?php echo $this->collector->getQueryCount(); ?> | ||
queries in | ||
<?php echo $this->ZDT_Time($this->collector->getQueryTime()); ?> s | ||
</span> | ||
</div> | ||
<div class="zdt-toolbar-detail"> | ||
<span class="zdt-toolbar-info zdt-toolbar-info-heading">Details</span> | ||
<span class="zdt-toolbar-info"> | ||
<span class="zdt-detail-label">Help us!</span> | ||
<span class="zdt-detail-value zdt-detail-value-right"> | ||
Please help us improve | ||
<br/> | ||
profiling by forking and | ||
<br/> | ||
contributing to | ||
<br/> | ||
<a href="https://github.com/doctrine/DoctrineORMModule/fork_select" target="_blank"> | ||
DoctrineORMModule | ||
</a> | ||
</span> | ||
</span> | ||
</div> | ||
</div> |