Completed
Push — master ( 3be072...ba2d3a )
by Marco
231:32 queued 209:55
created

LazyLoadingValueHolderPropertyAccessBench   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 3
dl 0
loc 208
rs 10
c 0
b 0
f 0
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\VirtualProxyInterface;
11
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator;
12
use ProxyManagerTestAsset\ClassWithMixedProperties;
13
use ProxyManagerTestAsset\ClassWithPublicProperties;
14
use ProxyManagerTestAsset\EmptyClass;
15
use ReflectionClass;
16
use function assert;
17
use function class_exists;
18
19
/**
20
 * Benchmark that provides results for state access/initialization time for lazy loading value holder proxies
21
 *
22
 * @BeforeMethods({"setUp"})
23
 */
24
final class LazyLoadingValueHolderPropertyAccessBench
25
{
26
    /** @var EmptyClass&VirtualProxyInterface */
27
    private EmptyClass $emptyClassProxy;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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