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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
<?php
/*
* Runs a series of benchmarks and prints the results
*/
error_reporting(E_ALL);
ini_set('memcache.hash_strategy', 'consistent');
//ini_set('memcache.protocol', 'binary');
$threshold = 0;
$count = 50; // Keys per interation (multi-key operations do $count keys at a time)
$reps = 1000; // Number of iterations
// hostname, tcp_port, udp_port
$hosts = array(
array('localhost', 11211, 11211),
array('localhost', 11212, 11212),
);
$pools = array();
$tests1 = array(
array('test_set_single', 'Single-Set'),
array('test_set_single_obj', 'Set-Object'),
array('test_incr_single', 'Single-Incr'),
array('test_delete_single', 'Single-Del'),
);
$tests2 = array(
array('test_get_single', 'Single-Get'),
array('test_get_multi', 'Multi-Get'),
array('test_get_single_obj', 'Get-Object'),
array('test_get_multi_obj', 'MulGet-Object'),
);
$tests3 = array(
array('test_set_multi', 'Multi-Set'),
array('test_set_multi_obj', 'MulSet-Object'),
array('test_incr_multi', 'Multi-Incr'),
array('test_delete_multi', 'Multi-Del'),
);
$tests4 = array(
array('test_hash_strategy', 'Hash-Strat'),
);
function run_tests($pools, $tests) {
global $values, $ints, $count, $reps;
printf('%16s', '');
foreach ($tests as $test) {
list ($function, $caption) = $test;
printf('%-15s', $caption);
}
print "\n";
foreach ($pools as $pool) {
list ($memcache, $caption) = $pool;
printf('%-16s', $caption);
foreach ($tests as $test) {
list ($function, $caption) = $test;
foreach ($values as $key => $value)
$memcache->set($key, $value);
foreach ($ints as $key => $value)
$memcache->set($key, $value);
$ts = microtime(true);
for ($i=0; $i<$reps; $i++) {
$function($memcache);
}
$ts2 = microtime(true);
printf('%-15s', sprintf('%u (%.2fs)', round(($count*$reps) / ($ts2-$ts)), round($ts2-$ts,2)));
}
print "\n";
}
print "\n";
}
// Create pools to run all tests against
$memcache1 = new Memcache();
$memcache1->connect($hosts[0][0], $hosts[0][1]);
$memcache1->setCompressThreshold($threshold);
$pools[] = array($memcache1, 'TCP (1 server)');
if (class_exists('MemcachePool')) {
$memcache2 = new MemcachePool();
$memcache2->connect($hosts[0][0], $hosts[0][1], $hosts[0][2], false);
$memcache2->setCompressThreshold($threshold);
$pools[] = array($memcache2, 'UDP (1 server)');
}
if (count($hosts) > 1) {
$memcache3 = new Memcache();
foreach ($hosts as $h)
$memcache3->connect($h[0], $h[1]);
$memcache3->setCompressThreshold($threshold);
$pools[] = array($memcache3, sprintf('TCP (%d servers)', count($hosts)));
if (class_exists('MemcachePool')) {
$memcache4 = new MemcachePool();
foreach ($hosts as $h)
$memcache4->connect($h[0], $h[1], $h[2], false);
$memcache4->setCompressThreshold($threshold);
$pools[] = array($memcache4, sprintf('UDP (%d servers)', count($hosts)));
}
}
// Create values to work with
$values = array();
$ints = array();
$objects = array();
$key = 'test_key';
$key2 = 'test_key_int';
$key3 = 'test_key_obj';
for ($i=0; $i<$count; $i++) {
$values[$key.$i] = $key.'_value_'.$i.str_repeat('a', $i*1000);
$ints[$key2.$i] = $i;
$objects[$key3.$i] = new TestClass(new TestClass(new TestClass(str_repeat('a', $i*100))));
}
print "Measures are in keys-per-second (higher is better) with number of seconds\nspent in test within parentheses (lower is better).\n\n";
// Configuration
printf("memcache.hash_strategy: %s\n", ini_get('memcache.hash_strategy'));
printf("memcache.chunk_size: %u\n", ini_get('memcache.chunk_size'));
printf("memcache.protocol: %s\n", ini_get('memcache.protocol'));
printf("memcache.compress_threshold: %s\n", $threshold);
printf("servers: %d\n", count($hosts));
printf("iterations: %d\n", $reps);
printf("keys per iteration: %d\n", $count);
print "\n";
$ts = time();
run_tests($pools, $tests1);
run_tests($pools, $tests2);
if (class_exists('MemcachePool'))
run_tests($pools, $tests3);
//run_tests($pools, $tests4);
$ts2 = time();
printf("total time: %d minutes, %d seconds\n", floor(($ts2 - $ts)/60), ($ts2 - $ts)%60);
// Tests
class TestClass {
function __construct($obj = null) {
$this->v1 = str_repeat('abc', 25);
$this->v2 = 123;
$this->v3 = 123.123;
$this->v4 = array('abc', 123);
$this->obj = $obj;
}
}
function test_set_single($memcache) {
global $values;
foreach ($values as $key => $value)
$memcache->set($key, $value);
}
function test_set_multi($memcache) {
global $values;
$memcache->set($values);
}
function test_set_single_obj($memcache) {
global $objects;
foreach ($objects as $key => $value)
$memcache->set($key, $value);
}
function test_set_multi_obj($memcache) {
global $objects;
$memcache->set($objects);
}
function test_get_single($memcache) {
global $values;
foreach (array_keys($values) as $key)
$memcache->get($key);
}
function test_get_multi_obj($memcache) {
global $objects;
$memcache->get(array_keys($objects));
}
function test_get_single_obj($memcache) {
global $objects;
foreach (array_keys($objects) as $key)
$memcache->get($key);
}
function test_get_multi($memcache) {
global $values;
$memcache->get(array_keys($values));
}
function test_delete_single($memcache) {
global $values;
foreach (array_keys($values) as $key)
$memcache->delete($key);
}
function test_delete_multi($memcache) {
global $values;
$memcache->delete(array_keys($values));
}
function test_incr_single($memcache) {
global $ints;
foreach (array_keys($ints) as $key)
$memcache->increment($key);
}
function test_incr_multi($memcache) {
global $ints;
$memcache->increment(array_keys($ints));
}
function test_hash_strategy($memcache) {
global $hosts;
$mc = new Memcache();
foreach ($hosts as $h)
$mc->addServer($h[0], $h[1], false, 100);
$mc->set('test_key', '1');
}
|