Skip to content

Commit

Permalink
Merge pull request #870 from zeroedin-bill/performance-testcase
Browse files Browse the repository at this point in the history
Add base performance testcase and initial TypeConversionPerformanceTest
  • Loading branch information
deeky666 committed Sep 11, 2015
2 parents c079d13 + 27efd86 commit f44782e
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@
<testsuites>
<testsuite name="Doctrine DBAL Test Suite">
<directory>./tests/Doctrine/Tests/DBAL</directory>
<exclude>./tests/Doctrine/Tests/DBAL/Performance</exclude>
</testsuite>
<testsuite name="Doctrine DBAL Performance Test Suite">
<directory>./tests/Doctrine/Tests/DBAL/Performance</directory>
</testsuite>
</testsuites>

<listeners>
<listener class="Doctrine\Tests\DbalPerformanceTestListener"/>
</listeners>

<groups>
<exclude>
<group>performance</group>
</exclude>
</groups>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Doctrine\Tests\DBAL\Performance;

use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DbalPerformanceTestCase;

/**
* Class TypeConversionPerformanceTest
* @package Doctrine\Tests\DBAL\Performance
* @author Bill Schaller
* @group performance
*/
class TypeConversionPerformanceTest extends DbalPerformanceTestCase
{
/**
* @throws \Doctrine\DBAL\DBALException
* @dataProvider itemCountProvider
*/
public function testDateTimeTypeConversionPerformance($count)
{
$value = new \DateTime;
$type = Type::getType("datetime");
$platform = $this->_conn->getDatabasePlatform();
$this->startTiming();
for ($i = 0; $i < $count; $i++) {
$type->convertToDatabaseValue($value, $platform);
}
$this->stopTiming();
}

public function itemCountProvider()
{
return [
'100 items' => [100],
'1000 items' => [1000],
'10000 items' => [10000],
'100000 items' => [100000],
];
}
}
64 changes: 64 additions & 0 deletions tests/Doctrine/Tests/DbalPerformanceTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Doctrine\Tests;

/**
* Base class for all DBAL performance tests.
*
* Tests implemented in this class must call startTiming at the beginning
* and stopTiming at the end of all tests. Tests that do not start or stop
* timing will fail.
*
* @package Doctrine\Tests\DBAL
* @author Bill Schaller
*/
class DbalPerformanceTestCase extends DbalFunctionalTestCase
{
/**
* time the test started
*
* @var float
*/
private $startTime;

/**
* elapsed run time of the last test
*
* @var float
*/
private $runTime;

/**
* {@inheritdoc}
*/
protected function assertPostConditions()
{
// If a perf test doesn't start or stop, it fails.
$this->assertNotNull($this->startTime, "Test timing was started");
$this->assertNotNull($this->runTime, "Test timing was stopped");
}

/**
* begin timing
*/
protected function startTiming()
{
$this->startTime = microtime(true);
}

/**
* end timing
*/
protected function stopTiming()
{
$this->runTime = microtime(true) - $this->startTime;
}

/**
* @return float elapsed test execution time
*/
public function getTime()
{
return $this->runTime;
}
}
54 changes: 54 additions & 0 deletions tests/Doctrine/Tests/DbalPerformanceTestListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Doctrine\Tests;

/**
* Listener for collecting and reporting results of performance tests
*
* @author Bill Schaller
*/
class DbalPerformanceTestListener extends \PHPUnit_Framework_BaseTestListener
{
private $timings = [];

/**
* {@inheritdoc}
*/
public function endTest(\PHPUnit_Framework_Test $test, $time)
{
// This listener only applies to performance tests.
if ($test instanceof \Doctrine\Tests\DbalPerformanceTestCase)
{
// we identify perf tests by class, method, and dataset
$class = str_replace('Doctrine\Tests\DBAL\Performance\\', '', get_class($test));

if (!isset($this->timings[$class])) {
$this->timings[$class] = [];
}

// Store timing data for each test in the order they were run.
$this->timings[$class][$test->getName(true)] = $test->getTime();
}
}

/**
* Report performance test timings.
*
* Note: __destruct is used here because PHPUnit doesn't have a
* 'All tests over' hook.
*/
public function __destruct()
{
if (!empty($this->timings)) {
// Report timings.
print("\nPerformance test results:\n\n");

foreach($this->timings as $class => $tests) {
printf("%s:\n", $class);
foreach($tests as $test => $time) {
printf("\t%s: %.3f seconds\n", $test, $time);
}
}
}
}
}

0 comments on commit f44782e

Please sign in to comment.