Passed
Push — 2.0 ( 6069a7...2a075c )
by Sébastien
05:45
created

TypeBench   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 59
c 1
b 0
f 0
dl 0
loc 146
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A bench_all_not_typed_with_cast() 0 7 2
A randArray() 0 25 5
A bench_insert_not_typed() 0 7 1
A bench_all_typed() 0 3 1
A setUp() 0 31 2
A bench_insert_typed() 0 7 1
A bench_all_not_typed() 0 3 1
1
<?php
2
3
namespace Bdf\Prime;
4
5
require_once __DIR__ . '/_files/BenchData.php';
6
7
use Bdf\Prime\Bench\BenchData;
8
use Bdf\Prime\Bench\EntityArrayOf;
9
use Bdf\Prime\Bench\EntityNotTypedArray;
10
use Bdf\Prime\Cache\ArrayCache;
11
use Bdf\Prime\Connection\ConnectionConfig;
0 ignored issues
show
Bug introduced by
The type Bdf\Prime\Connection\ConnectionConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Bdf\Prime\Repository\EntityRepository;
13
use Bdf\Prime\Types\ArrayType;
14
use BenchCaseAdapter;
15
16
/**
17
 * @Revs(100)
18
 */
19
class TypeBench extends BenchCaseAdapter
20
{
21
    const SCALAR_COUNT = 10;
22
    const DATEIME_COUNT = 10;
23
24
    /**
25
     * @var ServiceLocator
26
     */
27
    protected $prime;
28
29
    /**
30
     * @var BenchData
31
     */
32
    protected $data;
33
34
    /**
35
     * @var ArrayCache
36
     */
37
    protected $cache;
38
39
    /**
40
     * @var EntityRepository
41
     */
42
    protected $typedRepository;
43
44
    /**
45
     * @var EntityRepository
46
     */
47
    protected $notTypedRepository;
48
49
50
    public function setUp()
51
    {
52
        $this->cache = new ArrayCache();
53
        $this->prime = new ServiceLocator();
54
        $this->prime->connections()->declareConnection('test', BENCH_CONNECTION);
55
        $this->prime->connection('test')->getConfiguration()->getTypes()->register(ArrayType::class, 'array');
0 ignored issues
show
Bug introduced by
The method getConfiguration() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $this->prime->connection('test')->/** @scrutinizer ignore-call */ getConfiguration()->getTypes()->register(ArrayType::class, 'array');
Loading history...
56
        Locatorizable::configure($this->prime);
0 ignored issues
show
Bug introduced by
$this->prime of type Bdf\Prime\ServiceLocator is incompatible with the type Closure expected by parameter $locator of Bdf\Prime\Locatorizable::configure(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        Locatorizable::configure(/** @scrutinizer ignore-type */ $this->prime);
Loading history...
57
58
        $this->data = new BenchData($this->prime);
59
        $this->data->register([EntityArrayOf::class, EntityNotTypedArray::class]);
60
61
        $entities = [];
62
63
        for ($i = 0; $i < 100; ++$i) {
64
            $entities[] = new EntityArrayOf([
65
                'floats'   => $this->randArray('double', self::SCALAR_COUNT),
66
                'booleans' => $this->randArray('boolean', self::SCALAR_COUNT),
67
                'dates'    => $this->randArray('datetime', self::DATEIME_COUNT),
68
            ]);
69
70
            $entities[] = new EntityNotTypedArray([
71
                'floats'   => $this->randArray('double', self::SCALAR_COUNT),
72
                'booleans' => $this->randArray('boolean', self::SCALAR_COUNT),
73
                'dates'    => array_map(function (\DateTime $dateTime) { return $dateTime->format(\DateTime::ISO8601); }, $this->randArray('datetime', self::DATEIME_COUNT)),
74
            ]);
75
        }
76
77
        $this->data->push($entities);
78
79
        $this->typedRepository = $this->prime->repository(EntityArrayOf::class);
80
        $this->notTypedRepository = $this->prime->repository(EntityNotTypedArray::class);
81
    }
82
83
    /**
84
     * @Groups({"array", "read"})
85
     */
86
    public function bench_all_typed()
87
    {
88
        $this->typedRepository->all();
0 ignored issues
show
Bug introduced by
The method all() does not exist on Bdf\Prime\Repository\EntityRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $this->typedRepository->/** @scrutinizer ignore-call */ 
89
                                all();
Loading history...
89
    }
90
91
    /**
92
     * @Groups({"array", "read"})
93
     */
94
    public function bench_all_not_typed()
95
    {
96
        $this->notTypedRepository->all();
97
    }
98
99
    /**
100
     * @Groups({"array", "read"})
101
     */
102
    public function bench_all_not_typed_with_cast()
103
    {
104
        /** @var EntityNotTypedArray $entity */
105
        foreach ($this->notTypedRepository->all() as $entity) {
106
            $entity->booleans = array_map('boolval', $entity->booleans);
107
            $entity->floats = array_map('doubleval', $entity->floats);
108
            $entity->dates = array_map(function ($str) { return \DateTime::createFromFormat(\DateTime::ISO8601, $str); }, $entity->dates);
109
        }
110
    }
111
112
    /**
113
     * @Groups({"array", "insert"})
114
     */
115
    public function bench_insert_typed()
116
    {
117
        $this->typedRepository->save(
118
            new EntityArrayOf([
119
                'floats'   => $this->randArray('double', self::SCALAR_COUNT),
120
                'booleans' => $this->randArray('boolean', self::SCALAR_COUNT),
121
                'dates'    => $this->randArray('datetime', self::DATEIME_COUNT),
122
            ])
123
        );
124
    }
125
126
    /**
127
     * @Groups({"array", "insert"})
128
     */
129
    public function bench_insert_not_typed()
130
    {
131
        $this->notTypedRepository->save(
132
            new EntityNotTypedArray([
133
                'floats'   => $this->randArray('double', self::SCALAR_COUNT),
134
                'booleans' => $this->randArray('boolean', self::SCALAR_COUNT),
135
                'dates'    => array_map(function (\DateTime $dateTime) { return $dateTime->format(\DateTime::ISO8601); }, $this->randArray('datetime', self::DATEIME_COUNT)),
136
            ])
137
        );
138
    }
139
140
    private function randArray($type, $size)
141
    {
142
        $array = [];
143
144
        for (; $size > 0; --$size) {
145
            $value = rand(0, 100);
146
147
            switch ($type) {
148
                case 'double':
149
                    $value /= 100;
150
                    break;
151
152
                case 'boolean':
153
                    $value = $value >= 50;
154
                    break;
155
156
                case 'datetime':
157
                    $value = new \DateTime('+'.$value.'days');
158
                    break;
159
            }
160
161
            $array[] = $value;
162
        }
163
164
        return $array;
165
    }
166
}
167