Passed
Push — drop-CachedReader ( d857ce...aa721e )
by Michael
02:14
created

testCachedReadPerformanceWithInMemory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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