Commit 669c03d6 authored by Bill Schaller's avatar Bill Schaller

Improve structure of performance test case.

Add DbalPerformanceTestListener to collect & report performance test timing results.
parent 5dde3d0f
......@@ -46,9 +46,17 @@
<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>
......
......@@ -13,15 +13,29 @@ use Doctrine\Tests\DbalPerformanceTestCase;
*/
class TypeConversionPerformanceTest extends DbalPerformanceTestCase
{
public function testDateTimeTypeConversionPerformance100000Items()
/**
* @throws \Doctrine\DBAL\DBALException
* @dataProvider itemCountProvider
*/
public function testDateTimeTypeConversionPerformance($count)
{
$value = new \DateTime;
$start = microtime(true);
$type = Type::getType("datetime");
$platform = $this->_conn->getDatabasePlatform();
for ($i = 0; $i < 100000; $i++) {
$this->startTiming();
for ($i = 0; $i < $count; $i++) {
$type->convertToDatabaseValue($value, $platform);
}
echo __FUNCTION__ . " - " . (microtime(true) - $start) . " seconds" . PHP_EOL;
$this->stopTiming();
}
public function itemCountProvider()
{
return [
'100 items' => [100],
'1000 items' => [1000],
'10000 items' => [10000],
'100000 items' => [100000],
];
}
}
......@@ -3,60 +3,70 @@
namespace Doctrine\Tests;
/**
* Class DbalPerformanceTestCase
* 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
* @author robo
*/
class DbalPerformanceTestCase extends DbalFunctionalTestCase
{
/**
* @var integer
* time the test started
*
* @var float
*/
protected $maxRunningTime = 0;
private $startTime;
/**
* @return void
* elapsed run time of the last test
*
* @var float
*/
protected function runTest()
private $lastRunTime;
public function setUp()
{
$s = microtime(true);
parent::runTest();
$time = microtime(true) - $s;
if ($this->maxRunningTime != 0 && $time > $this->maxRunningTime) {
$this->fail(
sprintf(
'expected running time: <= %s but was: %s',
$this->maxRunningTime,
$time
)
);
parent::setUp();
// Reset timing vars
$this->startTime = $this->lastRunTime = null;
}
/**
* {@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->lastRunTime, "Test timing was stopped");
}
/**
* @param integer $maxRunningTime
*
* @return void
*
* @throws \InvalidArgumentException
* begin timing
*/
public function setMaxRunningTime($maxRunningTime)
protected function startTiming()
{
if (is_integer($maxRunningTime) && $maxRunningTime >= 0) {
$this->maxRunningTime = $maxRunningTime;
} else {
throw new \InvalidArgumentException;
$this->startTime = microtime(true);
}
/**
* end timing
*/
protected function stopTiming()
{
$this->lastRunTime = microtime(true) - $this->startTime;
}
/**
* @return integer
* @return float elapsed test execution time
*/
public function getMaxRunningTime()
public function getTime()
{
return $this->maxRunningTime;
return $this->lastRunTime;
}
}
<?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)
{
if ($test instanceof \Doctrine\Tests\DbalPerformanceTestCase)
{
$class = str_replace('Doctrine\Tests\DBAL\Performance\\', '', get_class($test));
$this->timings[$class . "::" . $test->getName(true)] = $test->getTime();
}
}
/**
* {@inheritdoc}
*/
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
{
if (!empty($this->timings)) {
print("\n");
foreach($this->timings as $test => $time) {
printf("%s: %6.3f\n", $test, $time);
}
$this->timings = [];
}
}
}
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