Skip to content

Instantly share code, notes, and snippets.

@pkallos
Created December 4, 2012 22:36
Show Gist options
  • Save pkallos/4209670 to your computer and use it in GitHub Desktop.
Save pkallos/4209670 to your computer and use it in GitHub Desktop.
Memcached Benchmark Tool
<?php
$memcached_servers = array(
array('127.0.0.01', 11211),
);
$mc = new Memcached();
$mc->setOption(Memcached::OPT_BINARY_PROTOCOL, TRUE);
$mc->setOption(Memcached::OPT_NO_BLOCK, TRUE);
$mc->setOption(Memcached::OPT_TCP_NODELAY, TRUE);
$mc->addServers($memcached_servers);
// Size in chars
$sizes = array(10, 100, 1000, 10000);
$count = 1000;
$alphabet_soup = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$soup_size = strlen($alphabet_soup);
$values = array();
p("Setting up values");
foreach ($sizes as $size) {
$v = array();
for ($i = 0; $i < $count; $i++) {
$turd = "";
for ($j = 0; $j < $size; $j++) {
$turd .= $alphabet_soup[rand(0, $soup_size)];
}
$values[$size][$i] = &$turd;
}
}
p("Done setup.");
foreach($sizes as $size) {
timeOp("Test $count MC sets (size: $size chars)", function() use(&$values, &$mc, &$count, &$size) {
for ($i = 0; $i < $count; $i++) {
$mc->set($size.$i, $values[$size][$i]);
}
});
}
foreach($sizes as $size) {
timeOp("Test $count MC sequential gets (size: $size chars)", function() use(&$values, &$mc, &$count, &$size) {
for ($i = 0; $i < $count; $i++) {
$mc->get($size.$i);
}
});
}
foreach($sizes as $size) {
timeOp("Test $count MC sequential delete (size: $size chars)", function() use(&$values, &$mc, &$count, &$size) {
for ($i = 0; $i < $count; $i++) {
$mc->delete($size.$i);
}
});
}
// Helper functions
function p($msg) {
print($msg . "\n");
}
function timeOp($name, Closure $function, $times = 1) {
p("Timing $name $times times...");
$start = microtime(TRUE);
for ($index = 0; $index < $times; $index++) {
$function();
}
$done = microtime(TRUE);
$delta = ($done - $start) * 1000;
p("Done $name in $delta miliseconds");
p("");
p("");
}
@acinader
Copy link

acinader commented Dec 4, 2012

print($msg . PHP_EOL);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment