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