Skip to content

Commit fdd7793

Browse files
authored
Add server description to the benchmark report (#1163)
* Add server description to the report * Move warmup to each benchmark * Remove BaseBench for specialized static classes
1 parent 9fd7457 commit fdd7793

11 files changed

+197
-76
lines changed

benchmark/BSON/DocumentBench.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
namespace MongoDB\Benchmark\BSON;
44

5-
use MongoDB\Benchmark\BaseBench;
5+
use MongoDB\Benchmark\Fixtures\Data;
66
use MongoDB\BSON\Document;
77
use PhpBench\Attributes\BeforeMethods;
8+
use PhpBench\Attributes\Warmup;
89
use stdClass;
910

1011
use function file_get_contents;
1112
use function iterator_to_array;
1213

1314
#[BeforeMethods('prepareData')]
14-
final class DocumentBench extends BaseBench
15+
#[Warmup(1)]
16+
final class DocumentBench
1517
{
1618
private static Document $document;
1719

1820
public function prepareData(): void
1921
{
20-
self::$document = Document::fromJSON(file_get_contents(self::LARGE_FILE_PATH));
22+
self::$document = Document::fromJSON(file_get_contents(Data::LARGE_FILE_PATH));
2123
}
2224

2325
public function benchCheckFirst(): void

benchmark/BSON/PackedArrayBench.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22

33
namespace MongoDB\Benchmark\BSON;
44

5-
use MongoDB\Benchmark\BaseBench;
5+
use MongoDB\Benchmark\Fixtures\Data;
66
use MongoDB\BSON\PackedArray;
77
use PhpBench\Attributes\BeforeMethods;
8+
use PhpBench\Attributes\Warmup;
89

910
use function array_values;
10-
use function file_get_contents;
1111
use function iterator_to_array;
12-
use function json_decode;
13-
14-
use const JSON_THROW_ON_ERROR;
1512

1613
#[BeforeMethods('prepareData')]
17-
final class PackedArrayBench extends BaseBench
14+
#[Warmup(1)]
15+
final class PackedArrayBench
1816
{
1917
private static PackedArray $array;
2018

2119
public function prepareData(): void
2220
{
23-
$array = array_values(json_decode(file_get_contents(self::LARGE_FILE_PATH), true, 512, JSON_THROW_ON_ERROR));
21+
$array = array_values(Data::readJsonFile(Data::LARGE_FILE_PATH));
2422

2523
self::$array = PackedArray::fromPHP($array);
2624
}

benchmark/BaseBench.php

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace MongoDB\Benchmark\Extension;
4+
5+
use MongoDB\Benchmark\Utils;
6+
use MongoDB\Driver\Command;
7+
use MongoDB\Driver\Manager;
8+
use MongoDB\Driver\ReadPreference;
9+
use MongoDB\Driver\Server;
10+
use PhpBench\Environment\Information;
11+
use PhpBench\Environment\ProviderInterface;
12+
13+
use function array_merge;
14+
use function implode;
15+
use function parse_url;
16+
use function str_replace;
17+
18+
use const PHP_URL_PASS;
19+
20+
class EnvironmentProvider implements ProviderInterface
21+
{
22+
public function isApplicable(): bool
23+
{
24+
return true;
25+
}
26+
27+
public function getInformation(): Information
28+
{
29+
$client = Utils::getClient();
30+
$client->getManager()->selectServer();
31+
32+
return new Information('mongodb', array_merge(
33+
$this->getUri(),
34+
$this->getServerInfo($client->getManager()),
35+
$this->getBuildInfo($client->getManager()),
36+
));
37+
}
38+
39+
private function getUri(): array
40+
{
41+
$uri = Utils::getUri();
42+
// Obfuscate the password in the URI
43+
$uri = str_replace(':' . parse_url($uri, PHP_URL_PASS) . '@', ':***@', $uri);
44+
45+
return ['uri' => $uri];
46+
}
47+
48+
private function getServerInfo(Manager $manager): array
49+
{
50+
$topology = match ($manager->selectServer()->getType()) {
51+
Server::TYPE_STANDALONE => 'standalone',
52+
Server::TYPE_LOAD_BALANCER => 'load-balanced',
53+
Server::TYPE_RS_PRIMARY => 'replica-set',
54+
Server::TYPE_MONGOS => 'sharded',
55+
default => 'unknown',
56+
};
57+
58+
return ['topology' => $topology];
59+
}
60+
61+
private function getBuildInfo(Manager $manager): array
62+
{
63+
$buildInfo = $manager->executeCommand(
64+
Utils::getDatabase(),
65+
new Command(['buildInfo' => 1]),
66+
new ReadPreference(ReadPreference::PRIMARY),
67+
)->toArray()[0];
68+
69+
return [
70+
'version' => $buildInfo->version ?? 'unknown server version',
71+
'modules' => implode(', ', $buildInfo->modules ?? []),
72+
];
73+
}
74+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace MongoDB\Benchmark\Extension;
4+
5+
use PhpBench\DependencyInjection\Container;
6+
use PhpBench\DependencyInjection\ExtensionInterface;
7+
use PhpBench\Extension\RunnerExtension;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
class MongoDBExtension implements ExtensionInterface
11+
{
12+
public function load(Container $container): void
13+
{
14+
$container->register(
15+
EnvironmentProvider::class,
16+
fn (Container $container) => new EnvironmentProvider(),
17+
[RunnerExtension::TAG_ENV_PROVIDER => ['name' => 'mongodb']],
18+
);
19+
}
20+
21+
public function configure(OptionsResolver $resolver): void
22+
{
23+
// No config
24+
}
25+
}

benchmark/Fixtures/Data.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace MongoDB\Benchmark\Fixtures;
4+
5+
use function file_get_contents;
6+
use function json_decode;
7+
8+
use const JSON_THROW_ON_ERROR;
9+
10+
final class Data
11+
{
12+
public const LARGE_FILE_PATH = __DIR__ . '/data/large_doc.json';
13+
public const SMALL_FILE_PATH = __DIR__ . '/data/small_doc.json';
14+
public const TWEET_FILE_PATH = __DIR__ . '/data/tweet.json';
15+
16+
public static function readJsonFile(string $path): array
17+
{
18+
return json_decode(file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
19+
}
20+
}
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/ReadLargeDocumentBench.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Benchmark;
44

55
use Generator;
6+
use MongoDB\Benchmark\Fixtures\Data;
67
use MongoDB\Benchmark\Fixtures\PassThruCodec;
78
use MongoDB\Benchmark\Fixtures\ToObjectCodec;
89
use MongoDB\BSON\Document;
@@ -11,27 +12,25 @@
1112
use MongoDB\Model\BSONDocument;
1213
use PhpBench\Attributes\BeforeClassMethods;
1314
use PhpBench\Attributes\ParamProviders;
15+
use PhpBench\Attributes\Warmup;
1416

1517
use function array_fill;
1618
use function array_intersect_key;
1719
use function assert;
18-
use function file_get_contents;
1920
use function is_array;
2021
use function is_object;
21-
use function json_decode;
2222
use function sprintf;
2323

24-
use const JSON_THROW_ON_ERROR;
25-
2624
#[BeforeClassMethods('prepareDatabase')]
27-
final class ReadLargeDocumentBench extends BaseBench
25+
#[Warmup(1)]
26+
final class ReadLargeDocumentBench
2827
{
2928
public static function prepareDatabase(): void
3029
{
31-
$collection = self::getCollection();
30+
$collection = Utils::getCollection();
3231
$collection->drop();
3332

34-
$document = json_decode(file_get_contents(self::LARGE_FILE_PATH), false, 512, JSON_THROW_ON_ERROR);
33+
$document = Data::readJsonFile(Data::LARGE_FILE_PATH);
3534

3635
$documents = array_fill(0, 10, $document);
3736

@@ -86,7 +85,7 @@ public function benchCursorToArray(array $params): void
8685
{
8786
$options = array_intersect_key($params, ['codec' => true, 'typeMap' => true]);
8887

89-
$collection = self::getCollection();
88+
$collection = Utils::getCollection();
9089

9190
$collection->find([], $options)->toArray();
9291
}
@@ -96,7 +95,7 @@ public function benchAccessId(array $params): void
9695
{
9796
$options = array_intersect_key($params, ['codec' => true, 'typeMap' => true]);
9897

99-
$collection = self::getCollection();
98+
$collection = Utils::getCollection();
10099

101100
// Exhaust cursor and access identifier on each document
102101
foreach ($collection->find([], $options) as $document) {
@@ -109,7 +108,7 @@ public function benchAccessFirstField(array $params): void
109108
{
110109
$options = array_intersect_key($params, ['codec' => true, 'typeMap' => true]);
111110

112-
$collection = self::getCollection();
111+
$collection = Utils::getCollection();
113112

114113
// Exhaust cursor and access identifier on each document
115114
foreach ($collection->find([], $options) as $document) {
@@ -122,7 +121,7 @@ public function benchAccessLastField(array $params): void
122121
{
123122
$options = array_intersect_key($params, ['codec' => true, 'typeMap' => true]);
124123

125-
$collection = self::getCollection();
124+
$collection = Utils::getCollection();
126125

127126
// Exhaust cursor and access identifier on each document
128127
foreach ($collection->find([], $options) as $document) {

benchmark/ReadMultipleDocumentsBench.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Benchmark;
44

55
use Generator;
6+
use MongoDB\Benchmark\Fixtures\Data;
67
use MongoDB\Benchmark\Fixtures\PassThruCodec;
78
use MongoDB\Benchmark\Fixtures\ToObjectCodec;
89
use MongoDB\BSON\Document;
@@ -11,27 +12,25 @@
1112
use MongoDB\Model\BSONDocument;
1213
use PhpBench\Attributes\BeforeClassMethods;
1314
use PhpBench\Attributes\ParamProviders;
15+
use PhpBench\Attributes\Warmup;
1416

1517
use function array_fill;
1618
use function array_intersect_key;
1719
use function assert;
18-
use function file_get_contents;
1920
use function is_array;
2021
use function is_object;
21-
use function json_decode;
2222
use function sprintf;
2323

24-
use const JSON_THROW_ON_ERROR;
25-
2624
#[BeforeClassMethods('prepareDatabase')]
27-
final class ReadMultipleDocumentsBench extends BaseBench
25+
#[Warmup(1)]
26+
final class ReadMultipleDocumentsBench
2827
{
2928
public static function prepareDatabase(): void
3029
{
31-
$collection = self::getCollection();
30+
$collection = Utils::getCollection();
3231
$collection->drop();
3332

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

3635
$documents = array_fill(0, 1000, $tweet);
3736

@@ -86,7 +85,7 @@ public function benchCursorToArray(array $params): void
8685
{
8786
$options = array_intersect_key($params, ['codec' => true, 'typeMap' => true]);
8887

89-
$collection = self::getCollection();
88+
$collection = Utils::getCollection();
9089

9190
$collection->find([], $options)->toArray();
9291
}
@@ -96,7 +95,7 @@ public function benchAccessId(array $params): void
9695
{
9796
$options = array_intersect_key($params, ['codec' => true, 'typeMap' => true]);
9897

99-
$collection = self::getCollection();
98+
$collection = Utils::getCollection();
10099

101100
// Exhaust cursor and access identifier on each document
102101
foreach ($collection->find([], $options) as $document) {
@@ -109,7 +108,7 @@ public function benchAccessNestedItem(array $params): void
109108
{
110109
$options = array_intersect_key($params, ['codec' => true, 'typeMap' => true]);
111110

112-
$collection = self::getCollection();
111+
$collection = Utils::getCollection();
113112

114113
// Exhaust cursor and access identifier on each document
115114
foreach ($collection->find([], $options) as $document) {

0 commit comments

Comments
 (0)