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\Proxy\VirtualProxyInterface; |
27
|
|
|
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator; |
28
|
|
|
use ProxyManagerTestAsset\ClassWithMixedProperties; |
29
|
|
|
use ProxyManagerTestAsset\ClassWithPublicProperties; |
30
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
31
|
|
|
use ReflectionClass; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Benchmark that provides results for state access/initialization time for lazy loading value holder proxies |
35
|
|
|
* |
36
|
|
|
* @author Marco Pivetta <[email protected]> |
37
|
|
|
* @license MIT |
38
|
|
|
* |
39
|
|
|
* @BeforeMethods({"setUp"}) |
40
|
|
|
*/ |
41
|
|
|
class LazyLoadingValueHolderPropertyAccessBench |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var EmptyClass|VirtualProxyInterface |
45
|
|
|
*/ |
46
|
|
|
private $emptyClassProxy; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var EmptyClass|VirtualProxyInterface |
50
|
|
|
*/ |
51
|
|
|
private $initializedEmptyClassProxy; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var ClassWithPublicProperties|VirtualProxyInterface |
55
|
|
|
*/ |
56
|
|
|
private $publicPropertiesProxy; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var ClassWithPublicProperties|VirtualProxyInterface |
60
|
|
|
*/ |
61
|
|
|
private $initializedPublicPropertiesProxy; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var ClassWithMixedProperties|VirtualProxyInterface |
65
|
|
|
*/ |
66
|
|
|
private $mixedPropertiesProxy; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var ClassWithMixedProperties|VirtualProxyInterface |
70
|
|
|
*/ |
71
|
|
|
private $initializedMixedPropertiesProxy; |
72
|
|
|
|
73
|
|
|
public function setUp() |
74
|
|
|
{ |
75
|
|
|
$this->emptyClassProxy = $this->buildProxy(EmptyClass::class); |
76
|
|
|
$this->publicPropertiesProxy = $this->buildProxy(ClassWithPublicProperties::class); |
77
|
|
|
$this->mixedPropertiesProxy = $this->buildProxy(ClassWithMixedProperties::class); |
78
|
|
|
|
79
|
|
|
$this->initializedEmptyClassProxy = $this->buildProxy(EmptyClass::class); |
80
|
|
|
$this->initializedPublicPropertiesProxy = $this->buildProxy(ClassWithPublicProperties::class); |
81
|
|
|
$this->initializedMixedPropertiesProxy = $this->buildProxy(ClassWithMixedProperties::class); |
82
|
|
|
|
83
|
|
|
$this->initializedEmptyClassProxy->initializeProxy(); |
84
|
|
|
$this->initializedPublicPropertiesProxy->initializeProxy(); |
85
|
|
|
$this->initializedMixedPropertiesProxy->initializeProxy(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function benchEmptyClassInitialization() : void |
89
|
|
|
{ |
90
|
|
|
$this->emptyClassProxy->initializeProxy(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function benchInitializedEmptyClassInitialization() : void |
94
|
|
|
{ |
95
|
|
|
$this->initializedEmptyClassProxy->initializeProxy(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function benchObjectWithPublicPropertiesInitialization() : void |
99
|
|
|
{ |
100
|
|
|
$this->publicPropertiesProxy->initializeProxy(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function benchInitializedObjectWithPublicPropertiesInitialization() : void |
104
|
|
|
{ |
105
|
|
|
$this->initializedPublicPropertiesProxy->initializeProxy(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function benchObjectWithPublicPropertiesPropertyRead() : void |
109
|
|
|
{ |
110
|
|
|
$this->publicPropertiesProxy->property0; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function benchInitializedObjectWithPublicPropertiesPropertyRead() : void |
114
|
|
|
{ |
115
|
|
|
$this->initializedPublicPropertiesProxy->property0; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function benchObjectWithPublicPropertiesPropertyWrite() : void |
119
|
|
|
{ |
120
|
|
|
$this->publicPropertiesProxy->property0 = 'foo'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function benchInitializedObjectWithPublicPropertiesPropertyWrite() : void |
124
|
|
|
{ |
125
|
|
|
$this->initializedPublicPropertiesProxy->property0 = 'foo'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function benchObjectWithPublicPropertiesPropertyIsset() : void |
129
|
|
|
{ |
130
|
|
|
/* @noinspection PhpExpressionResultUnusedInspection */ |
131
|
|
|
/* @noinspection UnSafeIsSetOverArrayInspection */ |
132
|
|
|
isset($this->publicPropertiesProxy->property0); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function benchInitializedObjectWithPublicPropertiesPropertyIsset() : void |
136
|
|
|
{ |
137
|
|
|
/* @noinspection PhpExpressionResultUnusedInspection */ |
138
|
|
|
/* @noinspection UnSafeIsSetOverArrayInspection */ |
139
|
|
|
isset($this->initializedPublicPropertiesProxy->property0); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function benchObjectWithPublicPropertiesPropertyUnset() : void |
143
|
|
|
{ |
144
|
|
|
unset($this->publicPropertiesProxy->property0); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function benchInitializedObjectWithPublicPropertiesPropertyUnset() : void |
148
|
|
|
{ |
149
|
|
|
unset($this->initializedPublicPropertiesProxy->property0); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function benchObjectWithMixedPropertiesInitialization() : void |
153
|
|
|
{ |
154
|
|
|
$this->mixedPropertiesProxy->initializeProxy(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function benchInitializedObjectWithMixedPropertiesInitialization() : void |
158
|
|
|
{ |
159
|
|
|
$this->initializedMixedPropertiesProxy->initializeProxy(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function benchObjectWithMixedPropertiesPropertyRead() : void |
163
|
|
|
{ |
164
|
|
|
$this->mixedPropertiesProxy->publicProperty0; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function benchInitializedObjectWithMixedPropertiesPropertyRead() : void |
168
|
|
|
{ |
169
|
|
|
$this->initializedMixedPropertiesProxy->publicProperty0; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function benchObjectWithMixedPropertiesPropertyWrite() : void |
173
|
|
|
{ |
174
|
|
|
$this->mixedPropertiesProxy->publicProperty0 = 'foo'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function benchInitializedObjectWithMixedPropertiesPropertyWrite() : void |
178
|
|
|
{ |
179
|
|
|
$this->initializedMixedPropertiesProxy->publicProperty0 = 'foo'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function benchObjectWithMixedPropertiesPropertyIsset() : void |
183
|
|
|
{ |
184
|
|
|
/* @noinspection PhpExpressionResultUnusedInspection */ |
185
|
|
|
/* @noinspection UnSafeIsSetOverArrayInspection */ |
186
|
|
|
isset($this->mixedPropertiesProxy->publicProperty0); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function benchInitializedObjectWithMixedPropertiesPropertyIsset() : void |
190
|
|
|
{ |
191
|
|
|
/* @noinspection PhpExpressionResultUnusedInspection */ |
192
|
|
|
/* @noinspection UnSafeIsSetOverArrayInspection */ |
193
|
|
|
isset($this->initializedMixedPropertiesProxy->publicProperty0); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function benchObjectWithMixedPropertiesPropertyUnset() : void |
197
|
|
|
{ |
198
|
|
|
unset($this->mixedPropertiesProxy->publicProperty0); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function benchInitializedObjectWithMixedPropertiesPropertyUnset() : void |
202
|
|
|
{ |
203
|
|
|
unset($this->initializedMixedPropertiesProxy->publicProperty0); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
private function buildProxy(string $originalClass) : VirtualProxyInterface |
207
|
|
|
{ |
208
|
|
|
return ($this->generateProxyClass($originalClass))::staticProxyConstructor( |
209
|
|
|
function ( |
210
|
|
|
& $valueHolder, |
211
|
|
|
VirtualProxyInterface $proxy, |
212
|
|
|
string $method, |
213
|
|
|
$params, |
214
|
|
|
& $initializer |
215
|
|
|
) use ($originalClass) : bool { |
216
|
|
|
$initializer = null; |
217
|
|
|
$valueHolder = new $originalClass(); |
218
|
|
|
|
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
private function generateProxyClass(string $originalClassName) : string |
225
|
|
|
{ |
226
|
|
|
$generatedClassName = __CLASS__ . '\\' . $originalClassName; |
227
|
|
|
|
228
|
|
|
if (class_exists($generatedClassName)) { |
229
|
|
|
return $generatedClassName; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$generatedClass = new ClassGenerator($generatedClassName); |
233
|
|
|
|
234
|
|
|
(new LazyLoadingValueHolderGenerator())->generate(new ReflectionClass($originalClassName), $generatedClass); |
235
|
|
|
(new EvaluatingGeneratorStrategy())->generate($generatedClass); |
236
|
|
|
|
237
|
|
|
return $generatedClassName; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|