-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #870 from zeroedin-bill/performance-testcase
Add base performance testcase and initial TypeConversionPerformanceTest
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 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
41 changes: 41 additions & 0 deletions
41
tests/Doctrine/Tests/DBAL/Performance/TypeConversionPerformanceTest.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,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], | ||
]; | ||
} | ||
} |
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 | ||
|
||
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; | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} | ||
} | ||
} |