Issues (590)

bench/Query/CustomQueriesBench.php (5 issues)

Labels
1
<?php
2
3
namespace Bdf\Prime\Query;
4
5
require_once __DIR__ . '/../_files/BenchData.php';
6
7
use Bdf\Prime\Bench\BenchData;
8
use Bdf\Prime\Bench\User;
9
use Bdf\Prime\Cache\ArrayCache;
10
use Bdf\Prime\Connection\ConnectionConfig;
0 ignored issues
show
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...
11
use Bdf\Prime\ConnectionManager;
12
use Bdf\Prime\Locatorizable;
13
use Bdf\Prime\Query\Custom\BulkInsert\BulkInsertQuery;
14
use Bdf\Prime\Repository\EntityRepository;
15
use Bdf\Prime\ServiceLocator;
16
use Bdf\Prime\Types\ArrayType;
17
use BenchCaseAdapter;
18
19
/**
20
 * Bench custom queries
21
 *
22
 * @Revs(10)
23
 * @Warmup(1)
24
 */
25
class CustomQueriesBench extends BenchCaseAdapter
26
{
27
    /**
28
     * @var ServiceLocator
29
     */
30
    protected $prime;
31
32
    /**
33
     * @var BenchData
34
     */
35
    protected $data;
36
37
    /**
38
     * @var ArrayCache
39
     */
40
    protected $cache;
41
42
    /**
43
     * @var EntityRepository
44
     */
45
    protected $repository;
46
47
48
    public function setUp()
49
    {
50
        $this->cache = new ArrayCache();
51
        $this->prime = new ServiceLocator();
52
        $this->prime->connections()->declareConnection('test', BENCH_CONNECTION);
53
        $this->prime->connection('test')->getConfiguration()->getTypes()->register(ArrayType::class, 'array');
0 ignored issues
show
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

53
        $this->prime->connection('test')->/** @scrutinizer ignore-call */ getConfiguration()->getTypes()->register(ArrayType::class, 'array');
Loading history...
54
        Locatorizable::configure($this->prime);
0 ignored issues
show
$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

54
        Locatorizable::configure(/** @scrutinizer ignore-type */ $this->prime);
Loading history...
55
56
        $this->data = new BenchData($this->prime);
57
        $this->data->addBulkUsers();
58
59
        $this->repository = $this->prime->repository(User::class);
60
    }
61
62
    /**
63
     * @Groups({"count"})
64
     */
65
    public function bench_count()
66
    {
67
        return $this->repository->count(['id' => '23000123']) > 0;
68
    }
69
70
    /**
71
     * @Groups({"count"})
72
     */
73
    public function bench_countKeyValue()
74
    {
75
        return $this->repository->queries()->countKeyValue(['id' => '23000123']) > 0;
76
    }
77
78
    /**
79
     * @Groups({"findById"})
80
     */
81
    public function bench_findById()
82
    {
83
        return $this->repository->queries()->findById('23000123');
84
    }
85
86
    /**
87
     * @Groups({"findById"})
88
     */
89
    public function bench_builder_get()
90
    {
91
        return $this->repository->builder()->get('23000123');
0 ignored issues
show
The method get() does not exist on Bdf\Prime\Query\QueryInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Bdf\Prime\Query\SqlQueryInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

91
        return $this->repository->builder()->/** @scrutinizer ignore-call */ get('23000123');
Loading history...
92
    }
93
94
    /**
95
     * @Groups({"search"})
96
     */
97
    public function bench_search_keyValue()
98
    {
99
        return $this->repository->keyValue([
0 ignored issues
show
The method keyValue() 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

99
        return $this->repository->/** @scrutinizer ignore-call */ keyValue([
Loading history...
100
            'name'        => 'user Common 123',
101
            'customer.id' => '330000',
102
        ])->first();
103
    }
104
105
    /**
106
     * @Groups({"search"})
107
     */
108
    public function bench_search_builder()
109
    {
110
        return $this->repository->builder()->where([
111
            'name'        => 'user Common 123',
112
            'customer.id' => '330000',
113
        ])->first();
114
    }
115
116
    /**
117
     * @Groups({"delete"})
118
     */
119
    public function bench_delete_keyValue()
120
    {
121
        $id = '23000'.str_pad(rand(1, 500), 3, '0', STR_PAD_LEFT);
122
        return $this->repository->keyValue('id', $id)->delete();
123
    }
124
125
    /**
126
     * @Groups({"delete"})
127
     */
128
    public function bench_delete_keyValue_cached()
129
    {
130
        static $query;
131
132
        if (!$query) {
133
            $query = $this->repository->keyValue();
134
        }
135
136
        $id = '23000'.str_pad(rand(1, 500), 3, '0', STR_PAD_LEFT);
137
        return $query->where('id', $id)->delete();
138
    }
139
140
    /**
141
     * @Groups({"delete"})
142
     */
143
    public function bench_delete_builder()
144
    {
145
        $id = '23000'.str_pad(rand(1, 500), 3, '0', STR_PAD_LEFT);
146
        return $this->repository->builder()->where('id', $id)->delete();
147
    }
148
149
    /**
150
     * @Groups({"update"})
151
     */
152
    public function bench_update_builder()
153
    {
154
        $id = '23000'.str_pad(rand(1, 500), 3, '0', STR_PAD_LEFT);
155
        return $this->repository->builder()->where('id', $id)->update(['name' => 'Bob']);
156
    }
157
158
    /**
159
     * @Groups({"update"})
160
     */
161
    public function bench_update_keyValue()
162
    {
163
        $id = '23000'.str_pad(rand(1, 500), 3, '0', STR_PAD_LEFT);
164
        return $this->repository->keyValue('id', $id)->update(['name' => 'Bob']);
165
    }
166
167
    /**
168
     * @Groups({"update"})
169
     */
170
    public function bench_update_keyValue_cached()
171
    {
172
        static $query;
173
174
        if (!$query) {
175
            $query = $this->repository->keyValue();
176
        }
177
178
        $id = '23000'.str_pad(rand(1, 500), 3, '0', STR_PAD_LEFT);
179
        return $query->where('id', $id)->update(['name' => 'Bob']);
180
    }
181
182
    /**
183
     * @Groups({"insert"})
184
     */
185
    public function bench_insert_bulk()
186
    {
187
        /** @var BulkInsertQuery $insert */
188
        $insert = $this->repository->queries()->make(BulkInsertQuery::class);
189
        $insert->bulk();
190
191
        for ($i = 0; $i < 100; ++$i) {
192
            $insert->values([
193
                'id'          => 6000000 + $i,
194
                'name'        => 'bulk user '.$i,
195
                'roles'       => ['2'],
196
                'customer.id' => 1,
197
            ]);
198
        }
199
200
        $insert->execute();
201
202
        // Clear data set
203
        $this->repository->where('id', 'between', [6000000, 7000000])->delete();
204
    }
205
206
    /**
207
     * @Groups({"insert"})
208
     */
209
    public function bench_insert_transaction()
210
    {
211
        $this->repository->transaction(function () {
212
            /** @var BulkInsertQuery $insert */
213
            $insert = $this->repository->queries()->make(BulkInsertQuery::class);
214
215
            for ($i = 0; $i < 100; ++$i) {
216
                $insert->values([
217
                    'id'          => 6000000 + $i,
218
                    'name'        => 'bulk user '.$i,
219
                    'roles'       => ['2'],
220
                    'customer.id' => 1,
221
                ]);
222
                $insert->execute();
223
            }
224
        });
225
226
        // Clear data set
227
        $this->repository->where('id', 'between', [6000000, 7000000])->delete();
228
    }
229
230
    /**
231
     * @Groups({"insert"})
232
     */
233
    public function bench_insert_builder()
234
    {
235
        $this->repository->transaction(function () {
236
            for ($i = 0; $i < 100; ++$i) {
237
                $this->repository->builder()->insert([
238
                    'id'          => 6000000 + $i,
239
                    'name'        => 'bulk user '.$i,
240
                    'roles'       => ['2'],
241
                    'customer.id' => 1,
242
                ]);
243
            }
244
        });
245
246
        // Clear data set
247
        $this->repository->where('id', 'between', [6000000, 7000000])->delete();
248
    }
249
}
250