Skip to content

Commit d54f390

Browse files
committed
Fix file pattern, add bson, remove iteration overloading
1 parent d3248b4 commit d54f390

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

benchmark/BaseBench.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ protected static function getDatabase(): string
4747
return getenv('MONGODB_DATABASE') ?: 'phplib_test';
4848
}
4949

50-
protected static function readJsonFile(string $path): stdClass
50+
protected static function readJsonFile(string $path): array
5151
{
52-
return json_decode(file_get_contents($path), false, 512, JSON_THROW_ON_ERROR);
52+
return json_decode(file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
5353
}
5454
}

benchmark/DriverBench/SingleDocBench.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Generator;
66
use MongoDB\Benchmark\BaseBench;
7+
use MongoDB\BSON\Document;
78
use PhpBench\Attributes as Bench;
89

910
use function array_map;
11+
use function file_get_contents;
1012
use function range;
1113

1214
/** @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#single-doc-benchmarks */
@@ -18,22 +20,34 @@ public function prepareDatabase(): void
1820
self::getCollection()->drop();
1921
}
2022

21-
public function setupFindOneByID(): void
23+
public function setupFindOneById(): void
2224
{
23-
$tweet = (array) self::readJsonFile(self::TWEET_FILE_PATH);
25+
$tweet = self::readJsonFile(self::TWEET_FILE_PATH);
2426
$docs = array_map(fn ($id) => $tweet + ['_id' => $id], range(1, 10_000));
2527
self::getCollection()->insertMany($docs);
2628
}
2729

28-
#[Bench\BeforeMethods('setupFindOneByID')]
30+
public static function provideFindOneByIdParams(): Generator
31+
{
32+
yield 'default' => [
33+
'options' => [],
34+
];
35+
36+
yield 'bson typemap' => [
37+
'options' => ['typeMap' => ['root' => 'bson']],
38+
];
39+
}
40+
41+
/** @param array{options: array} $params */
42+
#[Bench\BeforeMethods('setupFindOneById')]
43+
#[Bench\ParamProviders('provideFindOneByIdParams')]
2944
#[Bench\Revs(1)]
30-
#[Bench\Iterations(10)]
31-
public function benchFindOneByID(): void
45+
public function benchFindOneById(array $params): void
3246
{
3347
$collection = self::getCollection();
3448

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

@@ -44,16 +58,25 @@ public static function provideInsertOneParams(): Generator
4458
'repeat' => 10_000,
4559
];
4660

61+
yield 'Small BSON doc' => [
62+
'document' => Document::fromJSON(file_get_contents(self::SMALL_FILE_PATH)),
63+
'repeat' => 10_000,
64+
];
65+
4766
yield 'Large doc' => [
4867
'document' => self::readJsonFile(self::LARGE_FILE_PATH),
4968
'repeat' => 10,
5069
];
70+
71+
yield 'Large BSON doc' => [
72+
'document' => Document::fromJSON(file_get_contents(self::LARGE_FILE_PATH)),
73+
'repeat' => 10,
74+
];
5175
}
5276

53-
/** @param array{document: object, repeat: int} $params */
77+
/** @param array{document: object|array, repeat: int} $params */
5478
#[Bench\ParamProviders('provideInsertOneParams')]
5579
#[Bench\Revs(1)]
56-
#[Bench\Iterations(1)]
5780
public function benchInsertOne(array $params): void
5881
{
5982
$collection = self::getCollection();

phpbench.json.dist

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

0 commit comments

Comments
 (0)