steve3d's avatar

Interesting test with cache options: redis/memcached/file

I just want to know the performance difference with redis/memcahced/file cache, So I've wrote a very simple console command to test the performance.

All tests are made from L5.1/PHP7 with opcache, and here are the results:

Begin test with: redis (with compression and checksum)
Simple String set/unset:  : 35.73846578598
Simple integer set/unset:  : 35.826465845108
Simple integer set/unset with increment:  : 37.206690788269
Simple array set/unset:  : 37.09481215477
Simple object set/unset:  : 36.683856964111
Total timing is: 182.55029153824


Begin test with: memcached
Simple String set/unset:  : 17.991917133331
Simple integer set/unset:  : 18.552289962769
Simple integer set/unset with increment:  : 25.336076974869
Simple array set/unset:  : 19.417834997177
Simple object set/unset:  : 19.157718896866
Total timing is: 100.45583796501


Begin test with: file
Simple String set/unset:  : 37.066348075867
Simple integer set/unset:  : 35.057714939117
Simple integer set/unset with increment:  : 43.191074132919
Simple array set/unset:  : 33.435617923737
Simple object set/unset:  : 36.062146902084
Total timing is: 184.81290197372


Begin test with: redis (without the compression and checksum)
Simple String set/unset:  : 35.347318887711
Simple integer set/unset:  : 34.907536029816
Simple integer set/unset with increment:  : 36.236700057983
Simple array set/unset:  : 36.333000183105
Simple object set/unset:  : 36.539529800415
Total timing is: 179.36408495903

and I'm stunned with the redis result, almost same performance with file cache.

and here is my test codes

    protected function testString()
    {
        $item = 'Simple String set/unset: ';
        $string = str_random(32);
        $time = microtime(true);
        for($i = 0; $i< 10000; $i++) {
            \Cache::add('string.test', $string, 1);
            $r = \Cache::get('string.test');
            \Cache::forget('string.test');
        }

        $this->timing[$item] = microtime(true) - $time;

        $this->info("{$item} : {$this->timing[$item]}");
    }

    protected function testInteger()
    {
        $item = 'Simple integer set/unset: ';
        $value = rand();
        $time = microtime(true);
        for($i = 0; $i< 10000; $i++) {
            \Cache::add('integer.test', $value, 1);
            $r = \Cache::get('integer.test');
            \Cache::forget('integer.test');
        }

        $this->timing[$item] = microtime(true) - $time;
        $this->info("{$item} : {$this->timing[$item]}");
    }

    protected function testIncrement()
    {
        $item = 'Simple integer set/unset with increment: ';
        $value = rand();
        $time = microtime(true);
        for($i = 0; $i< 100; $i++) {
            \Cache::add('integer.inc', $value, 1);

            for($j = 0; $j< 100; $j++) {
                \Cache::increment('integer.inc', $j);
                $r = \Cache::get('integer.inc');
                \Cache::decrement('integer.inc', $j);
                $r = \Cache::get('integer.inc');
            }

            \Cache::forget('integer.inc');
        }

        $this->timing[$item] = microtime(true) - $time;
        $this->info("{$item} : {$this->timing[$item]}");
    }

    protected function testArray()
    {
        $item = 'Simple array set/unset: ';

        $value = [];
        for($i=0; $i<100; $i++) {
            $key = str_random();
            if(rand() & 2)
                $value[$key] = rand();
            else
                $value[$key] = str_random();
        }

        $time = microtime(true);
        for($i = 0; $i< 10000; $i++) {
            \Cache::add('array.test', $value, 1);
            $r = \Cache::get('array.test');
            \Cache::forget('array.test');
        }

        $this->timing[$item] = microtime(true) - $time;
        $this->info("{$item} : {$this->timing[$item]}");
    }

    protected function testObject()
    {
        $item = 'Simple object set/unset: ';

        $value = new \stdClass();
        for($i=0; $i<100; $i++) {
            $key = str_random(5);
            if(rand() & 2)
                $value->$key = rand();
            else
                $value->$key = str_random();
        }

        $time = microtime(true);
        for($i = 0; $i< 10000; $i++) {
            \Cache::add('object.test', $value, 1);
            $r = \Cache::get('object.test');
            \Cache::forget('object.test');
        }

        $this->timing[$item] = microtime(true) - $time;
        $this->info("{$item} : {$this->timing[$item]}");
    }

Is there anything I can do to improve the redis performance?

0 likes
0 replies

Please or to participate in this conversation.