Skip to content

Instantly share code, notes, and snippets.

@thbourlove
Forked from jmikola/foo.php
Last active December 22, 2015 17:09
Show Gist options
  • Save thbourlove/6504323 to your computer and use it in GitHub Desktop.
Save thbourlove/6504323 to your computer and use it in GitHub Desktop.
<?php
$m = new MongoClient('mongodb://admin:admin@192.168.108.19:27017');
$c = $m->findone->foo;
$c->drop();
$c->insert(['_id' => 1, 'x' => str_repeat('z', 1000)]);
$c->insert(['_id' => 2, 'x' => str_repeat('z', 1000)]);
$c->insert(['_id' => 3, 'x' => str_repeat('z', 1000)]);
$c->insert(['_id' => 4, 'x' => str_repeat('z', 1000)]);
$c->insert(['_id' => 5, 'x' => str_repeat('z', 1000)]);
$t = function ($iterations, $covered) use ($c) {
printf("Testing with%s covered index queries\n", $covered ? '' : 'out');
$projection = $covered ? ['_id' => 1] : [];
printf(" %d iterations: findOne()\n", $iterations);
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$id = $i % 5 + 1;
$r = $c->findOne(['_id' => $id], $projection);
}
$end = microtime(true);
printf(" Total time: %f\n", $end - $start);
printf(" Average time: %f\n", ($end - $start) / $iterations);
printf(" %d iterations: find()->limit(1)->getNext()\n", $iterations);
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$id = $i % 5 + 1;
$r = $c->find(['_id' => $id], $projection)->limit(1)->getNext();
}
$end = microtime(true);
printf(" Total time: %f\n", $end - $start);
printf(" Average time: %f\n", ($end - $start) / $iterations);
};
$t(10, true);
$t(10, false);
$t(100, true);
$t(100, false);
$t(1000, true);
$t(1000, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment