Skip to content

Commit 13f2431

Browse files
committed
PHPLIB-1235 Implements Single-Doc Benchmarks
1 parent 4e581cf commit 13f2431

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace MongoDB\Benchmark\DriverBench;
4+
5+
use Generator;
6+
use MongoDB\Benchmark\Fixtures\Data;
7+
use MongoDB\Benchmark\Utils;
8+
use MongoDB\BSON\Document;
9+
use MongoDB\Driver\Command;
10+
use PhpBench\Attributes\BeforeMethods;
11+
use PhpBench\Attributes\ParamProviders;
12+
use PhpBench\Attributes\Revs;
13+
14+
use function array_map;
15+
use function file_get_contents;
16+
use function range;
17+
18+
/**
19+
* For accurate results, run benchmarks on a standalone server.
20+
*
21+
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#single-doc-benchmarks
22+
*/
23+
#[BeforeMethods('prepareDatabase')]
24+
final class SingleDocBench
25+
{
26+
public function prepareDatabase(): void
27+
{
28+
Utils::getCollection()->drop();
29+
}
30+
31+
/** @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#run-command */
32+
public function benchRunCommand(): void
33+
{
34+
$manager = Utils::getClient()->getManager();
35+
$database = Utils::getDatabase();
36+
37+
for ($i = 0; $i < 10_000; $i++) {
38+
$manager->executeCommand($database, new Command(['hello' => true]));
39+
}
40+
}
41+
42+
/**
43+
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#find-one-by-id
44+
* @param array{options: array} $params
45+
*/
46+
#[BeforeMethods('setupFindOneById')]
47+
#[ParamProviders('provideFindOneByIdParams')]
48+
#[Revs(1)]
49+
public function benchFindOneById(array $params): void
50+
{
51+
$collection = Utils::getCollection();
52+
53+
for ($id = 1; $id <= 10_000; $id++) {
54+
$collection->findOne(['_id' => $id], $params['options']);
55+
}
56+
}
57+
58+
public function setupFindOneById(): void
59+
{
60+
$tweet = Data::readJsonFile(Data::TWEET_FILE_PATH);
61+
$docs = array_map(fn ($id) => $tweet + ['_id' => $id], range(1, 10_000));
62+
Utils::getCollection()->insertMany($docs);
63+
}
64+
65+
public static function provideFindOneByIdParams(): Generator
66+
{
67+
yield 'Driver default typemap' => [
68+
'options' => [],
69+
];
70+
71+
yield 'Raw BSON' => [
72+
'options' => ['typeMap' => ['root' => 'bson']],
73+
];
74+
}
75+
76+
/**
77+
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#small-doc-insertone
78+
* @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#large-doc-bulk-insert
79+
* @param array{document: object|array, repeat: int, options?: array} $params
80+
*/
81+
#[ParamProviders('provideInsertOneParams')]
82+
#[Revs(1)]
83+
public function benchInsertOne(array $params): void
84+
{
85+
$collection = Utils::getCollection();
86+
87+
for ($i = $params['repeat']; $i > 0; $i--) {
88+
$collection->insertOne($params['document'], $params['options'] ?? []);
89+
}
90+
}
91+
92+
public static function provideInsertOneParams(): Generator
93+
{
94+
yield 'Small doc' => [
95+
'document' => Data::readJsonFile(Data::SMALL_FILE_PATH),
96+
'repeat' => 10_000,
97+
];
98+
99+
yield 'Small BSON doc' => [
100+
'document' => Document::fromJSON(file_get_contents(Data::SMALL_FILE_PATH)),
101+
'repeat' => 10_000,
102+
];
103+
104+
yield 'Large doc' => [
105+
'document' => Data::readJsonFile(Data::LARGE_FILE_PATH),
106+
'repeat' => 10,
107+
];
108+
109+
yield 'Large BSON doc' => [
110+
'document' => Document::fromJSON(file_get_contents(Data::LARGE_FILE_PATH)),
111+
'repeat' => 10,
112+
];
113+
}
114+
}

0 commit comments

Comments
 (0)