Repository navigation
Expand file tree
/
Copy pathOrmBench.php
More file actions
282 lines (245 loc) · 7.99 KB
/
Copy pathOrmBench.php
File metadata and controls
282 lines (245 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
declare(strict_types=1);
namespace DbInterop\Benchmarks;
use PhpBench\Attributes as Bench;
/**
* The scenarios every contender runs. A contender implements the operations; the subjects here stay the same,
* so the results line up by subject name.
*
* Reads start from an empty identity map unless the scenario says otherwise. Writes run inside a transaction that
* is rolled back, so the dataset never drifts.
*/
#[Bench\BeforeMethods('setUp')]
#[Bench\Warmup(1)]
#[Bench\Iterations(10)]
#[Bench\OutputTimeUnit('microseconds', precision: 1)]
abstract class OrmBench
{
#[Bench\Revs(1000)]
public function benchFindByPk(): void
{
$this->clear();
$this->findUser(500);
}
#[Bench\Revs(500)]
public function benchList10(): void
{
$this->clear();
$this->users(10);
}
#[Bench\Revs(100)]
public function benchList100(): void
{
$this->clear();
$this->users(100);
}
#[Bench\Revs(10)]
public function benchList1000(): void
{
$this->clear();
$this->users(1000);
}
/**
* The same 100 rows again while the entities are still in the identity map, for the ORMs that have one.
*/
#[Bench\Revs(100)]
public function benchRefetch100(): void
{
$this->users(100);
}
/**
* 100 rows through the ORM query layer without entities.
*/
#[Bench\Revs(100)]
public function benchArrays100(): void
{
$this->userRows(100);
}
/**
* 100 posts with their authors loaded eagerly: 10 distinct users.
*/
#[Bench\Revs(50)]
public function benchEagerBelongsTo(): void
{
$this->clear();
foreach ($this->postsWithAuthors(100) as $post) {
$this->authorName($post);
}
}
/**
* 20 users with all their posts loaded eagerly: 200 posts.
*/
#[Bench\Revs(50)]
public function benchEagerHasMany(): void
{
$this->clear();
$this->usersWithPosts(20);
}
/**
* 100 posts, then each author through a lazy reference: 10 distinct users loaded on demand.
*/
#[Bench\Revs(50)]
public function benchLazyBelongsTo(): void
{
$this->clear();
foreach ($this->posts(100) as $post) {
$this->authorName($post);
}
}
/**
* Every post at once, 10 000 entities: compare `mem_peak` here.
*/
#[Bench\Revs(1)]
#[Bench\Iterations(5)]
public function benchAllPosts(): void
{
$this->clear();
$this->posts(Dataset::USERS * Dataset::POSTS_PER_USER);
}
#[Bench\Revs(20)]
public function benchInsert(): void
{
$this->clear();
$this->rollback(fn() => $this->insertUsers(100));
}
/**
* 20 users with 5 posts each, saved together.
*/
#[Bench\Revs(20)]
public function benchInsertGraph(): void
{
$this->clear();
$this->rollback(fn() => $this->insertUsersWithPosts(20, 5));
}
/**
* Load 100 users, change a field of each and save them: 100 UPDATE statements.
*/
#[Bench\Revs(20)]
public function benchUpdate(): void
{
$this->clear();
$this->rollback(fn() => $this->renameUsers(100));
}
/**
* Load 100 posts and delete them one by one.
*/
#[Bench\Revs(20)]
public function benchDelete(): void
{
$this->clear();
$this->rollback(fn() => $this->deletePosts(100));
}
/**
* What each scenario produced, for `bin/verify` to compare with the PDO baseline: every contender must do the
* same work.
*
* @return array<string, scalar>
*/
public function verify(): array
{
$this->setUp();
$count = static fn(iterable $items): int => \is_array($items) ? \count($items) : \iterator_count($items);
$names = fn(iterable $posts): int => \count(\array_unique(\array_map(
$this->authorName(...),
\is_array($posts) ? $posts : \iterator_to_array($posts, false),
)));
$all = Dataset::USERS * Dataset::POSTS_PER_USER * 2;
$facts = [];
$this->clear();
$facts['findByPk name'] = $this->findUser(500)->name ?? '?';
$this->clear();
$facts['list 100'] = $count($this->users(100));
$facts['arrays 100'] = \count($this->userRows(100));
$this->clear();
$facts['eager belongsTo authors'] = $names($this->postsWithAuthors(100));
$this->clear();
$users = $this->usersWithPosts(20);
$facts['eager hasMany users'] = $count($users);
$facts['eager hasMany posts'] = \array_sum(\array_map(
fn(object $user): int => $count($this->userPosts($user)),
\is_array($users) ? $users : \iterator_to_array($users, false),
));
$this->clear();
$facts['lazy belongsTo authors'] = $names($this->posts(100));
$this->clear();
$facts['all posts'] = $count($this->posts($all));
$this->clear();
$this->rollback(function () use (&$facts, $count, $all): void {
$this->insertUsers(100);
$this->clear();
$facts['users after insert'] = $count($this->users($all));
});
$this->clear();
$this->rollback(function () use (&$facts, $count, $all): void {
$this->insertUsersWithPosts(20, 5);
$this->clear();
$facts['users after graph'] = $count($this->users($all));
$facts['posts after graph'] = $count($this->posts($all));
});
$this->clear();
$this->rollback(function () use (&$facts): void {
$this->renameUsers(100);
$this->clear();
$facts['renamed users'] = \count(\array_filter(
\is_array($users = $this->users(1000)) ? $users : \iterator_to_array($users, false),
static fn(object $user): bool => \str_ends_with($user->name, ' (edited)'),
));
});
$this->clear();
$this->rollback(function () use (&$facts, $count, $all): void {
$this->deletePosts(100);
$this->clear();
$facts['posts after delete'] = $count($this->posts($all));
});
$this->clear();
$facts['users after rollbacks'] = $count($this->users($all));
$facts['posts after rollbacks'] = $count($this->posts($all));
return $facts;
}
public function setUp(): void
{
$this->boot();
}
/**
* Connects; with an in-memory SQLite, also fills the database through the contender's connection.
*/
abstract protected function boot(): void;
/**
* Forgets every loaded entity: the identity map, the unit of work.
*/
abstract protected function clear(): void;
/**
* Runs the callback in a transaction and rolls it back.
*/
abstract protected function rollback(\Closure $callback): void;
abstract protected function findUser(int $id): object;
/**
* The first users by id.
*/
abstract protected function users(int $limit): iterable;
/**
* The first users by id, as arrays.
*/
abstract protected function userRows(int $limit): array;
/**
* The first posts by id, relations not loaded.
*/
abstract protected function posts(int $limit): iterable;
/**
* The first posts by id with their authors.
*/
abstract protected function postsWithAuthors(int $limit): iterable;
/**
* The users with an id up to `$maxId` with all their posts.
*/
abstract protected function usersWithPosts(int $maxId): iterable;
abstract protected function authorName(object $post): string;
/**
* The posts of a user loaded by {@see usersWithPosts()}.
*/
abstract protected function userPosts(object $user): iterable;
abstract protected function insertUsers(int $count): void;
abstract protected function insertUsersWithPosts(int $users, int $postsEach): void;
abstract protected function renameUsers(int $count): void;
abstract protected function deletePosts(int $count): void;
}