-
-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathArrayDataSourceBench.php
More file actions
223 lines (188 loc) · 5.2 KB
/
Copy pathArrayDataSourceBench.php
File metadata and controls
223 lines (188 loc) · 5.2 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
<?php declare(strict_types = 1);
namespace Contributte\Datagrid\Benchmarks;
use DateTime;
use DateTimeInterface;
use PhpBench\Attributes as Bench;
/**
* Benchmarks for ArrayDataSource optimizations:
* - Sort flatten: nested foreach vs array_merge(...)
* - Date range filter: duplicated DateTime conversion vs single conversion
*/
class ArrayDataSourceBench
{
private array $sortGroupedData = [];
private array $dateRows = [];
/**
* Original sort flatten with nested foreach
*
* @param array{row_count: int} $params
*/
#[Bench\Revs(500)]
#[Bench\Iterations(10)]
#[Bench\BeforeMethods('setUpSortData')]
#[Bench\ParamProviders('provideRowCounts')]
public function benchSortFlattenForeach(array $params): void
{
$dataSource = [];
foreach ($this->sortGroupedData as $i) {
foreach ($i as $item) {
$dataSource[] = $item;
}
}
}
/**
* Optimized sort flatten with array_merge spread
*
* @param array{row_count: int} $params
*/
#[Bench\Revs(500)]
#[Bench\Iterations(10)]
#[Bench\BeforeMethods('setUpSortData')]
#[Bench\ParamProviders('provideRowCounts')]
public function benchSortFlattenArrayMerge(array $params): void
{
$dataSource = array_merge(...array_values($this->sortGroupedData));
}
/**
* Original date range filter with duplicated DateTime conversion
*
* @param array{row_count: int} $params
*/
#[Bench\Revs(100)]
#[Bench\Iterations(10)]
#[Bench\BeforeMethods('setUpDateRows')]
#[Bench\ParamProviders('provideRowCounts')]
public function benchDateRangeFilterDuplicated(array $params): void
{
$dateFrom = new DateTime('2024-01-01');
$dateTo = new DateTime('2024-12-31');
foreach ($this->dateRows as $row) {
$rowValue = $row['date'];
// Duplicated conversion (original pattern)
// "from" check
if (!($rowValue instanceof DateTime)) {
$rowValue = new DateTime($rowValue);
}
if ($rowValue->getTimestamp() < $dateFrom->getTimestamp()) {
continue;
}
// "to" check — re-read and re-convert (original bug)
$rowValue2 = $row['date'];
if (!($rowValue2 instanceof DateTime)) {
$rowValue2 = new DateTime($rowValue2);
}
if ($rowValue2->getTimestamp() > $dateTo->getTimestamp()) {
continue;
}
}
}
/**
* Optimized date range filter with single DateTime conversion
*
* @param array{row_count: int} $params
*/
#[Bench\Revs(100)]
#[Bench\Iterations(10)]
#[Bench\BeforeMethods('setUpDateRows')]
#[Bench\ParamProviders('provideRowCounts')]
public function benchDateRangeFilterSingle(array $params): void
{
$dateFrom = new DateTime('2024-01-01');
$dateTo = new DateTime('2024-12-31');
foreach ($this->dateRows as $row) {
$rowValue = $row['date'];
// Single conversion (optimized pattern)
if (!($rowValue instanceof DateTime)) {
$rowValue = new DateTime($rowValue);
}
if ($rowValue->getTimestamp() < $dateFrom->getTimestamp()) {
continue;
}
if ($rowValue->getTimestamp() > $dateTo->getTimestamp()) {
continue;
}
}
}
/**
* Original sort key extraction with string cast and DateTimeInterface check
*
* @param array{row_count: int} $params
*/
#[Bench\Revs(200)]
#[Bench\Iterations(10)]
#[Bench\BeforeMethods('setUpDateRows')]
#[Bench\ParamProviders('provideRowCounts')]
public function benchSortWithGrouping(array $params): void
{
$data = [];
foreach ($this->dateRows as $item) {
$value = $item['name'];
$sortBy = $value instanceof DateTimeInterface ? $value->format('Y-m-d H:i:s') : (string) $value;
$data[$sortBy][] = $item;
}
ksort($data, SORT_LOCALE_STRING);
$dataSource = [];
foreach ($data as $i) {
foreach ($i as $item) {
$dataSource[] = $item;
}
}
}
/**
* Optimized sort with array_merge spread for flattening
*
* @param array{row_count: int} $params
*/
#[Bench\Revs(200)]
#[Bench\Iterations(10)]
#[Bench\BeforeMethods('setUpDateRows')]
#[Bench\ParamProviders('provideRowCounts')]
public function benchSortWithArrayMerge(array $params): void
{
$data = [];
foreach ($this->dateRows as $item) {
$value = $item['name'];
$sortBy = $value instanceof DateTimeInterface ? $value->format('Y-m-d H:i:s') : (string) $value;
$data[$sortBy][] = $item;
}
ksort($data, SORT_LOCALE_STRING);
$dataSource = $data !== [] ? array_merge(...array_values($data)) : [];
}
/**
* @return array<string, array{row_count: int}>
*/
public function provideRowCounts(): array
{
return [
'50 rows' => ['row_count' => 50],
'200 rows' => ['row_count' => 200],
'1000 rows' => ['row_count' => 1000],
];
}
/**
* @param array{row_count: int} $params
*/
public function setUpSortData(array $params): void
{
$this->sortGroupedData = [];
$names = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'];
for ($i = 0; $i < $params['row_count']; $i++) {
$name = $names[$i % count($names)];
$this->sortGroupedData[$name][] = ['id' => $i, 'name' => $name, 'age' => rand(18, 80)];
}
}
/**
* @param array{row_count: int} $params
*/
public function setUpDateRows(array $params): void
{
$this->dateRows = [];
for ($i = 0; $i < $params['row_count']; $i++) {
$this->dateRows[] = [
'id' => $i,
'name' => 'Item ' . $i,
'date' => '2024-' . str_pad((string) (($i % 12) + 1), 2, '0', STR_PAD_LEFT) . '-15',
];
}
}
}