Commit f44782e9 authored by Steve Müller's avatar Steve Müller

Merge pull request #870 from zeroedin-bill/performance-testcase

Add base performance testcase and initial TypeConversionPerformanceTest
parents c079d137 27efd866
......@@ -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>
\ No newline at end of file
<?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],
];
}
}
<?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;
}
}
<?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);
}
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment