Skip to content

Add server description to the benchmark report #1163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions benchmark/BSON/DocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

namespace MongoDB\Benchmark\BSON;

use MongoDB\Benchmark\BaseBench;
use MongoDB\Benchmark\Fixtures\Data;
use MongoDB\BSON\Document;
use PhpBench\Attributes\BeforeMethods;
use PhpBench\Attributes\Warmup;
use stdClass;

use function file_get_contents;
use function iterator_to_array;

#[BeforeMethods('prepareData')]
final class DocumentBench extends BaseBench
#[Warmup(1)]
final class DocumentBench
{
private static Document $document;

public function prepareData(): void
{
self::$document = Document::fromJSON(file_get_contents(self::LARGE_FILE_PATH));
self::$document = Document::fromJSON(file_get_contents(Data::LARGE_FILE_PATH));
}

public function benchCheckFirst(): void
Expand Down
12 changes: 5 additions & 7 deletions benchmark/BSON/PackedArrayBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@

namespace MongoDB\Benchmark\BSON;

use MongoDB\Benchmark\BaseBench;
use MongoDB\Benchmark\Fixtures\Data;
use MongoDB\BSON\PackedArray;
use PhpBench\Attributes\BeforeMethods;
use PhpBench\Attributes\Warmup;

use function array_values;
use function file_get_contents;
use function iterator_to_array;
use function json_decode;

use const JSON_THROW_ON_ERROR;

#[BeforeMethods('prepareData')]
final class PackedArrayBench extends BaseBench
#[Warmup(1)]
final class PackedArrayBench
{
private static PackedArray $array;

public function prepareData(): void
{
$array = array_values(json_decode(file_get_contents(self::LARGE_FILE_PATH), true, 512, JSON_THROW_ON_ERROR));
$array = array_values(Data::readJsonFile(Data::LARGE_FILE_PATH));

self::$array = PackedArray::fromPHP($array);
}
Expand Down
43 changes: 0 additions & 43 deletions benchmark/BaseBench.php

This file was deleted.

74 changes: 74 additions & 0 deletions benchmark/Extension/EnvironmentProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace MongoDB\Benchmark\Extension;

use MongoDB\Benchmark\Utils;
use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use PhpBench\Environment\Information;
use PhpBench\Environment\ProviderInterface;

use function array_merge;
use function implode;
use function parse_url;
use function str_replace;

use const PHP_URL_PASS;

class EnvironmentProvider implements ProviderInterface
{
public function isApplicable(): bool
{
return true;
}

public function getInformation(): Information
{
$client = Utils::getClient();
$client->getManager()->selectServer();

return new Information('mongodb', array_merge(
$this->getUri(),
$this->getServerInfo($client->getManager()),
$this->getBuildInfo($client->getManager()),
));
}

private function getUri(): array
{
$uri = Utils::getUri();
// Obfuscate the password in the URI
$uri = str_replace(':' . parse_url($uri, PHP_URL_PASS) . '@', ':***@', $uri);

return ['uri' => $uri];
}

private function getServerInfo(Manager $manager): array
{
$topology = match ($manager->selectServer()->getType()) {
Server::TYPE_STANDALONE => 'standalone',
Server::TYPE_LOAD_BALANCER => 'load-balanced',
Server::TYPE_RS_PRIMARY => 'replica-set',
Server::TYPE_MONGOS => 'shared',
default => 'unknown',
};

return ['topology' => $topology];
}

private function getBuildInfo(Manager $manager): array
{
$buildInfo = $manager->executeCommand(
Utils::getDatabase(),
new Command(['buildInfo' => 1]),
new ReadPreference(ReadPreference::PRIMARY),
)->toArray()[0];

return [
'version' => $buildInfo->version ?? 'unknown server version',
'modules' => implode(', ', $buildInfo->modules ?? []),
];
}
}
25 changes: 25 additions & 0 deletions benchmark/Extension/MongoDBExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace MongoDB\Benchmark\Extension;

use PhpBench\DependencyInjection\Container;
use PhpBench\DependencyInjection\ExtensionInterface;
use PhpBench\Extension\RunnerExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MongoDBExtension implements ExtensionInterface
{
public function load(Container $container): void
{
$container->register(
EnvironmentProvider::class,
fn (Container $container) => new EnvironmentProvider(),
[RunnerExtension::TAG_ENV_PROVIDER => ['name' => 'mongodb']],
);
}

public function configure(OptionsResolver $resolver): void
{
// No config
}
}
20 changes: 20 additions & 0 deletions benchmark/Fixtures/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MongoDB\Benchmark\Fixtures;

use function file_get_contents;
use function json_decode;

use const JSON_THROW_ON_ERROR;

final class Data
{
public const LARGE_FILE_PATH = __DIR__ . '/data/large_doc.json';
public const SMALL_FILE_PATH = __DIR__ . '/data/small_doc.json';
public const TWEET_FILE_PATH = __DIR__ . '/data/tweet.json';

public static function readJsonFile(string $path): array
{
return json_decode(file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
}
}
1 change: 1 addition & 0 deletions benchmark/Fixtures/data/small_doc.json
Original file line number Diff line number Diff line change
@@ -0,0 +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}
21 changes: 10 additions & 11 deletions benchmark/ReadLargeDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MongoDB\Benchmark;

use Generator;
use MongoDB\Benchmark\Fixtures\Data;
use MongoDB\Benchmark\Fixtures\PassThruCodec;
use MongoDB\Benchmark\Fixtures\ToObjectCodec;
use MongoDB\BSON\Document;
Expand All @@ -11,27 +12,25 @@
use MongoDB\Model\BSONDocument;
use PhpBench\Attributes\BeforeClassMethods;
use PhpBench\Attributes\ParamProviders;
use PhpBench\Attributes\Warmup;

use function array_fill;
use function array_intersect_key;
use function assert;
use function file_get_contents;
use function is_array;
use function is_object;
use function json_decode;
use function sprintf;

use const JSON_THROW_ON_ERROR;

#[BeforeClassMethods('prepareDatabase')]
final class ReadLargeDocumentBench extends BaseBench
#[Warmup(1)]
final class ReadLargeDocumentBench
{
public static function prepareDatabase(): void
{
$collection = self::getCollection();
$collection = Utils::getCollection();
$collection->drop();

$document = json_decode(file_get_contents(self::LARGE_FILE_PATH), false, 512, JSON_THROW_ON_ERROR);
$document = Data::readJsonFile(Data::LARGE_FILE_PATH);

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

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

$collection = self::getCollection();
$collection = Utils::getCollection();

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

$collection = self::getCollection();
$collection = Utils::getCollection();

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

$collection = self::getCollection();
$collection = Utils::getCollection();

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

$collection = self::getCollection();
$collection = Utils::getCollection();

// Exhaust cursor and access identifier on each document
foreach ($collection->find([], $options) as $document) {
Expand Down
19 changes: 9 additions & 10 deletions benchmark/ReadMultipleDocumentsBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MongoDB\Benchmark;

use Generator;
use MongoDB\Benchmark\Fixtures\Data;
use MongoDB\Benchmark\Fixtures\PassThruCodec;
use MongoDB\Benchmark\Fixtures\ToObjectCodec;
use MongoDB\BSON\Document;
Expand All @@ -11,27 +12,25 @@
use MongoDB\Model\BSONDocument;
use PhpBench\Attributes\BeforeClassMethods;
use PhpBench\Attributes\ParamProviders;
use PhpBench\Attributes\Warmup;

use function array_fill;
use function array_intersect_key;
use function assert;
use function file_get_contents;
use function is_array;
use function is_object;
use function json_decode;
use function sprintf;

use const JSON_THROW_ON_ERROR;

#[BeforeClassMethods('prepareDatabase')]
final class ReadMultipleDocumentsBench extends BaseBench
#[Warmup(1)]
final class ReadMultipleDocumentsBench
{
public static function prepareDatabase(): void
{
$collection = self::getCollection();
$collection = Utils::getCollection();
$collection->drop();

$tweet = json_decode(file_get_contents(self::TWEET_FILE_PATH), false, 512, JSON_THROW_ON_ERROR);
$tweet = Data::readJsonFile(Data::TWEET_FILE_PATH);

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

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

$collection = self::getCollection();
$collection = Utils::getCollection();

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

$collection = self::getCollection();
$collection = Utils::getCollection();

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

$collection = self::getCollection();
$collection = Utils::getCollection();

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