Completed
Push — master ( 7a0385...8c7052 )
by Marco
38:42 queued 29:58
created

SimpleInsertPerformanceBench   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 24 2
A benchHydration() 0 16 3
1
<?php
2
3
namespace Doctrine\Performance\Hydration;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\Performance\EntityManagerFactory;
7
use Doctrine\Tests\Models\CMS;
8
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
9
10
/**
11
 * @BeforeMethods({"init"})
12
 */
13
final class SimpleInsertPerformanceBench
14
{
15
    /**
16
     * @var EntityManagerInterface
17
     */
18
    private $entityManager;
19
20
    /**
21
     * @var CMS\CmsUser[]
22
     */
23
    private $users;
24
25
    /**
26
     * @var string
27
     */
28
    private $tableName;
29
30
    public function init()
31
    {
32
        $this->entityManager = EntityManagerFactory::getEntityManager([
33
            CMS\CmsUser::class,
34
            CMS\CmsPhonenumber::class,
35
            CMS\CmsAddress::class,
36
            CMS\CmsEmail::class,
37
            CMS\CmsGroup::class,
38
            CMS\CmsTag::class,
39
            CMS\CmsArticle::class,
40
            CMS\CmsComment::class,
41
        ]);
42
43
        for ($i = 1; $i <= 10000; ++$i) {
44
            $user           = new CMS\CmsUser;
45
            $user->status   = 'user';
46
            $user->username = 'user' . $i;
47
            $user->name     = 'Mr.Smith-' . $i;
48
49
            $this->users[$i] = $user;
50
        }
51
52
        $this->tableName = $this->entityManager->getClassMetadata(CMS\CmsUser::class)->getTableName();
53
    }
54
55
    public function benchHydration()
56
    {
57
        // Yes, this is a lot of overhead, but I have no better solution other than
58
        // completely mocking out the DB, which would be silly (query impact is
59
        // necessarily part of our benchmarks)
60
        $this->entityManager->getConnection()->executeQuery('DELETE FROM ' . $this->tableName)->execute();
61
62
        foreach ($this->users as $key => $user) {
63
            $this->entityManager->persist($user);
64
65
            if (! ($key % 20)) {
66
                $this->entityManager->flush();
67
                $this->entityManager->clear();
68
            }
69
        }
70
    }
71
}
72