1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace ProxyManagerBench; |
22
|
|
|
|
23
|
|
|
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods; |
24
|
|
|
use ProxyManager\Generator\ClassGenerator; |
25
|
|
|
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy; |
26
|
|
|
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator; |
27
|
|
|
use ProxyManagerTestAsset\ClassWithMixedProperties; |
28
|
|
|
use ProxyManagerTestAsset\ClassWithPrivateProperties; |
29
|
|
|
use ProxyManagerTestAsset\ClassWithProtectedProperties; |
30
|
|
|
use ProxyManagerTestAsset\ClassWithPublicProperties; |
31
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
32
|
|
|
use ReflectionClass; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Benchmark that provides results for simple object instantiation for lazy loading value holder proxies |
36
|
|
|
* |
37
|
|
|
* @author Marco Pivetta <[email protected]> |
38
|
|
|
* @license MIT |
39
|
|
|
* |
40
|
|
|
* @BeforeMethods({"setUp"}) |
41
|
|
|
*/ |
42
|
|
|
class LazyLoadingValueHolderInstantiationBench |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $emptyClassProxy; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $privatePropertiesProxy; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $protectedPropertiesProxy; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $publicPropertiesProxy; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $mixedPropertiesProxy; |
68
|
|
|
|
69
|
|
|
public function setUp() |
70
|
|
|
{ |
71
|
|
|
$this->emptyClassProxy = $this->generateProxy(EmptyClass::class); |
72
|
|
|
$this->privatePropertiesProxy = $this->generateProxy(ClassWithPrivateProperties::class); |
73
|
|
|
$this->protectedPropertiesProxy = $this->generateProxy(ClassWithProtectedProperties::class); |
74
|
|
|
$this->publicPropertiesProxy = $this->generateProxy(ClassWithPublicProperties::class); |
75
|
|
|
$this->mixedPropertiesProxy = $this->generateProxy(ClassWithMixedProperties::class); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function benchOriginalConstructorInstantiationOfEmptyObject() : void |
79
|
|
|
{ |
80
|
|
|
new $this->emptyClassProxy; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function benchInstantiationOfEmptyObject() : void |
84
|
|
|
{ |
85
|
|
|
($this->emptyClassProxy)::staticProxyConstructor(function () { |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function benchOriginalConstructorInstantiationOfObjectWithPrivateProperties() : void |
90
|
|
|
{ |
91
|
|
|
new $this->privatePropertiesProxy; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function benchInstantiationOfObjectWithPrivateProperties() : void |
95
|
|
|
{ |
96
|
|
|
($this->privatePropertiesProxy)::staticProxyConstructor(function () { |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function benchOriginalConstructorInstantiationOfObjectWithProtectedProperties() : void |
101
|
|
|
{ |
102
|
|
|
new $this->protectedPropertiesProxy; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function benchInstantiationOfObjectWithProtectedProperties() : void |
106
|
|
|
{ |
107
|
|
|
($this->protectedPropertiesProxy)::staticProxyConstructor(function () { |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function benchOriginalConstructorInstantiationOfObjectWithPublicProperties() : void |
112
|
|
|
{ |
113
|
|
|
new $this->publicPropertiesProxy; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function benchInstantiationOfObjectWithPublicProperties() : void |
117
|
|
|
{ |
118
|
|
|
($this->publicPropertiesProxy)::staticProxyConstructor(function () { |
119
|
|
|
}); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function benchOriginalConstructorInstantiationOfObjectWithMixedProperties() : void |
123
|
|
|
{ |
124
|
|
|
new $this->mixedPropertiesProxy; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function benchInstantiationOfObjectWithMixedProperties() : void |
128
|
|
|
{ |
129
|
|
|
($this->mixedPropertiesProxy)::staticProxyConstructor(function () { |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function generateProxy(string $originalClass) : string |
134
|
|
|
{ |
135
|
|
|
$generatedClassName = __CLASS__ . '\\' . $originalClass; |
136
|
|
|
|
137
|
|
|
if (class_exists($generatedClassName)) { |
138
|
|
|
return $generatedClassName; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$generatedClass = new ClassGenerator($generatedClassName); |
142
|
|
|
|
143
|
|
|
(new LazyLoadingValueHolderGenerator())->generate(new ReflectionClass($originalClass), $generatedClass); |
144
|
|
|
(new EvaluatingGeneratorStrategy())->generate($generatedClass); |
145
|
|
|
|
146
|
|
|
return $generatedClassName; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|