<?php
define('KEYS_COUNT', 50);
function deleteKeys($mc, $keys) {
foreach ($keys as $key) {
$mc->delete($key);
}
return true;
}
function testSet($mc, $data) {
$mt = microtime(true);
foreach ($data as $key => $value) {
$mc->set($key, $value);
}
$mt = microtime(true) - $mt;
return $mt;
}
function testMultiSet($mc, $data) {
$mt = microtime(true);
$mc->setMulti($data);
$mt = microtime(true) - $mt;
return $mt;
}
function testGet($mc, $keys, $dataTest) {
$data = array();
$mt = microtime(true);
foreach ($keys as $key) {
$data[$key] = $mc->get($key);
}
$mt = microtime(true) - $mt;
if ($data != $dataTest) {
throw new Exception('get error');
}
return $mt;
}
function testMultiGet($mc, $keys, $dataTest) {
$mt = microtime(true);
$data = $mc->getMulti($keys);
$mt = microtime(true) - $mt;
if ($data != $dataTest) {
throw new Exception('get error');
}
return $mt;
}
$data = array();
for ($i = 0; $i < KEYS_COUNT; $i++) {
$hash = md5($i);
$key = 'test:'.substr($hash, 0, 5);
$value = substr($hash, 10, 20);
$data[$key] = $value;
}
$keys = array_keys($data);
$mt = array();
$mc = new Memcached();
$mc->addServer('localhost', 11211);
deleteKeys($mc, $keys);
$mt['set'] = testSet($mc, $data);
$mt['get'] = testGet($mc, $keys, $data);
deleteKeys($mc, $keys);
$mt['multi-set'] = testMultiSet($mc, $data);
$mt['multi-get'] = testMultiGet($mc, $keys, $data);
foreach ($mt as &$m) {
$m = number_format($m * 1000000 / KEYS_COUNT, 0, ',', ' ');
}
$dSet = number_format($mt['set'] / $mt['multi-set'], 1, ',', ' ');
$dGet = number_format($mt['get'] / $mt['multi-get'], 1, ',', ' ');
?>
<h1>Test for <?php echo KEYS_COUNT; ?> keys</h1>
<table>
<tr>
<td></td>
<td><b>Single</b></td>
<td><b>Multi</b></td>
<td><b>/</b></td>
</tr>
<tr>
<td><b>Set</b></td>
<td><?php echo $mt['set']; ?></td>
<td><?php echo $mt['multi-set']; ?></td>
<td><?php echo $dSet; ?></td>
</tr>
<tr>
<td><b>Get</b></td>
<td><?php echo $mt['get']; ?></td>
<td><?php echo $mt['multi-get']; ?></td>
<td><?php echo $dGet; ?></td>
</tr>
</table>