|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Benchmark\DriverBench; |
| 4 | + |
| 5 | +use Amp\Parallel\Worker\DefaultPool; |
| 6 | +use Generator; |
| 7 | +use MongoDB\Benchmark\Fixtures\Data; |
| 8 | +use MongoDB\Benchmark\Utils; |
| 9 | +use MongoDB\BSON\Document; |
| 10 | +use MongoDB\Driver\BulkWrite; |
| 11 | +use PhpBench\Attributes\AfterClassMethods; |
| 12 | +use PhpBench\Attributes\BeforeClassMethods; |
| 13 | +use PhpBench\Attributes\BeforeMethods; |
| 14 | +use PhpBench\Attributes\Iterations; |
| 15 | +use PhpBench\Attributes\ParamProviders; |
| 16 | +use PhpBench\Attributes\Revs; |
| 17 | +use RuntimeException; |
| 18 | + |
| 19 | +use function Amp\ParallelFunctions\parallelMap; |
| 20 | +use function Amp\Promise\wait; |
| 21 | +use function array_map; |
| 22 | +use function count; |
| 23 | +use function fclose; |
| 24 | +use function fgets; |
| 25 | +use function file_get_contents; |
| 26 | +use function file_put_contents; |
| 27 | +use function fopen; |
| 28 | +use function is_dir; |
| 29 | +use function mkdir; |
| 30 | +use function pcntl_fork; |
| 31 | +use function pcntl_waitpid; |
| 32 | +use function range; |
| 33 | +use function sprintf; |
| 34 | +use function str_repeat; |
| 35 | +use function stream_get_line; |
| 36 | +use function sys_get_temp_dir; |
| 37 | +use function unlink; |
| 38 | + |
| 39 | +/** |
| 40 | + * For accurate results, run benchmarks on a standalone server. |
| 41 | + * |
| 42 | + * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#ldjson-multi-file-import |
| 43 | + */ |
| 44 | +#[BeforeClassMethods('beforeClass')] |
| 45 | +#[AfterClassMethods('afterClass')] |
| 46 | +#[BeforeMethods('beforeIteration')] |
| 47 | +#[Iterations(1)] |
| 48 | +#[Revs(1)] |
| 49 | +final class ParallelMultiFileImportBench |
| 50 | +{ |
| 51 | + public static function beforeClass(): void |
| 52 | + { |
| 53 | + // Generate files |
| 54 | + $fileContents = str_repeat(file_get_contents(Data::LDJSON_FILE_PATH), 5_000); |
| 55 | + foreach (self::getFileNames() as $file) { |
| 56 | + file_put_contents($file, $fileContents); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static function afterClass(): void |
| 61 | + { |
| 62 | + foreach (self::getFileNames() as $file) { |
| 63 | + unlink($file); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public function beforeIteration(): void |
| 68 | + { |
| 69 | + $database = Utils::getDatabase(); |
| 70 | + $database->drop(); |
| 71 | + $database->createCollection(Utils::getCollectionName()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Using Driver's BulkWrite in a single thread |
| 76 | + */ |
| 77 | + public function benchMultiFileImportBulkWrite(): void |
| 78 | + { |
| 79 | + foreach (self::getFileNames() as $file) { |
| 80 | + self::importFile($file); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Using library's Collection::insertMany in a single thread |
| 86 | + */ |
| 87 | + public function benchMultiFileImportInsertMany(): void |
| 88 | + { |
| 89 | + $collection = Utils::getCollection(); |
| 90 | + foreach (self::getFileNames() as $file) { |
| 91 | + $docs = []; |
| 92 | + // Read file contents into BSON documents |
| 93 | + $fh = fopen($file, 'r'); |
| 94 | + while (($line = fgets($fh)) !== false) { |
| 95 | + if ($line !== '') { |
| 96 | + $docs[] = Document::fromJSON($line); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + fclose($fh); |
| 101 | + |
| 102 | + // Insert documents in bulk |
| 103 | + $collection->insertMany($docs); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Using multiple forked threads |
| 109 | + * |
| 110 | + * @param array{processes:int, files:string[], batchSize:int} $params |
| 111 | + */ |
| 112 | + #[ParamProviders(['provideProcessesParameter'])] |
| 113 | + public function benchMultiFileImportFork(array $params): void |
| 114 | + { |
| 115 | + $pids = []; |
| 116 | + foreach (self::getFileNames() as $file) { |
| 117 | + // Wait for a child process to finish if we have reached the maximum number of processes |
| 118 | + if (count($pids) >= $params['processes']) { |
| 119 | + $pid = pcntl_waitpid(-1, $status); |
| 120 | + unset($pids[$pid]); |
| 121 | + } |
| 122 | + |
| 123 | + $pid = pcntl_fork(); |
| 124 | + if ($pid === 0) { |
| 125 | + // Reset to ensure that the existing libmongoc client (via the Manager) is not re-used by the child |
| 126 | + // process. When the child process constructs a new Manager, the differing PID will result in creation |
| 127 | + // of a new libmongoc client. |
| 128 | + Utils::reset(); |
| 129 | + self::importFile($file); |
| 130 | + |
| 131 | + // Exit the child process |
| 132 | + exit(0); |
| 133 | + } |
| 134 | + |
| 135 | + if ($pid === -1) { |
| 136 | + throw new RuntimeException('Failed to fork'); |
| 137 | + } |
| 138 | + |
| 139 | + // Keep the forked process id to wait for it later |
| 140 | + $pids[$pid] = true; |
| 141 | + } |
| 142 | + |
| 143 | + // Wait for all child processes to finish |
| 144 | + while ($pids !== []) { |
| 145 | + $pid = pcntl_waitpid(-1, $status); |
| 146 | + unset($pids[$pid]); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Using amphp/parallel-functions with worker pool |
| 152 | + * |
| 153 | + * @param array{processes:int, files:string[], batchSize:int} $params |
| 154 | + */ |
| 155 | + #[ParamProviders(['provideProcessesParameter'])] |
| 156 | + public function benchMultiFileImportAmp(array $params): void |
| 157 | + { |
| 158 | + wait(parallelMap( |
| 159 | + 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 | + )); |
| 165 | + } |
| 166 | + |
| 167 | + public static function provideProcessesParameter(): Generator |
| 168 | + { |
| 169 | + yield '1 proc' => ['processes' => 1]; // 100 sequences, to compare to the single thread baseline |
| 170 | + yield '2 proc' => ['processes' => 2]; // 50 sequences |
| 171 | + yield '4 proc' => ['processes' => 4]; // 25 sequences |
| 172 | + yield '8 proc' => ['processes' => 8]; // 13 sequences |
| 173 | + yield '13 proc' => ['processes' => 13]; // 8 sequences |
| 174 | + yield '20 proc' => ['processes' => 20]; // 5 sequences |
| 175 | + yield '34 proc' => ['processes' => 34]; // 3 sequences |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * We benchmarked the following solutions to read a file line by line: |
| 180 | + * - file |
| 181 | + * - SplFileObject |
| 182 | + * - fgets |
| 183 | + * - stream_get_line 🏆 |
| 184 | + */ |
| 185 | + public static function importFile(string $file): void |
| 186 | + { |
| 187 | + $namespace = sprintf('%s.%s', Utils::getDatabaseName(), Utils::getCollectionName()); |
| 188 | + |
| 189 | + $bulkWrite = new BulkWrite(); |
| 190 | + $fh = fopen($file, 'r'); |
| 191 | + while (($line = stream_get_line($fh, 10_000, "\n")) !== false) { |
| 192 | + $bulkWrite->insert(Document::fromJSON($line)); |
| 193 | + } |
| 194 | + |
| 195 | + fclose($fh); |
| 196 | + Utils::getClient()->getManager()->executeBulkWrite($namespace, $bulkWrite); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Using a method to regenerate the file names because we cannot cache the result of the method in a static |
| 201 | + * property. The benchmark runner will call the method in a different process, so the static property will not be |
| 202 | + * populated. |
| 203 | + */ |
| 204 | + private static function getFileNames(): array |
| 205 | + { |
| 206 | + $tempDir = sys_get_temp_dir() . '/mongodb-php-benchmark'; |
| 207 | + if (! is_dir($tempDir)) { |
| 208 | + mkdir($tempDir); |
| 209 | + } |
| 210 | + |
| 211 | + return array_map( |
| 212 | + static fn (int $i) => sprintf('%s/%03d.txt', $tempDir, $i), |
| 213 | + range(0, 99), |
| 214 | + ); |
| 215 | + } |
| 216 | +} |
0 commit comments