1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Performance\Hydration; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Internal\Hydration\ScalarHydrator; |
6
|
|
|
use Doctrine\ORM\Query; |
7
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
8
|
|
|
use Doctrine\Performance\EntityManagerFactory; |
9
|
|
|
use Doctrine\Tests\Mocks\HydratorMockStatement; |
10
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
11
|
|
|
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @BeforeMethods({"init"}) |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
final class SimpleQueryScalarHydrationPerformanceBench |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ScalarHydrator |
20
|
|
|
*/ |
21
|
|
|
private $hydrator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ResultSetMapping |
25
|
|
|
*/ |
26
|
|
|
private $rsm; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var HydratorMockStatement |
30
|
|
|
*/ |
31
|
|
|
private $stmt; |
32
|
|
|
|
33
|
|
|
public function init() |
34
|
|
|
{ |
35
|
|
|
$resultSet = [ |
36
|
|
|
[ |
37
|
|
|
'u__id' => '1', |
38
|
|
|
'u__status' => 'developer', |
39
|
|
|
'u__username' => 'romanb', |
40
|
|
|
'u__name' => 'Roman', |
41
|
|
|
], |
42
|
|
|
[ |
43
|
|
|
'u__id' => '1', |
44
|
|
|
'u__status' => 'developer', |
45
|
|
|
'u__username' => 'romanb', |
46
|
|
|
'u__name' => 'Roman', |
47
|
|
|
], |
48
|
|
|
[ |
49
|
|
|
'u__id' => '2', |
50
|
|
|
'u__status' => 'developer', |
51
|
|
|
'u__username' => 'romanb', |
52
|
|
|
'u__name' => 'Roman', |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
for ($i = 4; $i < 10000; ++$i) { |
57
|
|
|
$resultSet[] = [ |
58
|
|
|
'u__id' => $i, |
59
|
|
|
'u__status' => 'developer', |
60
|
|
|
'u__username' => 'jwage', |
61
|
|
|
'u__name' => 'Jonathan', |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->stmt = new HydratorMockStatement($resultSet); |
66
|
|
|
$this->hydrator = new ScalarHydrator(EntityManagerFactory::getEntityManager([])); |
67
|
|
|
$this->rsm = new ResultSetMapping; |
68
|
|
|
|
69
|
|
|
$this->rsm->addEntityResult(CmsUser::class, 'u'); |
70
|
|
|
$this->rsm->addFieldResult('u', 'u__id', 'id'); |
71
|
|
|
$this->rsm->addFieldResult('u', 'u__status', 'status'); |
72
|
|
|
$this->rsm->addFieldResult('u', 'u__username', 'username'); |
73
|
|
|
$this->rsm->addFieldResult('u', 'u__name', 'name'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function benchHydration() |
77
|
|
|
{ |
78
|
|
|
$this->hydrator->hydrateAll($this->stmt, $this->rsm); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.