PHP Speed Test – Compressed Cache vs PlainText – Benchmark

If the data on the server is saved in an SSD drive, my simple tests clearly showed that it is more efficient to use data without compression, because the space gained is detrimental to the execution time, time that turns into resources, which turns into energy = additional costs and pollution!

In the image below you have details about the cache-files used in the test:

php-test-gzip-vs-textflat

Here is the php code used for the benchmark

function file_get_any_contents($file, &$data) 
{
    ob_start();
    @readgzfile($file);    
    $data = ob_get_clean(); 
}    

function file_get_gz_contents($file, &$data) 
{
    $zd = gzopen($file, "r");
    $data = gzread($zd, 1048576000);         
    gzclose($zd);
}    

$files = ['options.cache','post_meta.cache','post_tag_relationships.cache','user_meta.cache'];

/*
foreach ($files as $file) {
    $data = file_get_contents($file);
    $gz = gzopen($file.'.gz','w9');
    gzwrite($gz, $data);
    gzclose($gz);
}
*/

$testmax = 1000;

$time_all=0;
foreach ($files as $file) {    
    $fgz = $file.'.gz';
    $start_time = microtime(true);    
    for($i=1; $i<$testmax; $i++) {
        $data = ''; file_get_any_contents($fgz, $data); $data=unserialize($data);        
    }

    $runtime = number_format(( microtime(true) - $start_time), 4);
    echo $runtime.' ~ '.$fgz.' sec <br />';
    $time_all += $runtime;
}
echo 'Total: '.$time_all.' sec';
echo '<hr />';

$time_all=0;
foreach ($files as $file) {  
    $fgz = $file;
    $start_time = microtime(true);
    for($i=1; $i<$testmax; $i++) {       
        $data = unserialize(@file_get_contents($fgz));        
    }

    $runtime = number_format(( microtime(true) - $start_time), 4);
    echo $runtime.' ~ '.$fgz.' sec <br />';
    $time_all += $runtime;
}
echo 'Total: '.$time_all.' sec';

 

And these are the results for 1000 iterations:

0.1939 ~ options.cache.gz sec
0.5643 ~ post_meta.cache.gz sec
0.0611 ~ post_tag_relationships.cache.gz sec
0.0850 ~ user_meta.cache.gz sec
Total: 0.9043 sec


0.0779 ~ options.cache sec
0.2343 ~ post_meta.cache sec
0.0579 ~ post_tag_relationships.cache sec
0.0640 ~ user_meta.cache sec
Total: 0.4341 sec

 

The test was done during the development of a wordpress cache plugin that is simple, ultra-fast, self-configurable and PnP (Plug and Play). The plugin has the ability to compress “static” files and the option is accessible (and dynamic), but compression is not required to save objects (which I decided from the results of this test) !

Why do I make another plugin when there are so many? Because some are to complex and resource-hungry, sometimes you end up earning nothing.

And because it’s not the first wordpress cache plugin I’ve made. I did the first one 13-14 years ago, when such plugins were rare and inferior to the speed gain I needed at the time.

byrev Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *