Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-1237 Implement Parallel GridFS multi-file upload & download benchmarks #1170

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions benchmark/src/DriverBench/ParallelGridFSBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace MongoDB\Benchmark\DriverBench;

use MongoDB\Benchmark\Fixtures\Data;
use MongoDB\Benchmark\Utils;
use PhpBench\Attributes\BeforeMethods;

use function array_map;
use function basename;
use function fopen;
use function is_dir;
use function mkdir;
use function pcntl_fork;
use function pcntl_waitpid;
use function range;
use function sprintf;
use function stream_copy_to_stream;
use function sys_get_temp_dir;
use function unlink;

/**
* For accurate results, run benchmarks on a standalone server.
*
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#parallel
*/
#[AfterMethods('cleanup')]
final class ParallelGridFSBench
{
public static function cleanup(): void
{
Utils::getDatabase()->drop();

foreach (self::getFileNames() as $file) {
unlink($file);
}
}

/**
* GridFS multi-file upload
*
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#gridfs-multi-file-upload
*/
#[BeforeMethods('beforeUpload')]
public function benchUpload(): void
{
$pids = [];
foreach (self::getFileNames() as $file) {
$pid = pcntl_fork();
if ($pid === 0) {
Utils::getDatabase()->selectGridFSBucket()->uploadFromStream(basename($file), fopen($file, 'r'));

// Exit the child process
exit(0);
}

if ($pid === -1) {
throw new RuntimeException('Failed to fork');
}

// Keep the forked process id to wait for it later
$pids[$pid] = true;
}

// Wait for all child processes to finish
while ($pids !== []) {
$pid = pcntl_waitpid(-1, $status);
unset($pids[$pid]);
}
}

public function beforeUpload(): void
{
foreach (self::getFileNames() as $file) {
stream_copy_to_stream(Data::getStream(5 * 1024 * 1024), fopen($file, 'w'));
}

$database = Utils::getDatabase();
$database->drop();

$bucket = $database->selectGridFSBucket();
$bucket->uploadFromStream('init', Data::getStream(1));

Utils::reset();
}

/**
* GridFS multi-file download
*
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#gridfs-multi-file-download
*/
#[BeforeMethods('beforeDownload')]
public function benchDownload(): void
{
$pids = [];
foreach (self::getFileNames() as $file) {
$pid = pcntl_fork();
if ($pid === 0) {
$stream = Utils::getDatabase()
->selectGridFSBucket()
->openDownloadStreamByName(basename($file));
stream_copy_to_stream($stream, fopen($file, 'w'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a particular reason you didn't use Bucket::downloadToStream() here?

I wouldn't expect a performance difference since that method basically calls stream_copy_to_stream(). Just curious.

Copy link
Member Author

@GromNaN GromNaN Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason. Actually I missed downloadToStreamByName.

I'm not sure we need to switch the implement for this benchmark.


// Exit the child process
exit(0);
}

if ($pid === -1) {
throw new RuntimeException('Failed to fork');
}

// Keep the forked process id to wait for it later
$pids[$pid] = true;
}

// Wait for all child processes to finish
while ($pids !== []) {
$pid = pcntl_waitpid(-1, $status);
unset($pids[$pid]);
}
}

public function beforeDownload(): void
{
// Initialize the GridFS bucket with the files
$this->beforeUpload();
$this->benchUpload();
}

private static function getFileNames(): array
{
$tempDir = sys_get_temp_dir() . '/mongodb-php-benchmark';
Copy link
Member

@jmikola jmikola Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the same temp directory referenced in a previous PR. Would it make sense to use a constant or environment variable for this? I assume phpbench may have something like phpunit.xml for specifying env vars.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tmp directory is a system setting. It can be changed with sys_temp_dir ini setting or with the TMPDIR env var.
I don't think anyone will ever need to change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mostly referring to mongodb-php-benchmark since it's repeated across tests. No worries.

if (! is_dir($tempDir)) {
mkdir($tempDir);
}

return array_map(
static fn (int $i) => sprintf('%s/file%02d.txt', $tempDir, $i),
range(0, 49),
);
}
}