sanis
7/5/2017 - 6:10 PM

Just a quick benchmark if it's worth to cache oxid category in redis

Just a quick benchmark if it's worth to cache oxid category in redis

<?php
/**
 * composer require phpbench/phpbench
 * php vendor/bin/phpbench run CachingBench.php --report=default
 */

/*
PhpBench 0.13.0. Running benchmarks.

\CachingBench

    benchGetFileCaching           I49 P0 	[μ Mo]/r: 0.034 0.031 (ms) 	[μSD μRSD]/r: 0.005ms 15.97%
    benchGetIgbinaryFileCaching   I49 P0 	[μ Mo]/r: 0.025 0.024 (ms) 	[μSD μRSD]/r: 0.004ms 17.77%
    benchGetRamFileCaching        I49 P0 	[μ Mo]/r: 0.029 0.028 (ms) 	[μSD μRSD]/r: 0.001ms 4.15%
    benchGetIgbinaryRamFileCachingI49 P0 	[μ Mo]/r: 0.023 0.022 (ms) 	[μSD μRSD]/r: 0.001ms 3.14%
    benchGetRedisCaching          I49 P0 	[μ Mo]/r: 0.127 0.144 (ms) 	[μSD μRSD]/r: 0.032ms 24.97%
    benchGetIgbinaryRedisCaching  I49 P0 	[μ Mo]/r: 0.138 0.146 (ms) 	[μSD μRSD]/r: 0.039ms 28.24%
    benchGetPlainRedisCaching     I49 P0 	[μ Mo]/r: 0.099 0.110 (ms) 	[μSD μRSD]/r: 0.021ms 21.21%

7 subjects, 350 iterations, 70,000 revs, 0 rejects
(best [mean mode] worst) = 21.829 [67.636 72.267] 25.374 (μs)
⅀T: 23,672.437μs μSD/r 14.747μs μRSD/r: 16.492%
*/

/**
 * @\PhpBench\Benchmark\Metadata\Annotations\BeforeClassMethods({"initOxidThings"})
 */
class CachingBench
{
    public static $redis;

    public static function getRedis()
    {
        if (self::$redis) {
            return self::$redis;
        } else {
            $redis = new \Redis();
            $redis->connect('redis', 6379);
            self::$redis = $redis;
        }
        return self::$redis;
    }

    /**
     * Inits the files and puts to databases
     */
    public static function initOxidThings()
    {
        // get from database
        include_once __DIR__ . '/http/bootstrap.php';
        $config = oxRegistry::getConfig();
        $category = oxNew('oxCategory');
        $category->load('cxmCID2557884');

        file_put_contents('test.txt', serialize($category));
        file_put_contents('test2.txt', igbinary_serialize($category));

        file_put_contents('/dev/shm/test.txt', serialize($category));
        file_put_contents('/dev/shm/test2.txt', igbinary_serialize($category));

        $redis = self::getRedis();
        $redis->flushAll();
        $redis->set('test', serialize($category));
        $redis->set('test2', igbinary_serialize($category));
        $redis->set('test3', $category);
    }

    /**
     * @\PhpBench\Benchmark\Metadata\Annotations\Revs(10000)
     * @\PhpBench\Benchmark\Metadata\Annotations\Iterations(50)
     * @\PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit("milliseconds", precision=3)
     */
    public function benchGetFileCaching()
    {
        $fileContents = file_get_contents('test.txt');
        $category = unserialize($fileContents);
    }

    /**
     * @\PhpBench\Benchmark\Metadata\Annotations\Revs(10000)
     * @\PhpBench\Benchmark\Metadata\Annotations\Iterations(50)
     * @\PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit("milliseconds", precision=3)
     */
    public function benchGetIgbinaryFileCaching()
    {
        $fileContents = file_get_contents('test2.txt');
        $category = igbinary_unserialize($fileContents);
    }

    /**
     * @\PhpBench\Benchmark\Metadata\Annotations\Revs(10000)
     * @\PhpBench\Benchmark\Metadata\Annotations\Iterations(50)
     * @\PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit("milliseconds", precision=3)
     */
    public function benchGetRamFileCaching()
    {
        $fileContents = file_get_contents('/dev/shm/test.txt');
        $category = unserialize($fileContents);
    }

    /**
     * @\PhpBench\Benchmark\Metadata\Annotations\Revs(10000)
     * @\PhpBench\Benchmark\Metadata\Annotations\Iterations(50)
     * @\PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit("milliseconds", precision=3)
     */
    public function benchGetIgbinaryRamFileCaching()
    {
        $fileContents = file_get_contents('/dev/shm/test2.txt');
        $category = igbinary_unserialize($fileContents);
    }

    /**
     * @\PhpBench\Benchmark\Metadata\Annotations\Revs(10000)
     * @\PhpBench\Benchmark\Metadata\Annotations\Iterations(50)
     * @\PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit("milliseconds", precision=3)
     */

    public function benchGetRedisCaching()
    {
        $redis = self::getRedis();
        $contents = $redis->get('test');
        $category = unserialize($contents);
    }

    /**
     * @\PhpBench\Benchmark\Metadata\Annotations\Revs(10000)
     * @\PhpBench\Benchmark\Metadata\Annotations\Iterations(50)
     * @\PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit("milliseconds", precision=3)
     */
    public function benchGetIgbinaryRedisCaching()
    {
        $redis = self::getRedis();
        $contents = $redis->get('test2');
        $category = igbinary_unserialize($contents);
    }

    /**
     * @\PhpBench\Benchmark\Metadata\Annotations\Revs(10000)
     * @\PhpBench\Benchmark\Metadata\Annotations\Iterations(50)
     * @\PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit("milliseconds", precision=3)
     */
    public function benchGetPlainRedisCaching()
    {
        $redis = self::getRedis();
        $category = $redis->get('test3');
    }
}