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\ProxyGenerator\LazyLoadingValueHolderGenerator; |
11
|
|
|
use ProxyManagerTestAsset\ClassWithMixedProperties; |
12
|
|
|
use ProxyManagerTestAsset\ClassWithPrivateProperties; |
13
|
|
|
use ProxyManagerTestAsset\ClassWithProtectedProperties; |
14
|
|
|
use ProxyManagerTestAsset\ClassWithPublicProperties; |
15
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
16
|
|
|
use ReflectionClass; |
17
|
|
|
use function assert; |
18
|
|
|
use function class_exists; |
19
|
|
|
use function is_a; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Benchmark that provides results for simple object instantiation for lazy loading value holder proxies |
23
|
|
|
* |
24
|
|
|
* @BeforeMethods({"setUp"}) |
25
|
|
|
*/ |
26
|
|
|
final class LazyLoadingValueHolderInstantiationBench |
27
|
|
|
{ |
28
|
|
|
/** @psalm-var class-string<EmptyClass> */ |
29
|
|
|
private string $emptyClassProxy; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** @psalm-var class-string<ClassWithPrivateProperties> */ |
32
|
|
|
private string $privatePropertiesProxy; |
33
|
|
|
|
34
|
|
|
/** @psalm-var class-string<ClassWithProtectedProperties> */ |
35
|
|
|
private string $protectedPropertiesProxy; |
36
|
|
|
|
37
|
|
|
/** @psalm-var class-string<ClassWithPublicProperties> */ |
38
|
|
|
private string $publicPropertiesProxy; |
39
|
|
|
|
40
|
|
|
/** @psalm-var class-string<ClassWithMixedProperties> */ |
41
|
|
|
private string $mixedPropertiesProxy; |
42
|
|
|
|
43
|
|
|
public function setUp() : void |
44
|
|
|
{ |
45
|
|
|
$this->emptyClassProxy = $this->generateProxy(EmptyClass::class); |
46
|
|
|
$this->privatePropertiesProxy = $this->generateProxy(ClassWithPrivateProperties::class); |
47
|
|
|
$this->protectedPropertiesProxy = $this->generateProxy(ClassWithProtectedProperties::class); |
48
|
|
|
$this->publicPropertiesProxy = $this->generateProxy(ClassWithPublicProperties::class); |
49
|
|
|
$this->mixedPropertiesProxy = $this->generateProxy(ClassWithMixedProperties::class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function benchOriginalConstructorInstantiationOfEmptyObject() : void |
53
|
|
|
{ |
54
|
|
|
new $this->emptyClassProxy(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function benchInstantiationOfEmptyObject() : void |
58
|
|
|
{ |
59
|
|
|
/** @psalm-suppress UndefinedMethod */ |
60
|
|
|
$this->emptyClassProxy::staticProxyConstructor(static function () : void { |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function benchOriginalConstructorInstantiationOfObjectWithPrivateProperties() : void |
65
|
|
|
{ |
66
|
|
|
new $this->privatePropertiesProxy(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function benchInstantiationOfObjectWithPrivateProperties() : void |
70
|
|
|
{ |
71
|
|
|
/** @psalm-suppress UndefinedMethod */ |
72
|
|
|
$this->privatePropertiesProxy::staticProxyConstructor(static function () : void { |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function benchOriginalConstructorInstantiationOfObjectWithProtectedProperties() : void |
77
|
|
|
{ |
78
|
|
|
new $this->protectedPropertiesProxy(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function benchInstantiationOfObjectWithProtectedProperties() : void |
82
|
|
|
{ |
83
|
|
|
/** @psalm-suppress UndefinedMethod */ |
84
|
|
|
$this->protectedPropertiesProxy::staticProxyConstructor(static function () : void { |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function benchOriginalConstructorInstantiationOfObjectWithPublicProperties() : void |
89
|
|
|
{ |
90
|
|
|
new $this->publicPropertiesProxy(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function benchInstantiationOfObjectWithPublicProperties() : void |
94
|
|
|
{ |
95
|
|
|
/** @psalm-suppress UndefinedMethod */ |
96
|
|
|
$this->publicPropertiesProxy::staticProxyConstructor(static function () : void { |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function benchOriginalConstructorInstantiationOfObjectWithMixedProperties() : void |
101
|
|
|
{ |
102
|
|
|
new $this->mixedPropertiesProxy(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function benchInstantiationOfObjectWithMixedProperties() : void |
106
|
|
|
{ |
107
|
|
|
/** @psalm-suppress UndefinedMethod */ |
108
|
|
|
$this->mixedPropertiesProxy::staticProxyConstructor(static function () : void { |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @psalm-template OriginalClass |
114
|
|
|
* @psalm-param class-string<OriginalClass> $originalClass |
115
|
|
|
* @psalm-return class-string<OriginalClass> |
116
|
|
|
* @psalm-suppress MoreSpecificReturnType |
117
|
|
|
*/ |
118
|
|
|
private function generateProxy(string $originalClass) : string |
119
|
|
|
{ |
120
|
|
|
$generatedClassName = self::class . '\\' . $originalClass; |
121
|
|
|
|
122
|
|
|
if (class_exists($generatedClassName)) { |
123
|
|
|
assert(is_a($generatedClassName, $originalClass, true)); |
124
|
|
|
|
125
|
|
|
return $generatedClassName; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$generatedClass = new ClassGenerator($generatedClassName); |
129
|
|
|
|
130
|
|
|
(new LazyLoadingValueHolderGenerator())->generate(new ReflectionClass($originalClass), $generatedClass); |
131
|
|
|
(new EvaluatingGeneratorStrategy())->generate($generatedClass); |
132
|
|
|
|
133
|
|
|
/** @psalm-suppress LessSpecificReturnStatement */ |
134
|
|
|
return $generatedClassName; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|