Skip to content

Commit

Permalink
Merge branch '1.3.x'
Browse files Browse the repository at this point in the history
* 1.3.x:
  Add build stages for MongoDB 3.0+
  Extract coverage to separate build
  Fix wrong version number in MongoDB 4.0 build stage
  Move toolchain installation to install step
  Add performance stage to travis-ci
  Add basic benchmark tests
  Rework MongoDB installation to correctly install server versions
  Add note about ini settings for legacy drivers
  Add test stage for replica sets
  Only run sharding tests on sharded clusters
  • Loading branch information
alcaeus committed Sep 24, 2019
2 parents 7c3e82c + 41a9bc3 commit 3ac6f45
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 27 deletions.
65 changes: 38 additions & 27 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,42 @@ php:

env:
global:
- DEPLOYMENT=SHARDED_CLUSTER_RS
- KEY_SERVER="hkp://keyserver.ubuntu.com:80"
- MONGO_REPO_URI="https://repo.mongodb.org/apt/ubuntu"
- MONGO_REPO_TYPE="xenial/mongodb-org/"
- SOURCES_LOC="/etc/apt/sources.list.d/mongodb.list"
- DEPLOYMENT=STANDALONE
- DRIVER_VERSION="stable"
- ADAPTER_VERSION="^1.0.0"
- SERVER_DISTRO=ubuntu1604
- SERVER_VERSION=4.2.0
matrix:
- SERVER_VERSION="3.6" KEY_ID="2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5"
- SERVER_VERSION="4.0" KEY_ID="9DA31620334BD75D9DCB49F368818C72E52529D4"
- SERVER_VERSION="4.2" KEY_ID="E162F504A20CDF15827F718D4B7C549A058F8B6B"
- SERVER_VERSION="3.0.15"
- SERVER_VERSION="3.2.22"
- SERVER_VERSION="3.4.23"
- SERVER_VERSION="3.6.14"
- SERVER_VERSION="4.0.12"
- SERVER_VERSION="4.2.0"

jobs:
include:
# Test against lowest dependencies, including driver and server versions
- stage: Test
php: 7.2
env: SERVER_VERSION="3.6" KEY_ID="2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5" DRIVER_VERSION="1.5.0"
install:
- composer update --prefer-lowest
env: DRIVER_VERSION="1.5.0" COMPOSER_FLAGS="--prefer-lowest" SERVER_VERSION="3.0.15"

# Test sharded cluster functionality
- stage: Test
php: 7.3
env: DEPLOYMENT=SHARDED_CLUSTER_RS
script:
- ./vendor/bin/phpunit --group=sharding

# Test on replica sets
- stage: Test
php: 7.3
env: DEPLOYMENT=REPLICASET

# Run tests with coverage
- stage: Code Quality
php: 7.2
env: SERVER_VERSION="4.0" KEY_ID="9DA31620334BD75D9DCB49F368818C72E52529D4"
env: DEPLOYMENT=SHARDED_CLUSTER_RS
before_script:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
Expand All @@ -46,36 +58,35 @@ jobs:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

# Check coding standards
- stage: Code Quality
php: 7.2
env: SERVER_VERSION="4.0" KEY_ID="9DA31620334BD75D9DCB49F368818C72E52529D4"
script:
- vendor/bin/phpstan analyse

- stage: Code Quality
php: 7.2
env: SERVER_VERSION="4.0" KEY_ID="9DA31620334BD75D9DCB49F368818C72E52529D4"
script:
- vendor/bin/phpcs

# Performance tests
- stage: Performance
php: 7.3
script: vendor/bin/phpbench run --report=default --revs=100 --iterations=5 --report=aggregate

before_install:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
- sudo apt-key adv --keyserver ${KEY_SERVER} --recv ${KEY_ID}
- echo "deb ${MONGO_REPO_URI} ${MONGO_REPO_TYPE}${SERVER_VERSION} multiverse" | sudo tee ${SOURCES_LOC}
- sudo apt-get update -qq
- sudo apt-get --allow-unauthenticated install mongodb-org
- if nc -z localhost 27017; then sudo service mongod stop; fi

install:
- .travis/setup_mongodb.sh
- sudo pip install mongo-orchestration
- sudo mongo-orchestration start
- curl -XPUT http://localhost:8889/v1/sharded_clusters/myCluster --data @tests/sharded.json
- pecl install -f mongodb-${DRIVER_VERSION}
- .travis/setup_mo.sh
- composer self-update

install:
- composer update

script:
- pecl install -f mongodb-${DRIVER_VERSION}
- composer update ${COMPOSER_FLAGS}
- export DOCTRINE_MONGODB_SERVER=`cat /tmp/uri.txt`
- echo $DOCTRINE_MONGODB_SERVER
- vendor/bin/phpunit

script:
- ./vendor/bin/phpunit
2 changes: 2 additions & 0 deletions .travis/setup_mo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

echo Loading MO for $DEPLOYMENT

mongo-orchestration --version

if [[ -z $TRAVIS_BUILD_DIR ]]; then
export TRAVIS_BUILD_DIR=`pwd`;
fi
Expand Down
11 changes: 11 additions & 0 deletions .travis/setup_mongodb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

if nc -z localhost 27017
then
sudo service mongod stop
fi

export SERVER_FILENAME=mongodb-linux-x86_64-${SERVER_DISTRO}-${SERVER_VERSION}
wget -qO- http://fastdl.mongodb.org/linux/${SERVER_FILENAME}.tgz | tar xz
export PATH=${PWD}/${SERVER_FILENAME}/bin:${PATH}
mongod --version
91 changes: 91 additions & 0 deletions benchmark/BaseBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Benchmark;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use MongoDB\Client;
use MongoDB\Model\DatabaseInfo;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use function array_map;
use function getenv;
use function in_array;
use function iterator_to_array;

/**
* @BeforeMethods({"initDocumentManager", "clearDatabase"})
*/
abstract class BaseBench
{
public const DATABASE_NAME = 'doctrine_odm_performance';
private const DEFAULT_MONGODB_SERVER = 'mongodb://localhost:27017';

/** @var DocumentManager */
protected static $documentManager;

/**
* @return DocumentManager
*/
protected function getDocumentManager()
{
return self::$documentManager;
}

public function initDocumentManager()
{
$config = new Configuration();

$config->setProxyDir(__DIR__ . '/../../tests/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/../../tests/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setPersistentCollectionDir(__DIR__ . '/../../tests/PersistentCollections');
$config->setPersistentCollectionNamespace('PersistentCollections');
$config->setDefaultDB(self::DATABASE_NAME);
$config->setMetadataDriverImpl(self::createMetadataDriverImpl());
$config->setMetadataCacheImpl(new ArrayCache());

$client = new Client(
getenv('DOCTRINE_MONGODB_SERVER') ?: self::DEFAULT_MONGODB_SERVER,
[],
['typeMap' => ['root' => 'array', 'document' => 'array']]
);

self::$documentManager = DocumentManager::create($client, $config);
}

public function clearDatabase()
{
// Check if the database exists. Calling listCollections on a non-existing
// database in a sharded setup will cause an invalid command cursor to be
// returned
$client = self::$documentManager->getClient();
$databases = iterator_to_array($client->listDatabases());
$databaseNames = array_map(static function (DatabaseInfo $database) {
return $database->getName();
}, $databases);
if (! in_array(self::DATABASE_NAME, $databaseNames)) {
return;
}

$collections = $client->selectDatabase(self::DATABASE_NAME)->listCollections();

foreach ($collections as $collection) {
// See https://jira.mongodb.org/browse/SERVER-16541
if ($collection->getName() === 'system.indexes') {
continue;
}

$client->selectCollection(self::DATABASE_NAME, $collection->getName())->drop();
}
}

protected static function createMetadataDriverImpl()
{
return AnnotationDriver::create(__DIR__ . '/../tests/Documents');
}
}
122 changes: 122 additions & 0 deletions benchmark/Document/HydrateDocumentBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Benchmark\Document;

use Doctrine\ODM\MongoDB\Benchmark\BaseBench;
use Doctrine\ODM\MongoDB\Hydrator\HydratorInterface;
use Documents\User;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;

/**
* @BeforeMethods({"init"}, extend=true)
*/
final class HydrateDocumentBench extends BaseBench
{
/** @var array */
private static $data;

/** @var array */
private static $embedOneData;

/** @var array */
private static $embedManyData;

/** @var array */
private static $referenceOneData;

/** @var array */
private static $referenceManyData;

/** @var HydratorInterface */
private static $hydrator;

public function init()
{
self::$data = [
'_id' => new ObjectId(),
'username' => 'alcaeus',
'createdAt' => new UTCDateTime(),
];

self::$embedOneData = [
'address' => ['city' => 'Munich'],
];

self::$embedManyData = [
'phonenumbers' => [
['phonenumber' => '12345678'],
['phonenumber' => '12345678'],
],
];

self::$referenceOneData = [
'account' => [
'$ref' => 'Account',
'$id' => new ObjectId(),
],
];

self::$referenceManyData = [
'groups' => [
[
'$ref' => 'Group',
'$id' => new ObjectId(),
],
[
'$ref' => 'Group',
'$id' => new ObjectId(),
],
],
];

self::$hydrator = $this
->getDocumentManager()
->getHydratorFactory()
->getHydratorFor(User::class);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocument()
{
self::$hydrator->hydrate(new User(), self::$data);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocumentWithEmbedOne()
{
self::$hydrator->hydrate(new User(), self::$data + self::$embedOneData);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocumentWithEmbedMany()
{
self::$hydrator->hydrate(new User(), self::$data + self::$embedManyData);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocumentWithReferenceOne()
{
self::$hydrator->hydrate(new User(), self::$data + self::$referenceOneData);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocumentWithReferenceMany()
{
self::$hydrator->hydrate(new User(), self::$data + self::$referenceManyData);
}
}
Loading

0 comments on commit 3ac6f45

Please sign in to comment.