-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperf.php
More file actions
52 lines (45 loc) · 1.41 KB
/
perf.php
File metadata and controls
52 lines (45 loc) · 1.41 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
<?php
/**
* Benchmark tests for array_aggregate().
*/
require_once './src/array_aggregate.php';
$rows = 1e6;
echo ($rows / 1e6) . 'M rows' . PHP_EOL;
function run(int $total, int $i): void
{
// Generate test rows
$rows = [];
array_push($rows, array(
'id' => 1,
'group_id' => rand(1, 20),
'city_id' => rand(1, 20),
'name' => substr(str_shuffle(md5(time())),0, rand(4, 12))
));
for ($ii = 1; $ii <= $total; $ii++) {
array_push($rows, array(
'id' => $ii + 1,
'group_id' => (rand(1, 20) % 3) === 0 ?
$rows[$ii - 1]['group_id'] + 1 :
$rows[$ii - 1]['group_id'],
'city_id' => (rand(1, 20) % 5) === 0 ?
$rows[$ii - 1]['city_id'] + 1 :
$rows[$ii - 1]['city_id'],
'name' => substr(str_shuffle(md5(time())),0, rand(4, 12))
));
}
// Start taking time
$time = -microtime(true);
array_aggregate(array('group_id', 'city_id'), $rows, function (array $group): array {
return array(
'ids' => implode(',',array_column($group, 'id')),
'group_id' => $group[0]['group_id'],
'city_id' => $group[0]['city_id'],
'names' => implode(',', array_column($group, 'name'))
);
});
$time += microtime(true);
echo "T$i $time" . "s" . PHP_EOL;
}
for ($i = 0; $i < 5; $i++) {
run($rows, $i + 1);
}