Skip to content

Instantly share code, notes, and snippets.

@kchodorow
Created August 13, 2009 19:40
Show Gist options
  • Save kchodorow/167408 to your computer and use it in GitHub Desktop.
Save kchodorow/167408 to your computer and use it in GitHub Desktop.
<?php
// inserts 1,000,000 docs in 14 seconds on a MacBook Pro running Ubuntu 9.04
class Timer
{
var $start;
var $pause_time;
/* start the timer */
function __construct($start = 0)
{
if($start) { $this->start(); }
}
/* start the timer */
function start()
{
$this->start = $this->get_time();
$this->pause_time = 0;
}
/* get the current timer value */
function get($decimals = 8)
{
return round(($this->get_time() - $this->start),
$decimals);
}
}
$m = new Mongo( "localhost:27017");
$db = $m->selectDB("performance");
$c = $m->selectCollection("performance", "bulk");
$c->drop();
$AMOUNT_RECORDS = 8000;
$RECORD = array(
"component_version" => "X",
"state" => "Analyze",
"assignee" => "3002",
"snapshot_date" => "20080701",
"legacy_id" => "1318600",
"component_name" => "Something Core",
"priority" => "3");
function bulk_of_docs() {
global $AMOUNT_RECORDS;
global $RECORD;
$records = array();
for ($i=0; $i<$AMOUNT_RECORDS; $i++) {
$records[] = $RECORD;
}
return $records;
}
$times = 1000000/$AMOUNT_RECORDS;
$t = new Timer();
$t->start();
for ($i=0; $i<$times; $i++) {
$c->batchInsert(bulk_of_docs());
}
echo "inserted 1_000_000 documents in ".$t->get()."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment