-
Notifications
You must be signed in to change notification settings - Fork 266
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} | ||
|
||
/** | ||
* @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, | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key doesn't matter for these commands, but we typically use
1
as a value for things likehello
andping
.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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.