1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
<?php
namespace Doctrine\Tests\Common\Annotations;
use Doctrine\Common\Annotations\FileCacheReader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\DocLexer;
use Doctrine\Common\Annotations\DocParser;
use Doctrine\Common\Annotations\PhpParser;
use Doctrine\Common\Annotations\AnnotationReader;
require_once __DIR__ . '/Fixtures/Annotation/Route.php';
require_once __DIR__ . '/Fixtures/Annotation/Template.php';
require_once __DIR__ . '/Fixtures/Annotation/Secure.php';
require_once __DIR__ . '/Fixtures/SingleClassLOC1000.php';
class PerformanceTest extends \PHPUnit_Framework_TestCase
{
/**
* @group performance
*/
public function testCachedReadPerformanceWithInMemory()
{
$reader = new CachedReader(new AnnotationReader(), new ArrayCache());
$method = $this->getMethod();
$time = microtime(true);
for ($i=0,$c=500; $i<$c; $i++) {
$reader->getMethodAnnotations($method);
}
$time = microtime(true) - $time;
$this->printResults('cached reader (in-memory)', $time, $c);
}
/**
* @group performance
*/
public function testCachedReadPerformanceWithFileCache()
{
$method = $this->getMethod();
// prime cache
$reader = new FileCacheReader(new AnnotationReader(), sys_get_temp_dir());
$reader->getMethodAnnotations($method);
$time = microtime(true);
for ($i=0,$c=500; $i<$c; $i++) {
$reader = new FileCacheReader(new AnnotationReader(), sys_get_temp_dir());
$reader->getMethodAnnotations($method);
clearstatcache();
}
$time = microtime(true) - $time;
$this->printResults('cached reader (file)', $time, $c);
}
/**
* @group performance
*/
public function testReadPerformance()
{
$method = $this->getMethod();
$time = microtime(true);
for ($i=0,$c=150; $i<$c; $i++) {
$reader = new AnnotationReader();
$reader->getMethodAnnotations($method);
}
$time = microtime(true) - $time;
$this->printResults('reader', $time, $c);
}
/**
* @group performance
*/
public function testDocParsePerformance()
{
$imports = array(
'ignorephpdoc' => 'Annotations\Annotation\IgnorePhpDoc',
'ignoreannotation' => 'Annotations\Annotation\IgnoreAnnotation',
'route' => 'Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Route',
'template' => 'Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Template',
'__NAMESPACE__' => 'Doctrine\Tests\Common\Annotations\Fixtures',
);
$ignored = array(
'access', 'author', 'copyright', 'deprecated', 'example', 'ignore',
'internal', 'link', 'see', 'since', 'tutorial', 'version', 'package',
'subpackage', 'name', 'global', 'param', 'return', 'staticvar',
'static', 'var', 'throws', 'inheritdoc',
);
$method = $this->getMethod();
$methodComment = $method->getDocComment();
$classComment = $method->getDeclaringClass()->getDocComment();
$time = microtime(true);
for ($i=0,$c=200; $i<$c; $i++) {
$parser = new DocParser();
$parser->setImports($imports);
$parser->setIgnoredAnnotationNames($ignored);
$parser->setIgnoreNotImportedAnnotations(true);
$parser->parse($methodComment);
$parser->parse($classComment);
}
$time = microtime(true) - $time;
$this->printResults('doc-parser', $time, $c);
}
/**
* @group performance
*/
public function testDocLexerPerformance()
{
$method = $this->getMethod();
$methodComment = $method->getDocComment();
$classComment = $method->getDeclaringClass()->getDocComment();
$time = microtime(true);
for ($i=0,$c=500; $i<$c; $i++) {
$lexer = new DocLexer();
$lexer->setInput($methodComment);
$lexer->setInput($classComment);
}
$time = microtime(true) - $time;
$this->printResults('doc-lexer', $time, $c);
}
/**
* @group performance
*/
public function testPhpParserPerformanceWithShortCut()
{
$class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\NamespacedSingleClassLOC1000');
$time = microtime(true);
for ($i=0,$c=500; $i<$c; $i++) {
$parser = new PhpParser();
$parser->parseClass($class);
}
$time = microtime(true) - $time;
$this->printResults('doc-parser-with-short-cut', $time, $c);
}
/**
* @group performance
*/
public function testPhpParserPerformanceWithoutShortCut()
{
$class = new \ReflectionClass('SingleClassLOC1000');
$time = microtime(true);
for ($i=0,$c=500; $i<$c; $i++) {
$parser = new PhpParser();
$parser->parseClass($class);
}
$time = microtime(true) - $time;
$this->printResults('doc-parser-without-short-cut', $time, $c);
}
private function getMethod()
{
return new \ReflectionMethod('Doctrine\Tests\Common\Annotations\Fixtures\Controller', 'helloAction');
}
private function printResults($test, $time, $iterations)
{
if (0 == $iterations) {
throw new \InvalidArgumentException('$iterations cannot be zero.');
}
$title = $test." results:\n";
$iterationsText = sprintf("Iterations: %d\n", $iterations);
$totalTime = sprintf("Total Time: %.3f s\n", $time);
$iterationTime = sprintf("Time per iteration: %.3f ms\n", $time/$iterations * 1000);
$max = max(strlen($title), strlen($iterationTime)) - 1;
echo "\n".str_repeat('-', $max)."\n";
echo $title;
echo str_repeat('=', $max)."\n";
echo $iterationsText;
echo $totalTime;
echo $iterationTime;
echo str_repeat('-', $max)."\n";
}
}
|