Skip to content

Commit 17b5b74

Browse files
committed
PHPLIB-1235 Implements Single-Doc Benchmarks
1 parent 7589840 commit 17b5b74

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

benchmark/BSON/PackedArrayBench.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class PackedArrayBench extends BaseBench
2020

2121
public function prepareData(): void
2222
{
23-
$array = array_values(json_decode(file_get_contents(self::LARGE_FILE_PATH), true, 512, JSON_THROW_ON_ERROR));
23+
$array = array_values(self::readJsonFile(self::LARGE_FILE_PATH));
2424

2525
self::$array = PackedArray::fromPHP($array);
2626
}

benchmark/BaseBench.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44

55
use MongoDB\Client;
66
use MongoDB\Collection;
7+
use stdClass;
78

9+
use function file_get_contents;
810
use function getenv;
11+
use function json_decode;
12+
13+
use const JSON_THROW_ON_ERROR;
914

1015
abstract class BaseBench
1116
{
1217
protected const LARGE_FILE_PATH = __DIR__ . '/Fixtures/data/large_doc.json';
18+
protected const SMALL_FILE_PATH = __DIR__ . '/Fixtures/data/small_doc.json';
1319
protected const TWEET_FILE_PATH = __DIR__ . '/Fixtures/data/tweet.json';
1420

1521
private static ?Collection $collection;
@@ -40,4 +46,9 @@ protected static function getDatabase(): string
4046
{
4147
return getenv('MONGODB_DATABASE') ?: 'phplib_test';
4248
}
49+
50+
protected static function readJsonFile(string $path): stdClass
51+
{
52+
return json_decode(file_get_contents($path), false, 512, JSON_THROW_ON_ERROR);
53+
}
4354
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace MongoDB\Benchmark\DriverBench;
4+
5+
use Generator;
6+
use MongoDB\Benchmark\BaseBench;
7+
use PhpBench\Attributes as Bench;
8+
9+
use function array_map;
10+
use function range;
11+
12+
/** @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#single-doc-benchmarks */
13+
#[Bench\BeforeMethods('prepareDatabase')]
14+
final class SingleDocBench extends BaseBench
15+
{
16+
public function prepareDatabase(): void
17+
{
18+
self::getCollection()->drop();
19+
}
20+
21+
public function setupFindOneByID(): void
22+
{
23+
$tweet = (array) self::readJsonFile(self::TWEET_FILE_PATH);
24+
$docs = array_map(fn ($id) => $tweet + ['_id' => $id], range(1, 10_000));
25+
self::getCollection()->insertMany($docs);
26+
}
27+
28+
#[Bench\BeforeMethods('setupFindOneByID')]
29+
#[Bench\Revs(1)]
30+
#[Bench\Iterations(10)]
31+
public function benchFindOneByID(): void
32+
{
33+
$collection = self::getCollection();
34+
35+
for ($id = 1; $id <= 10_000; $id++) {
36+
$collection->findOne(['_id' => $id]);
37+
}
38+
}
39+
40+
public static function provideInsertOneParams(): Generator
41+
{
42+
yield 'Small doc' => [
43+
'document' => self::readJsonFile(self::SMALL_FILE_PATH),
44+
'repeat' => 10_000,
45+
];
46+
47+
yield 'Large doc' => [
48+
'document' => self::readJsonFile(self::LARGE_FILE_PATH),
49+
'repeat' => 10,
50+
];
51+
}
52+
53+
/** @param array{document: object, repeat: int} $params */
54+
#[Bench\ParamProviders('provideInsertOneParams')]
55+
#[Bench\Revs(1)]
56+
#[Bench\Iterations(1)]
57+
public function benchInsertOne(array $params): void
58+
{
59+
$collection = self::getCollection();
60+
61+
for ($i = $params['repeat']; $i > 0; $i--) {
62+
$collection->insertOne($params['document']);
63+
}
64+
}
65+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"oggaoR4O":"miNVpaKW","Vxri7mmI":"CS5VwrwN","IQ8K4ZMG":"Oq5Csk1w","WzI8s1W0":"ZPm57dhu","E5Aj2zB3":"gxUpzIjg","3MXe8Wi7":"Smo9whci","PHPSSV51":"TW34kfzq","CxSCo4jD":55336395,"5C8GSDC5":41992681,"fC63DsLR":72188733,"l6e0U4bR":46660880,"TLRpkltp":3527055,"Ph9CZN5L":74094448}

benchmark/ReadMultipleDocumentsBench.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function prepareDatabase(): void
3131
$collection = self::getCollection();
3232
$collection->drop();
3333

34-
$tweet = json_decode(file_get_contents(self::TWEET_FILE_PATH), false, 512, JSON_THROW_ON_ERROR);
34+
$tweet = self::readJsonFile(self::TWEET_FILE_PATH);
3535

3636
$documents = array_fill(0, 1000, $tweet);
3737

phpbench.json.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"$schema": "./vendor/phpbench/phpbench/phpbench.schema.json",
33
"runner.bootstrap": "vendor/autoload.php",
4+
"runner.file_pattern": ".*Bench.php$",
45
"runner.path": "benchmark",
56
"runner.iterations": 3,
67
"runner.revs": 10,

0 commit comments

Comments
 (0)