Skip to content

PHPLIB-1235 Implements Single-Doc Benchmarks #1161

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

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
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
114 changes: 114 additions & 0 deletions benchmark/DriverBench/SingleDocBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace MongoDB\Benchmark\DriverBench;

use Generator;
use MongoDB\Benchmark\Fixtures\Data;
use MongoDB\Benchmark\Utils;
use MongoDB\BSON\Document;
use MongoDB\Driver\Command;
use PhpBench\Attributes\BeforeMethods;
use PhpBench\Attributes\ParamProviders;
use PhpBench\Attributes\Revs;

use function array_map;
use function file_get_contents;
use function range;

/**
* For accurate results, run benchmarks on a standalone server.
*
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#single-doc-benchmarks
*/
#[BeforeMethods('prepareDatabase')]
final class SingleDocBench
{
public function prepareDatabase(): void
{
Utils::getCollection()->drop();
}

/** @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#run-command */
public function benchRunCommand(): void
{
$manager = Utils::getClient()->getManager();
$database = Utils::getDatabase();

for ($i = 0; $i < 10_000; $i++) {
$manager->executeCommand($database, new Command(['hello' => true]));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$manager->executeCommand($database, new Command(['hello' => true]));
$manager->executeCommand($database, new Command(['hello' => 1]));

The key doesn't matter for these commands, but we typically use 1 as a value for things like hello and ping.

I also agree with @alcaeus earlier comment that you can probably move Command construction out of the benchmark if you want to remove more unrelated overhead. Either a data provider or a class property might work.

Copy link
Member Author

@GromNaN GromNaN Sep 19, 2023

Choose a reason for hiding this comment

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

The spec orders to use true explicitly.

For the command object creation. I think it's part of the driver task to send a command. I ça add an additional bench with excluded command instantiation from the loop.

}
}

/**
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#find-one-by-id
* @param array{options: array} $params
*/
#[BeforeMethods('beforeFindOneById')]
#[ParamProviders('provideFindOneByIdParams')]
#[Revs(1)]
public function benchFindOneById(array $params): void
{
$collection = Utils::getCollection();

for ($id = 1; $id <= 10_000; $id++) {
$collection->findOne(['_id' => $id], $params['options']);
}
}

public function beforeFindOneById(): void
{
$tweet = Data::readJsonFile(Data::TWEET_FILE_PATH);
$docs = array_map(fn ($id) => $tweet + ['_id' => $id], range(1, 10_000));
Utils::getCollection()->insertMany($docs);
}

public static function provideFindOneByIdParams(): Generator
{
yield 'Driver default typemap' => [
'options' => [],
];

yield 'Raw BSON' => [
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/**
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#small-doc-insertone
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#large-doc-bulk-insert
* @param array{document: object|array, repeat: int, options?: array} $params
*/
#[ParamProviders('provideInsertOneParams')]
#[Revs(1)]
public function benchInsertOne(array $params): void
{
$collection = Utils::getCollection();

for ($i = $params['repeat']; $i > 0; $i--) {
$collection->insertOne($params['document']);
}
}

public static function provideInsertOneParams(): Generator
{
yield 'Small doc' => [
'document' => Data::readJsonFile(Data::SMALL_FILE_PATH),
'repeat' => 10_000,
];

yield 'Small BSON doc' => [
'document' => Document::fromJSON(file_get_contents(Data::SMALL_FILE_PATH)),
'repeat' => 10_000,
];

yield 'Large doc' => [
'document' => Data::readJsonFile(Data::LARGE_FILE_PATH),
'repeat' => 10,
];

yield 'Large BSON doc' => [
'document' => Document::fromJSON(file_get_contents(Data::LARGE_FILE_PATH)),
'repeat' => 10,
];
}
}
7 changes: 3 additions & 4 deletions benchmark/Extension/EnvironmentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ public function isApplicable(): bool

public function getInformation(): Information
{
$client = Utils::getClient();
$client->getManager()->selectServer();
$manager = Utils::getClient()->getManager();

return new Information('mongodb', array_merge(
$this->getUri(),
$this->getServerInfo($client->getManager()),
$this->getBuildInfo($client->getManager()),
$this->getServerInfo($manager),
$this->getBuildInfo($manager),
));
}

Expand Down