Last active
January 23, 2019 21:27
-
-
Save totten/6d6524be115c193e0704ff3cf250336d to your computer and use it in GitHub Desktop.
Comparing performance of different front-caches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Total number of times to take measurements. | |
$trialCount = 3; | |
// Number of items to write when filling the cache | |
$writeItems = 1000; | |
// Number of items to read from the cache. | |
$readItems = 200; | |
// Number of times to re-read the same records from the cache. | |
//$readCount = 150; | |
//$readCount = 10; | |
//$readCount = 1; | |
foreach ([1, 10, 150] as $readCount) { | |
printf("[[ trialCount=%d writeItems=%d readItems=%d numTimesToReadEach=%d ]]\n\n", $trialCount, $writeItems, $readItems, $readCount); | |
$summary = []; | |
for ($trial = 0; $trial < 3; $trial++) { | |
// echo "\n"; | |
$cacheSpecs = [ | |
"t{$trial}_direct" => function() use ($trial) { | |
return CRM_Utils_Cache::create([ | |
'name' => "t{$trial}_direct", | |
'type' => array('*memory*'), | |
'withArray' => FALSE, | |
]); | |
}, | |
"t{$trial}_tiered" => function() use ($trial) { | |
return new CRM_Utils_Cache_Tiered([ | |
new CRM_Utils_Cache_Arraycache([]), | |
CRM_Utils_Cache::create([ | |
'name' => "t{$trial}_innersql", | |
'type' => array('*memory*'), | |
'withArray' => 'fast', | |
]), | |
]); | |
}, | |
"t{$trial}_arrdec" => function() use ($trial) { | |
return CRM_Utils_Cache::create([ | |
'name' => "t{$trial}_arrdec", | |
'type' => array('*memory*'), | |
'withArray' => 'decor', | |
]); | |
}, | |
"t{$trial}_fastdec" => function() use ($trial) { | |
return CRM_Utils_Cache::create([ | |
'name' => "t{$trial}_fastdec", | |
'type' => array('*memory*'), | |
'withArray' => 'fast', | |
]); | |
}, | |
]; | |
foreach ($cacheSpecs as $cacheName => $cacheFactory) { | |
$cacheFactory()->clear(); | |
$writeStart = microtime(1); | |
$cache = $cacheFactory(); | |
for ($i = 0; $i < $writeItems; $i++) { | |
$val = "value_$i "; | |
for ($ch = 0; $ch < $i; $ch++) { $val .= '...'; } | |
$cache->set("item_$i", "value_$i"); | |
} | |
$writeStop = microtime(1); | |
// Pick a random subsection and read it several times. | |
$readOffset = rand(0, $writeItems - 1);; | |
$readStart = microtime(1); | |
$cache = $cacheFactory(); | |
for ($readNum = 0; $readNum < $readCount; $readNum++) { | |
for ($i = 0; $i < $readItems; $i++) { | |
$itemNum = ($i + $readOffset) % $writeItems; | |
$val = $cache->get("item_$itemNum"); | |
} | |
} | |
$readStop = microtime(1); | |
// printf("%-15s write=%.4fs read=%.4fs readPerItem=%.8fs\n", | |
// $cacheName, | |
// $writeStop - $writeStart, | |
// $readStop - $readStart, | |
// ($readStop - $readStart) / $readCount | |
// ); | |
$summary[substr($cacheName, 3)][] = [ | |
'write' => $writeStop - $writeStart, | |
'read' => $readStop - $readStart, | |
'readPerItem' => ($readStop - $readStart) / $readCount | |
]; | |
} | |
} | |
echo "Mean values across all trials:\n\n"; | |
foreach ($summary as $name => $values) { | |
printf("%-15s write=%.4fs read=%.4fs readPerItem=%.8fs\n", | |
$name, | |
mean(array_column($values, 'write')), | |
mean(array_column($values, 'read')), | |
mean(array_column($values, 'readPerItem')) | |
); | |
} | |
echo "\n"; | |
} | |
function mean($v) { | |
return array_sum($v) / count($v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment