15
15
use PhpBench \Attributes \BeforeClassMethods ;
16
16
use PhpBench \Attributes \Iterations ;
17
17
use PhpBench \Attributes \ParamProviders ;
18
- use PhpBench \Attributes \Revs ;
19
18
use RuntimeException ;
20
19
21
20
use function array_chunk ;
44
43
#[AfterClassMethods('afterClass ' )]
45
44
#[AfterMethods('afterIteration ' )]
46
45
#[Iterations(1 )]
47
- #[Revs(1 )]
48
46
final class ParallelMultiFileExportBench
49
47
{
50
48
public static function beforeClass (): void
@@ -74,15 +72,15 @@ public function afterIteration(): void
74
72
* Using a single thread to export multiple files.
75
73
* By executing a single Find command for multiple files, we can reduce the number of roundtrips to the server.
76
74
*
77
- * @param array{chunk :int} $params
75
+ * @param array{chunkSize :int} $params
78
76
*/
79
77
#[ParamProviders(['provideChunkParams ' ])]
80
78
public function benchSequential (array $ params ): void
81
79
{
82
- foreach (array_chunk (self ::getFileNames (), $ params ['chunk ' ]) as $ i => $ files ) {
80
+ foreach (array_chunk (self ::getFileNames (), $ params ['chunkSize ' ]) as $ i => $ files ) {
83
81
self ::exportFile ($ files , [], [
84
- 'limit ' => 5_000 * $ params ['chunk ' ],
85
- 'skip ' => 5_000 * $ params ['chunk ' ] * $ i ,
82
+ 'limit ' => 5_000 * $ params ['chunkSize ' ],
83
+ 'skip ' => 5_000 * $ params ['chunkSize ' ] * $ i ,
86
84
]);
87
85
}
88
86
}
@@ -103,12 +101,12 @@ public function benchFork(array $params): void
103
101
Utils::reset ();
104
102
105
103
// Create a child process for each chunk of files
106
- foreach (array_chunk (self ::getFileNames (), $ params ['chunk ' ]) as $ i => $ files ) {
104
+ foreach (array_chunk (self ::getFileNames (), $ params ['chunkSize ' ]) as $ i => $ files ) {
107
105
$ pid = pcntl_fork ();
108
106
if ($ pid === 0 ) {
109
107
self ::exportFile ($ files , [], [
110
- 'limit ' => 5_000 * $ params ['chunk ' ],
111
- 'skip ' => 5_000 * $ params ['chunk ' ] * $ i ,
108
+ 'limit ' => 5_000 * $ params ['chunkSize ' ],
109
+ 'skip ' => 5_000 * $ params ['chunkSize ' ] * $ i ,
112
110
]);
113
111
114
112
// Exit the child process
@@ -133,21 +131,21 @@ public function benchFork(array $params): void
133
131
/**
134
132
* Using amphp/parallel with worker pool
135
133
*
136
- * @param array{chunk :int} $params
134
+ * @param array{chunkSize :int} $params
137
135
*/
138
136
#[ParamProviders(['provideChunkParams ' ])]
139
137
public function benchAmpWorkers (array $ params ): void
140
138
{
141
- $ workerPool = new ContextWorkerPool (ceil (100 / $ params ['chunk ' ]), new ContextWorkerFactory ());
139
+ $ workerPool = new ContextWorkerPool (ceil (100 / $ params ['chunkSize ' ]), new ContextWorkerFactory ());
142
140
143
141
$ futures = [];
144
- foreach (array_chunk (self ::getFileNames (), $ params ['chunk ' ]) as $ i => $ files ) {
142
+ foreach (array_chunk (self ::getFileNames (), $ params ['chunkSize ' ]) as $ i => $ files ) {
145
143
$ futures [] = $ workerPool ->submit (
146
144
new ExportFileTask (
147
145
files: $ files ,
148
146
options: [
149
- 'limit ' => 5_000 * $ params ['chunk ' ],
150
- 'skip ' => 5_000 * $ params ['chunk ' ] * $ i ,
147
+ 'limit ' => 5_000 * $ params ['chunkSize ' ],
148
+ 'skip ' => 5_000 * $ params ['chunkSize ' ] * $ i ,
151
149
],
152
150
),
153
151
)->getFuture ();
@@ -160,13 +158,9 @@ public function benchAmpWorkers(array $params): void
160
158
161
159
public static function provideChunkParams (): Generator
162
160
{
163
- yield 'by 1 ' => ['chunk ' => 1 ];
164
- yield 'by 2 ' => ['chunk ' => 2 ];
165
- yield 'by 4 ' => ['chunk ' => 4 ];
166
- yield 'by 8 ' => ['chunk ' => 8 ];
167
- yield 'by 13 ' => ['chunk ' => 13 ];
168
- yield 'by 20 ' => ['chunk ' => 20 ];
169
- yield 'by 100 ' => ['chunk ' => 100 ];
161
+ yield '100 chunks ' => ['chunkSize ' => 1 ];
162
+ yield '25 chunks ' => ['chunkSize ' => 4 ];
163
+ yield '10 chunks ' => ['chunkSize ' => 10 ];
170
164
}
171
165
172
166
/**
0 commit comments