-
Notifications
You must be signed in to change notification settings - Fork 266
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
Changes from all commits
dc033ae
8c63333
24fffe8
a970edb
e3cd6c8
d220bdb
951e6ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,8 @@ phpcs.xml | |
# psalm | ||
psalm.xml | ||
|
||
# phpbench | ||
.phpbench/ | ||
phpbench.json | ||
|
||
mongocryptd.pid |
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); | ||
} | ||
} |
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace MongoDB\Benchmark; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.