Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created May 18, 2012 15:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jmikola/2725976 to your computer and use it in GitHub Desktop.
Save jmikola/2725976 to your computer and use it in GitHub Desktop.
Doctrine MongoDB ODM flush benchmark
#!/usr/bin/env php
<?php
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\MongoDB\Connection;
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/Document.php';
$config = require_once __DIR__.'/config.php';
$dm = DocumentManager::create(new Connection(), $config);
$collection = $dm->getDocumentCollection('Document')->getMongoCollection();
$sizes = array_slice($argv, 1);
foreach ($sizes as $i) {
$preMem = memory_get_usage(true);
$preTime = microtime(true);
foreach (range(1, $i) as $j) {
$document = new Document();
$document->name = sprintf('doc-%d-%d', $i, $j);
$collection->insert(array('name' => $document->name));
}
$postMem = memory_get_usage(true);
$postTime = microtime(true);
$collection->drop();
printf("Inserting %d documents took %f seconds and used %d bytes\n", $i, $postTime - $preTime, $postMem - $preMem);
}
printf("%d bytes still allocated after benchmarking\n", memory_get_usage(true));
#!/usr/bin/env php
<?php
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\MongoDB\Connection;
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/Document.php';
$config = require_once __DIR__.'/config.php';
$dm = DocumentManager::create(new Connection(), $config);
$collection = $dm->getDocumentCollection('Document');
$sizes = array_slice($argv, 1);
foreach ($sizes as $i) {
$preMem = memory_get_usage(true);
$preTime = microtime(true);
foreach (range(1, $i) as $j) {
$document = new Document();
$document->name = sprintf('doc-%d-%d', $i, $j);
// See: https://github.com/doctrine/mongodb/issues/55
$a = array('name' => $document->name);
$collection->insert($a);
}
$postMem = memory_get_usage(true);
$postTime = microtime(true);
$collection->drop();
printf("Inserting %d documents took %f seconds and used %d bytes\n", $i, $postTime - $preTime, $postMem - $preMem);
}
printf("%d bytes still allocated after benchmarking\n", memory_get_usage(true));
#!/usr/bin/env php
<?php
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\MongoDB\Connection;
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/Document.php';
$config = require_once __DIR__.'/config.php';
$dm = DocumentManager::create(new Connection(), $config);
$sizes = array_slice($argv, 1);
foreach ($sizes as $i) {
$preMem = memory_get_usage(true);
$preTime = microtime(true);
foreach (range(1, $i) as $j) {
$document = new Document();
$document->name = sprintf('doc-%d-%d', $i, $j);
$dm->persist($document);
}
$dm->flush();
$postMem = memory_get_usage(true);
$postTime = microtime(true);
$dm->clear();
$dm->getDocumentCollection('Document')->drop();
printf("Flushing %d documents took %f seconds and used %d bytes\n", $i, $postTime - $preTime, $postMem - $preMem);
}
printf("%d bytes still allocated after benchmarking\n", memory_get_usage(true));
#!/usr/bin/env php
<?php
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\MongoDB\Connection;
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/Document.php';
$config = require_once __DIR__.'/config.php';
$dm = DocumentManager::create(new Connection(), $config);
$sizes = array_slice($argv, 1);
foreach ($sizes as $i) {
$preMem = memory_get_usage(true);
$preTime = microtime(true);
foreach (range(1, $i) as $j) {
$document = new Document();
$document->name = sprintf('doc-%d-%d', $i, $j);
$dm->createQueryBuilder('Document')
->insert()
->field('name')->set($document->name)
->getQuery()
->execute();
}
$postMem = memory_get_usage(true);
$postTime = microtime(true);
$dm->getDocumentCollection('Document')->drop();
printf("Inserting %d documents took %f seconds and used %d bytes\n", $i, $postTime - $preTime, $postMem - $preMem);
}
printf("%d bytes still allocated after benchmarking\n", memory_get_usage(true));
{
"require": {
"doctrine/mongodb-odm": "dev-master"
}
}
<?php
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\Configuration;
AnnotationDriver::registerAnnotationClasses();
$cacheDir = __DIR__;
$config = new Configuration();
$config->setProxyDir($cacheDir . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir($cacheDir . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__));
$config->setDefaultDB('benchmark');
return $config;
<?php
use Doctrine\ODM\MongoDB\Mapping\Annotations as Mongo;
/** @Mongo\Document(collection="documents") */
class Document
{
/** @Mongo\Id */
public $id;
/** @Mongo\String */
public $name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment