4
4
5
5
use Generator ;
6
6
use MongoDB \Benchmark \BaseBench ;
7
+ use MongoDB \BSON \Document ;
7
8
use PhpBench \Attributes as Bench ;
8
9
9
10
use function array_map ;
11
+ use function file_get_contents ;
10
12
use function range ;
11
13
12
14
/** @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#single-doc-benchmarks */
@@ -18,22 +20,34 @@ public function prepareDatabase(): void
18
20
self ::getCollection ()->drop ();
19
21
}
20
22
21
- public function setupFindOneByID (): void
23
+ public function setupFindOneById (): void
22
24
{
23
- $ tweet = ( array ) self ::readJsonFile (self ::TWEET_FILE_PATH );
25
+ $ tweet = self ::readJsonFile (self ::TWEET_FILE_PATH );
24
26
$ docs = array_map (fn ($ id ) => $ tweet + ['_id ' => $ id ], range (1 , 10_000 ));
25
27
self ::getCollection ()->insertMany ($ docs );
26
28
}
27
29
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 ' )]
29
44
#[Bench \Revs(1 )]
30
- #[Bench \Iterations(10 )]
31
- public function benchFindOneByID (): void
45
+ public function benchFindOneById (array $ params ): void
32
46
{
33
47
$ collection = self ::getCollection ();
34
48
35
49
for ($ id = 1 ; $ id <= 10_000 ; $ id ++) {
36
- $ collection ->findOne (['_id ' => $ id ]);
50
+ $ collection ->findOne (['_id ' => $ id ], $ params [ ' options ' ] );
37
51
}
38
52
}
39
53
@@ -44,16 +58,25 @@ public static function provideInsertOneParams(): Generator
44
58
'repeat ' => 10_000 ,
45
59
];
46
60
61
+ yield 'Small BSON doc ' => [
62
+ 'document ' => Document::fromJSON (file_get_contents (self ::SMALL_FILE_PATH )),
63
+ 'repeat ' => 10_000 ,
64
+ ];
65
+
47
66
yield 'Large doc ' => [
48
67
'document ' => self ::readJsonFile (self ::LARGE_FILE_PATH ),
49
68
'repeat ' => 10 ,
50
69
];
70
+
71
+ yield 'Large BSON doc ' => [
72
+ 'document ' => Document::fromJSON (file_get_contents (self ::LARGE_FILE_PATH )),
73
+ 'repeat ' => 10 ,
74
+ ];
51
75
}
52
76
53
- /** @param array{document: object, repeat: int} $params */
77
+ /** @param array{document: object|array , repeat: int} $params */
54
78
#[Bench \ParamProviders('provideInsertOneParams ' )]
55
79
#[Bench \Revs(1 )]
56
- #[Bench \Iterations(1 )]
57
80
public function benchInsertOne (array $ params ): void
58
81
{
59
82
$ collection = self ::getCollection ();
0 commit comments