Skip to content

Commit 11b0213

Browse files
committed
Upgrade benchmark to AMPHP with fibers
1 parent bb63143 commit 11b0213

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

benchmark/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"require": {
1616
"php": ">=8.1",
1717
"ext-pcntl": "*",
18-
"amphp/parallel-functions": "^1.1",
18+
"amphp/parallel": "^2.2",
1919
"mongodb/mongodb": "@dev",
2020
"phpbench/phpbench": "^1.2"
2121
},
@@ -26,5 +26,8 @@
2626
},
2727
"scripts": {
2828
"benchmark": "phpbench run --report=aggregate"
29+
},
30+
"config": {
31+
"sort-packages": true
2932
}
3033
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace MongoDB\Benchmark\DriverBench\Amp;
4+
5+
use Amp\Cancellation;
6+
use Amp\Parallel\Worker\Task;
7+
use Amp\Sync\Channel;
8+
use MongoDB\Benchmark\DriverBench\ParallelMultiFileImportBench;
9+
10+
final class ImportFileTask implements Task
11+
{
12+
public function __construct(
13+
private string $file,
14+
) {
15+
}
16+
17+
public function run(Channel $channel, Cancellation $cancellation): mixed
18+
{
19+
ParallelMultiFileImportBench::importFile($this->file);
20+
21+
return null;
22+
}
23+
}

benchmark/src/DriverBench/ParallelMultiFileImportBench.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace MongoDB\Benchmark\DriverBench;
44

5-
use Amp\Parallel\Worker\DefaultPool;
5+
use Amp\Future;
6+
use Amp\Parallel\Worker\ContextWorkerFactory;
7+
use Amp\Parallel\Worker\ContextWorkerPool;
68
use Generator;
9+
use MongoDB\Benchmark\DriverBench\Amp\ImportFileTask;
710
use MongoDB\Benchmark\Fixtures\Data;
811
use MongoDB\Benchmark\Utils;
912
use MongoDB\BSON\Document;
@@ -16,8 +19,6 @@
1619
use PhpBench\Attributes\Revs;
1720
use RuntimeException;
1821

19-
use function Amp\ParallelFunctions\parallelMap;
20-
use function Amp\Promise\wait;
2122
use function array_map;
2223
use function count;
2324
use function fclose;
@@ -107,7 +108,7 @@ public function benchMultiFileImportInsertMany(): void
107108
/**
108109
* Using multiple forked threads
109110
*
110-
* @param array{processes:int, files:string[], batchSize:int} $params
111+
* @param array{processes:int} $params
111112
*/
112113
#[ParamProviders(['provideProcessesParameter'])]
113114
public function benchMultiFileImportFork(array $params): void
@@ -148,20 +149,25 @@ public function benchMultiFileImportFork(array $params): void
148149
}
149150

150151
/**
151-
* Using amphp/parallel-functions with worker pool
152+
* Using amphp/parallel with worker pool
152153
*
153-
* @param array{processes:int, files:string[], batchSize:int} $params
154+
* @param array{processes:int} $params
154155
*/
155156
#[ParamProviders(['provideProcessesParameter'])]
156157
public function benchMultiFileImportAmp(array $params): void
157158
{
158-
wait(parallelMap(
159+
$workerPool = new ContextWorkerPool($params['processes'], new ContextWorkerFactory());
160+
161+
$futures = array_map(
162+
fn ($file) => $workerPool->submit(new ImportFileTask($file))->getFuture(),
159163
self::getFileNames(),
160-
// Uses array callable instead of closure to skip complex serialization
161-
[self::class, 'importFile'],
162-
// The pool size is the number of processes
163-
new DefaultPool($params['processes']),
164-
));
164+
);
165+
166+
foreach (Future::iterate($futures) as $future) {
167+
$future->await();
168+
}
169+
170+
$workerPool->shutdown();
165171
}
166172

167173
public static function provideProcessesParameter(): Generator
@@ -172,7 +178,6 @@ public static function provideProcessesParameter(): Generator
172178
yield '8 proc' => ['processes' => 8]; // 13 sequences
173179
yield '13 proc' => ['processes' => 13]; // 8 sequences
174180
yield '20 proc' => ['processes' => 20]; // 5 sequences
175-
yield '34 proc' => ['processes' => 34]; // 3 sequences
176181
}
177182

178183
/**

0 commit comments

Comments
 (0)