Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 171 lines (149 sloc) 4.112 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
<?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);
echo '.';
break;
}

clearstatcache();
$beforeCompact = array(
'stats' => $db->send('get', '/benchmark_db'),
'fileSize' => filesize('/usr/local/var/lib/couchdb/benchmark_db.couch'),
);

$compactStart = microtime(true);
$r = $db->send('post', '/benchmark_db/_compact');
while ($status = $db->send('get', '/benchmark_db')) {
if (!$status['compact_running']) {
break;
}
echo 'x';
usleep(1000000);
}
$compactEnd = microtime(true);

clearstatcache();
$afterCompact = array(
'stats' => $db->send('get', '/benchmark_db'),
'fileSize' => filesize('/usr/local/var/lib/couchdb/benchmark_db.couch'),
);

echo "\n\n";
echo sprintf(
"doc count (before compact): %s\n".
"doc count (after compact): %s\n".
"insert time: %s sec\n".
"insert time / doc: %s ms\n".
"compact time: %s sec\n".
"compact time / doc: %s ms\n".
"disk size (before compact): %s bytes\n".
"disk size (after compact): %s bytes\n".
".couch size (before compact): %s bytes\n".
".couch size (after compact): %s bytes\n".
".couch size / doc (before compact): %s bytes\n".
".couch size / doc (after compact): %s bytes\n\n",

$beforeCompact['stats']['doc_count'],
$afterCompact['stats']['doc_count'],
round($insertEnd - $insertStart, 4),
($beforeCompact['stats']['doc_count'])
? round((($insertEnd - $insertStart) * 1000) / $beforeCompact['stats']['doc_count'], 2)
: 'n/a',
round($compactEnd - $compactStart, 4),
($beforeCompact['stats']['doc_count'])
? round((($compactEnd - $compactStart) * 1000) / $beforeCompact['stats']['doc_count'], 2)
: 'n/a',
$beforeCompact['stats']['disk_size'],
$afterCompact['stats']['disk_size'],
$beforeCompact['fileSize'],
$afterCompact['fileSize'],
($beforeCompact['stats']['doc_count'])
? round($beforeCompact['fileSize'] / $beforeCompact['stats']['doc_count'], 2)
: 'n/a',
($afterCompact['stats']['doc_count'])
? round($afterCompact['fileSize'] / $afterCompact['stats']['doc_count'], 2)
: 'n/a'
);
}

class CouchDb {
public $config = array(
'host' => 'localhost',
'port' => 5984
);

public function __construct($config = array()) {
$this->config = $config + $this->config;
}

public function send($method, $resource, $document = array()) {
$url = sprintf(
'http://%s:%s%s',
$this->config['host'],
$this->config['port'],
$resource
);

$curlOptions = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
);

if (!empty($document)) {
$curlOptions[CURLOPT_POSTFIELDS] = json_encode($document);
}


$curl = curl_init($url);
curl_setopt_array($curl, $curlOptions);
$r = curl_exec($curl);

return json_decode($r, true);
}

public static function uuid() {
return substr(sha1(uniqid(mt_rand(), true)), 0, 32);

}
}

?>
Something went wrong with that request. Please try again.