Skip to content

PHPLIB-1183: Initial performance tests #1141

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 7 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
.* export-ignore
*.md export-ignore
tests export-ignore
benchmark export-ignore
docs export-ignore
examples export-ignore
mongo-orchestration export-ignore
stubs export-ignore
tools export-ignore
Makefile export-ignore
phpbench.json.dist export-ignore
phpcs.xml.dist export-ignore
phpunit.evergreen.xml export-ignore
phpunit.xml.dist export-ignore
Expand Down
77 changes: 77 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: "Performance Benchmark"

on:
pull_request:
branches:
- "v*.*"
- "master"
- "feature/*"
paths-ignore:
- "docs/**"
push:
branches:
- "v*.*"
- "master"
- "feature/*"
paths-ignore:
- "docs/**"

env:
PHP_VERSION: "8.2"
DRIVER_VERSION: "stable"

jobs:
psalm:
name: "phpbench"
runs-on: "ubuntu-22.04"

steps:
- name: "Checkout"
uses: "actions/checkout@v3"

- id: setup-mongodb
uses: mongodb-labs/drivers-evergreen-tools@master
with:
version: "6.0"
topology: "server"
skip-legacy-shell: true

- name: Setup cache environment
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ env.PHP_VERSION }}
extensions: "mongodb-${{ ENV.DRIVER_VERSION }}"
key: "extcache-v1"

- name: Cache extensions
uses: actions/cache@v3
with:
path: ${{ steps.extcache.outputs.dir }}
key: ${{ steps.extcache.outputs.key }}
restore-keys: ${{ steps.extcache.outputs.key }}

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
extensions: "mongodb-${{ ENV.DRIVER_VERSION }}"
php-version: "${{ env.PHP_VERSION }}"

- name: "Show driver information"
run: "php --ri mongodb"

- name: "Install dependencies with Composer"
uses: "ramsey/[email protected]"
with:
composer-options: "--no-suggest"

- name: "Run phpbench"
run: "vendor/bin/phpbench run --report=aggregate --report=bar_chart_time --output html"

- name: Upload HTML report
uses: actions/upload-artifact@v3
with:
name: phpbench-${{ github.sha }}.html
path: .phpbench/html/index.html
retention-days: 3
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ phpcs.xml
# psalm
psalm.xml

# phpbench
.phpbench/
phpbench.json

mongocryptd.pid
77 changes: 77 additions & 0 deletions benchmark/BSON/DocumentBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace MongoDB\Benchmark\BSON;

use MongoDB\Benchmark\BaseBench;
use MongoDB\BSON\Document;
use PhpBench\Attributes\BeforeMethods;

use function file_get_contents;
use function iterator_to_array;

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

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

public function benchCheckFirst(): void
{
self::$document->has('qx3MigjubFSm');
}

public function benchCheckLast(): void
{
self::$document->has('Zz2MOlCxDhLl');
}

public function benchAccessFirst(): void
{
self::$document->get('qx3MigjubFSm');
}

public function benchAccessLast(): void
{
self::$document->get('Zz2MOlCxDhLl');
}

public function benchIteratorToArray(): void
{
iterator_to_array(self::$document);
}

public function benchToPHPObject(): void
{
self::$document->toPHP();
}

public function benchToPHPArray(): void
{
self::$document->toPHP(['root' => 'array']);
}

public function benchIteration(): void
{
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
// phpcs:ignore Generic.ControlStructures.InlineControlStructure.NotAllowed
foreach (self::$document as $key => $value);
}

public function benchIterationAsArray(): void
{
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
// phpcs:ignore Generic.ControlStructures.InlineControlStructure.NotAllowed
foreach (self::$document->toPHP(['root' => 'array']) as $key => $value);
}

public function benchIterationAsObject(): void
{
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
// phpcs:ignore Generic.ControlStructures.InlineControlStructure.NotAllowed
foreach (self::$document->toPHP() as $key => $value);
}
}
78 changes: 78 additions & 0 deletions benchmark/BSON/PackedArrayBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace MongoDB\Benchmark\BSON;

use MongoDB\Benchmark\BaseBench;
use MongoDB\BSON\PackedArray;
use PhpBench\Attributes\BeforeMethods;

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
{
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));

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

public function benchCheckFirst(): void
{
self::$array->has(0);
}

public function benchCheckLast(): void
{
self::$array->has(94354);
}

public function benchAccessFirst(): void
{
self::$array->get(0);
}

public function benchAccessLast(): void
{
self::$array->get(94354);
}

public function benchIteratorToArray(): void
{
iterator_to_array(self::$array);
}

public function benchToPHPArray(): void
{
self::$array->toPHP();
}

public function benchIteration(): void
{
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
// phpcs:ignore Generic.ControlStructures.InlineControlStructure.NotAllowed
foreach (self::$array as $key => $value);
}

public function benchIterationAfterIteratorToArray(): void
{
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
// phpcs:ignore Generic.ControlStructures.InlineControlStructure.NotAllowed
foreach (iterator_to_array(self::$array) as $key => $value);
}

public function benchIterationAsArray(): void
{
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
// phpcs:ignore Generic.ControlStructures.InlineControlStructure.NotAllowed
foreach (self::$array->toPHP() as $key => $value);
}
}
43 changes: 43 additions & 0 deletions benchmark/BaseBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MongoDB\Benchmark;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file missing a copyright block?

Do we actually need those for benchmarks? We don't use it in our test files.

Copy link
Member Author

@alcaeus alcaeus Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use them for tests, so I omitted them here as well.


Edit: I noted other benchmark files included them. I removed them everywhere, as strictly speaking the copyright is already noted in the repository anyways.


use MongoDB\Client;
use MongoDB\Collection;

use function getenv;

abstract class BaseBench
{
protected const LARGE_FILE_PATH = __DIR__ . '/data/large_doc.json';
protected const TWEET_FILE_PATH = __DIR__ . '/data/tweet.json';

private static ?Collection $collection;

protected static function getCollection(): Collection
{
return self::$collection ??= self::createCollection();
}

protected static function createClient(array $options = [], array $driverOptions = []): Client
{
return new Client(self::getUri(), $options, $driverOptions);
}

public static function createCollection(): Collection
{
$client = self::createClient();

return $client->selectCollection(self::getDatabase(), 'perftest');
}

protected static function getUri(): string
{
return getenv('MONGODB_URI') ?: 'mongodb://localhost:27017/';
}

protected static function getDatabase(): string
{
return getenv('MONGODB_DATABASE') ?: 'phplib_test';
}
}
Loading