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\GhostObjectInterface; |
27
|
|
|
use ProxyManager\Proxy\LazyLoadingInterface; |
28
|
|
|
use ProxyManager\ProxyGenerator\LazyLoadingGhostGenerator; |
29
|
|
|
use ProxyManagerTestAsset\ClassWithMixedProperties; |
30
|
|
|
use ProxyManagerTestAsset\ClassWithPrivateProperties; |
31
|
|
|
use ProxyManagerTestAsset\ClassWithProtectedProperties; |
32
|
|
|
use ProxyManagerTestAsset\ClassWithPublicProperties; |
33
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
34
|
|
|
use ReflectionClass; |
35
|
|
|
use ReflectionProperty; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Benchmark that provides results for simple initialization/state access for lazy loading ghost proxies |
39
|
|
|
* |
40
|
|
|
* @author Marco Pivetta <[email protected]> |
41
|
|
|
* @license MIT |
42
|
|
|
* |
43
|
|
|
* @BeforeMethods({"setUp"}) |
44
|
|
|
*/ |
45
|
|
|
class LazyLoadingGhostPropertyAccessBench |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @var EmptyClass|GhostObjectInterface |
49
|
|
|
*/ |
50
|
|
|
private $emptyClassProxy; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var EmptyClass|GhostObjectInterface |
54
|
|
|
*/ |
55
|
|
|
private $initializedEmptyClassProxy; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ClassWithPrivateProperties|GhostObjectInterface |
59
|
|
|
*/ |
60
|
|
|
private $privatePropertiesProxy; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var ClassWithPrivateProperties|GhostObjectInterface |
64
|
|
|
*/ |
65
|
|
|
private $initializedPrivatePropertiesProxy; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var ReflectionProperty |
69
|
|
|
*/ |
70
|
|
|
private $accessPrivateProperty; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var ClassWithProtectedProperties|GhostObjectInterface |
74
|
|
|
*/ |
75
|
|
|
private $protectedPropertiesProxy; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var ClassWithProtectedProperties|GhostObjectInterface |
79
|
|
|
*/ |
80
|
|
|
private $initializedProtectedPropertiesProxy; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var ReflectionProperty |
84
|
|
|
*/ |
85
|
|
|
private $accessProtectedProperty; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var ClassWithPublicProperties|GhostObjectInterface |
89
|
|
|
*/ |
90
|
|
|
private $publicPropertiesProxy; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var ClassWithPublicProperties|GhostObjectInterface |
94
|
|
|
*/ |
95
|
|
|
private $initializedPublicPropertiesProxy; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var ClassWithMixedProperties|GhostObjectInterface |
99
|
|
|
*/ |
100
|
|
|
private $mixedPropertiesProxy; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var ClassWithMixedProperties|GhostObjectInterface |
104
|
|
|
*/ |
105
|
|
|
private $initializedMixedPropertiesProxy; |
106
|
|
|
|
107
|
|
|
public function setUp() |
108
|
|
|
{ |
109
|
|
|
$this->emptyClassProxy = $this->buildProxy(EmptyClass::class); |
110
|
|
|
$this->privatePropertiesProxy = $this->buildProxy(ClassWithPrivateProperties::class); |
|
|
|
|
111
|
|
|
$this->protectedPropertiesProxy = $this->buildProxy(ClassWithProtectedProperties::class); |
|
|
|
|
112
|
|
|
$this->publicPropertiesProxy = $this->buildProxy(ClassWithPublicProperties::class); |
|
|
|
|
113
|
|
|
$this->mixedPropertiesProxy = $this->buildProxy(ClassWithMixedProperties::class); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$this->initializedEmptyClassProxy = $this->buildProxy(EmptyClass::class); |
116
|
|
|
$this->initializedPrivatePropertiesProxy = $this->buildProxy(ClassWithPrivateProperties::class); |
|
|
|
|
117
|
|
|
$this->initializedProtectedPropertiesProxy = $this->buildProxy(ClassWithProtectedProperties::class); |
|
|
|
|
118
|
|
|
$this->initializedPublicPropertiesProxy = $this->buildProxy(ClassWithPublicProperties::class); |
|
|
|
|
119
|
|
|
$this->initializedMixedPropertiesProxy = $this->buildProxy(ClassWithMixedProperties::class); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$this->initializedEmptyClassProxy->initializeProxy(); |
122
|
|
|
$this->initializedPrivatePropertiesProxy->initializeProxy(); |
123
|
|
|
$this->initializedProtectedPropertiesProxy->initializeProxy(); |
124
|
|
|
$this->initializedPublicPropertiesProxy->initializeProxy(); |
125
|
|
|
$this->initializedMixedPropertiesProxy->initializeProxy(); |
126
|
|
|
|
127
|
|
|
$this->accessPrivateProperty = new ReflectionProperty(ClassWithPrivateProperties::class, 'property0'); |
128
|
|
|
$this->accessPrivateProperty->setAccessible(true); |
129
|
|
|
|
130
|
|
|
$this->accessProtectedProperty = new ReflectionProperty(ClassWithProtectedProperties::class, 'property0'); |
131
|
|
|
$this->accessProtectedProperty->setAccessible(true); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function benchEmptyClassInitialization() : void |
135
|
|
|
{ |
136
|
|
|
$this->emptyClassProxy->initializeProxy(); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function benchInitializedEmptyClassInitialization() : void |
140
|
|
|
{ |
141
|
|
|
$this->initializedEmptyClassProxy->initializeProxy(); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function benchObjectWithPrivatePropertiesInitialization() : void |
145
|
|
|
{ |
146
|
|
|
$this->privatePropertiesProxy->initializeProxy(); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function benchInitializedObjectWithPrivatePropertiesInitialization() : void |
150
|
|
|
{ |
151
|
|
|
$this->initializedPrivatePropertiesProxy->initializeProxy(); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function benchObjectWithPrivatePropertiesPropertyRead() : void |
155
|
|
|
{ |
156
|
|
|
$this->accessPrivateProperty->getValue($this->privatePropertiesProxy); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function benchInitializedObjectWithPrivatePropertiesPropertyRead() : void |
160
|
|
|
{ |
161
|
|
|
$this->accessPrivateProperty->getValue($this->initializedPrivatePropertiesProxy); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function benchObjectWithPrivatePropertiesPropertyWrite() : void |
165
|
|
|
{ |
166
|
|
|
$this->accessPrivateProperty->setValue($this->privatePropertiesProxy, 'foo'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function benchInitializedObjectWithPrivatePropertiesPropertyWrite() : void |
170
|
|
|
{ |
171
|
|
|
$this->accessPrivateProperty->setValue($this->initializedPrivatePropertiesProxy, 'foo'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function benchObjectWithProtectedPropertiesInitialization() : void |
175
|
|
|
{ |
176
|
|
|
$this->protectedPropertiesProxy->initializeProxy(); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function benchInitializedObjectWithProtectedPropertiesInitialization() : void |
180
|
|
|
{ |
181
|
|
|
$this->initializedProtectedPropertiesProxy->initializeProxy(); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function benchObjectWithProtectedPropertiesPropertyRead() : void |
185
|
|
|
{ |
186
|
|
|
$this->accessProtectedProperty->getValue($this->protectedPropertiesProxy); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function benchInitializedObjectWithProtectedPropertiesPropertyRead() : void |
190
|
|
|
{ |
191
|
|
|
$this->accessProtectedProperty->getValue($this->initializedProtectedPropertiesProxy); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function benchObjectWithProtectedPropertiesPropertyWrite() : void |
195
|
|
|
{ |
196
|
|
|
$this->accessProtectedProperty->setValue($this->protectedPropertiesProxy, 'foo'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function benchInitializedObjectWithProtectedPropertiesPropertyWrite() : void |
200
|
|
|
{ |
201
|
|
|
$this->accessProtectedProperty->setValue($this->initializedProtectedPropertiesProxy, 'foo'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function benchObjectWithPublicPropertiesInitialization() : void |
205
|
|
|
{ |
206
|
|
|
$this->publicPropertiesProxy->initializeProxy(); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function benchInitializedObjectWithPublicPropertiesInitialization() : void |
210
|
|
|
{ |
211
|
|
|
$this->initializedPublicPropertiesProxy->initializeProxy(); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function benchObjectWithPublicPropertiesPropertyRead() : void |
215
|
|
|
{ |
216
|
|
|
$this->publicPropertiesProxy->property0; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function benchInitializedObjectWithPublicPropertiesPropertyRead() : void |
220
|
|
|
{ |
221
|
|
|
$this->initializedPublicPropertiesProxy->property0; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function benchObjectWithPublicPropertiesPropertyWrite() : void |
225
|
|
|
{ |
226
|
|
|
$this->publicPropertiesProxy->property0 = 'foo'; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function benchInitializedObjectWithPublicPropertiesPropertyWrite() : void |
230
|
|
|
{ |
231
|
|
|
$this->initializedPublicPropertiesProxy->property0 = 'foo'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function benchObjectWithPublicPropertiesPropertyIsset() : void |
235
|
|
|
{ |
236
|
|
|
/* @noinspection PhpExpressionResultUnusedInspection */ |
237
|
|
|
/* @noinspection UnSafeIsSetOverArrayInspection */ |
238
|
|
|
isset($this->publicPropertiesProxy->property0); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function benchInitializedObjectWithPublicPropertiesPropertyIsset() : void |
242
|
|
|
{ |
243
|
|
|
/* @noinspection PhpExpressionResultUnusedInspection */ |
244
|
|
|
/* @noinspection UnSafeIsSetOverArrayInspection */ |
245
|
|
|
isset($this->initializedPublicPropertiesProxy->property0); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function benchObjectWithPublicPropertiesPropertyUnset() : void |
249
|
|
|
{ |
250
|
|
|
unset($this->publicPropertiesProxy->property0); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function benchInitializedObjectWithPublicPropertiesPropertyUnset() : void |
254
|
|
|
{ |
255
|
|
|
unset($this->initializedPublicPropertiesProxy->property0); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function benchObjectWithMixedPropertiesInitialization() : void |
259
|
|
|
{ |
260
|
|
|
$this->mixedPropertiesProxy->initializeProxy(); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function benchInitializedObjectWithMixedPropertiesInitialization() : void |
264
|
|
|
{ |
265
|
|
|
$this->initializedMixedPropertiesProxy->initializeProxy(); |
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function benchObjectWithMixedPropertiesPropertyRead() : void |
269
|
|
|
{ |
270
|
|
|
$this->mixedPropertiesProxy->publicProperty0; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function benchInitializedObjectWithMixedPropertiesPropertyRead() : void |
274
|
|
|
{ |
275
|
|
|
$this->initializedMixedPropertiesProxy->publicProperty0; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function benchObjectWithMixedPropertiesPropertyWrite() : void |
279
|
|
|
{ |
280
|
|
|
$this->mixedPropertiesProxy->publicProperty0 = 'foo'; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function benchInitializedObjectWithMixedPropertiesPropertyWrite() : void |
284
|
|
|
{ |
285
|
|
|
$this->initializedMixedPropertiesProxy->publicProperty0 = 'foo'; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function benchObjectWithMixedPropertiesPropertyIsset() : void |
289
|
|
|
{ |
290
|
|
|
/* @noinspection PhpExpressionResultUnusedInspection */ |
291
|
|
|
/* @noinspection UnSafeIsSetOverArrayInspection */ |
292
|
|
|
isset($this->mixedPropertiesProxy->publicProperty0); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function benchInitializedObjectWithMixedPropertiesPropertyIsset() : void |
296
|
|
|
{ |
297
|
|
|
/* @noinspection PhpExpressionResultUnusedInspection */ |
298
|
|
|
/* @noinspection UnSafeIsSetOverArrayInspection */ |
299
|
|
|
isset($this->initializedMixedPropertiesProxy->publicProperty0); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function benchObjectWithMixedPropertiesPropertyUnset() : void |
303
|
|
|
{ |
304
|
|
|
unset($this->mixedPropertiesProxy->publicProperty0); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function benchInitializedObjectWithMixedPropertiesPropertyUnset() : void |
308
|
|
|
{ |
309
|
|
|
unset($this->initializedMixedPropertiesProxy->publicProperty0); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
private function buildProxy(string $originalClass) : LazyLoadingInterface |
313
|
|
|
{ |
314
|
|
|
return ($this->generateProxyClass($originalClass))::staticProxyConstructor( |
315
|
|
|
function ($proxy, string $method, $params, & $initializer) : bool { |
316
|
|
|
$initializer = null; |
317
|
|
|
|
318
|
|
|
return true; |
319
|
|
|
} |
320
|
|
|
); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
private function generateProxyClass(string $originalClassName) : string |
324
|
|
|
{ |
325
|
|
|
$generatedClassName = __CLASS__ . '\\' . $originalClassName; |
326
|
|
|
|
327
|
|
|
if (class_exists($generatedClassName)) { |
328
|
|
|
return $generatedClassName; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$generatedClass = new ClassGenerator($generatedClassName); |
332
|
|
|
|
333
|
|
|
(new LazyLoadingGhostGenerator())->generate(new ReflectionClass($originalClassName), $generatedClass, []); |
334
|
|
|
(new EvaluatingGeneratorStrategy())->generate($generatedClass); |
335
|
|
|
|
336
|
|
|
return $generatedClassName; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..