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