<?php

$config = array('method' => 'bulk_insert', 'insertCounts' => array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50, 100, 500, 1000, 2500, 5000, 7500, 10000, 25000, 50000, 100000, 250000, 500000, 750000, 1000000));
$db = new CouchDb();
foreach ($config['insertCounts'] as $docCount) {
    // Re-create the database for each attempt
    $db->send('delete', '/benchmark_db');
    $db->send('put', '/benchmark_db');
    echo sprintf("-> %s %d docs:\n", $method, $docCount);
    switch ($config['method']) {
        case 'bulk_insert':
            $insertStart = microtime(true);
            $docsWritten = 0;
            while ($docsWritten < $docCount) {
                $insertAtOnce = $docCount - $docsWritten > 1000 ? 1000 : $docCount - $docsWritten;
                $docs = array();
                for ($i = 0; $i < $insertAtOnce; $i++) {
                    $docs[] = array('_id' => CouchDb::uuid(), 'foo' => 'bar');
                }
                $db->send('post', '/benchmark_db/_bulk_docs', compact('docs'));
                $docsWritten = $docsWritten + $insertAtOnce;
                echo '.';
            }
            $insertEnd = microtime(true);
            break;
        case 'single_insert':
            $insertStart = microtime(true);
            for ($i = 0; $i < $docCount; $i++) {
                $db->send('put', sprintf('/benchmark_db/%s', CouchDb::uuid()), array('foo' => 'bar'));
            }
            $insertEnd = microtime(true);
 /**
  * Drop database
  * 
  * @param  string $db 
  * @return Phly_Couch_Result
  * @throws Phly_Couch_Exception when fails
  */
 public function drop(CouchDb $db)
 {
     $name = $db->getName();
     $request = new CouchRequest('DELETE', $name);
     $response = $this->send($request);
     if (!$response->isSuccessful()) {
         throw new CouchException(sprintf('Failed dropping database "%s"; received response code "%s"', $db, (string) $response->getStatus()));
     }
     unset($this->databases[$name]);
 }