Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created August 10, 2012 17:35
Show Gist options
  • Save jmikola/3315992 to your computer and use it in GitHub Desktop.
Save jmikola/3315992 to your computer and use it in GitHub Desktop.
Test batch insertion of documents and referenced GridFS files
#!/usr/bin/env php
<?php
require_once 'config.php'; // mongodb-odm/tools/sandbox/config.php
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* Test batch insertion of documents and referenced GridFS files.
*
* For each test iteration, a number (i.e. batch size) of documents and files
* will be persisted and flushed. Time and peak memory usage will be recorded
* between tests, with the collections dropped and DocumentManager cleared
* between tests.
*
* CLI arguments determine the size of the initial and final batches, and the
* step to increase the batch size between iterations.
*/
if (4 !== $argc) {
printf("Usage: %s [start] [end] [step]\n", basename(__FILE__));
exit;
}
$test = function($iterations) use ($dm) {
for ($i = 0; $i < $iterations; ++$i) {
$image = new Image();
$image->name = 'foo';
$image->file = __FILE__;
$profile = new Profile();
$profile->name = 'bar';
$profile->image = $image;
$dm->persist($profile);
}
$dm->flush();
};
$units = array('b', 'k', 'm', 'g', 't', 'p');
for ($i = $argv[1]; $i <= $argv[2]; $i += $argv[3]) {
$dm->getDocumentCollection('Profile')->drop();
$dm->getDocumentCollection('Image')->drop();
$start = microtime(true);
$test($i);
$elapsedTime = microtime(true) - $start;
$dm->clear();
$size = memory_get_peak_usage(true);
$peakMem = round($size / pow(1024, ($_ = floor(log($size, 1024)))), 2).$units[$_];
printf("size: %d, time: %f, peak: %s\n", $i, $elapsedTime, $peakMem);
}
/** @ODM\Document(collection="profiles") */
class Profile
{
/** @ODM\Id */
public $id;
/** @ODM\String */
public $name;
/** @ODM\ReferenceOne(targetDocument="Image", cascade={"all"}) */
public $image;
}
/** @ODM\Document(collection="images") */
class Image
{
/** @ODM\Id */
public $id;
/** @ODM\String */
public $name;
/** @ODM\File */
public $file;
}
@jmikola
Copy link
Author

jmikola commented Aug 10, 2012

To add some context to this gist, it's related to: https://jira.mongodb.org/browse/PHP-453

Thanks to a one-line optimization in UnitOfWork::computeAssociationChanges() (doctrine/mongodb-odm@6653a60), execution time is more than halved:

$ ./gridfs_batch.php 50000 50000 10000
size: 50000, time: 128.727638, peak: 667m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment