6
6
use MongoDB \Benchmark \BaseBench ;
7
7
use MongoDB \BSON \Document ;
8
8
use MongoDB \Driver \Command ;
9
- use PhpBench \Attributes as Bench ;
9
+ use MongoDB \Driver \WriteConcern ;
10
+ use PhpBench \Attributes \BeforeMethods ;
11
+ use PhpBench \Attributes \ParamProviders ;
12
+ use PhpBench \Attributes \Revs ;
10
13
11
14
use function array_map ;
12
15
use function file_get_contents ;
13
16
use function range ;
14
17
15
18
/** @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#single-doc-benchmarks */
16
- #[Bench \ BeforeMethods('prepareDatabase ' )]
19
+ #[BeforeMethods('prepareDatabase ' )]
17
20
final class SingleDocBench extends BaseBench
18
21
{
19
22
public function prepareDatabase (): void
@@ -31,6 +34,22 @@ public function benchRunCommand(): void
31
34
}
32
35
}
33
36
37
+ /**
38
+ * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#find-one-by-id
39
+ * @param array{options: array} $params
40
+ */
41
+ #[BeforeMethods('setupFindOneById ' )]
42
+ #[ParamProviders('provideFindOneByIdParams ' )]
43
+ #[Revs(1 )]
44
+ public function benchFindOneById (array $ params ): void
45
+ {
46
+ $ collection = self ::getCollection ();
47
+
48
+ for ($ id = 1 ; $ id <= 10_000 ; $ id ++) {
49
+ $ collection ->findOne (['_id ' => $ id ], $ params ['options ' ]);
50
+ }
51
+ }
52
+
34
53
public function setupFindOneById (): void
35
54
{
36
55
$ tweet = self ::readJsonFile (self ::TWEET_FILE_PATH );
@@ -50,18 +69,18 @@ public static function provideFindOneByIdParams(): Generator
50
69
}
51
70
52
71
/**
53
- * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#find-one-by-id
54
- * @param array{options: array} $params
72
+ * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#small-doc-insertone
73
+ * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#large-doc-bulk-insert
74
+ * @param array{document: object|array, repeat: int, options?: array} $params
55
75
*/
56
- #[Bench \BeforeMethods('setupFindOneById ' )]
57
- #[Bench \ParamProviders('provideFindOneByIdParams ' )]
58
- #[Bench \Revs(1 )]
59
- public function benchFindOneById (array $ params ): void
76
+ #[ParamProviders('provideInsertOneParams ' )]
77
+ #[Revs(1 )]
78
+ public function benchInsertOne (array $ params ): void
60
79
{
61
80
$ collection = self ::getCollection ();
62
81
63
- for ($ id = 1 ; $ id <= 10_000 ; $ id ++ ) {
64
- $ collection ->findOne ([ ' _id ' => $ id ], $ params ['options ' ]);
82
+ for ($ i = $ params [ ' repeat ' ] ; $ i > 0 ; $ i -- ) {
83
+ $ collection ->insertOne ( $ params [ ' document ' ], $ params ['options ' ] ?? [ ]);
65
84
}
66
85
}
67
86
@@ -77,6 +96,12 @@ public static function provideInsertOneParams(): Generator
77
96
'repeat ' => 10_000 ,
78
97
];
79
98
99
+ yield 'Small doc WC 0 ' => [
100
+ 'document ' => self ::readJsonFile (self ::SMALL_FILE_PATH ),
101
+ 'repeat ' => 10_000 ,
102
+ 'options ' => ['writeConcern ' => new WriteConcern (0 )],
103
+ ];
104
+
80
105
yield 'Large doc ' => [
81
106
'document ' => self ::readJsonFile (self ::LARGE_FILE_PATH ),
82
107
'repeat ' => 10 ,
@@ -86,21 +111,11 @@ public static function provideInsertOneParams(): Generator
86
111
'document ' => Document::fromJSON (file_get_contents (self ::LARGE_FILE_PATH )),
87
112
'repeat ' => 10 ,
88
113
];
89
- }
90
-
91
- /**
92
- * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#small-doc-insertone
93
- * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#large-doc-bulk-insert
94
- * @param array{document: object|array, repeat: int} $params
95
- */
96
- #[Bench \ParamProviders('provideInsertOneParams ' )]
97
- #[Bench \Revs(1 )]
98
- public function benchInsertOne (array $ params ): void
99
- {
100
- $ collection = self ::getCollection ();
101
114
102
- for ($ i = $ params ['repeat ' ]; $ i > 0 ; $ i --) {
103
- $ collection ->insertOne ($ params ['document ' ]);
104
- }
115
+ yield 'Large doc WC 0 ' => [
116
+ 'document ' => Document::fromJSON (file_get_contents (self ::LARGE_FILE_PATH )),
117
+ 'repeat ' => 10 ,
118
+ 'options ' => ['writeConcern ' => new WriteConcern (0 )],
119
+ ];
105
120
}
106
121
}
0 commit comments