Skip to content

Performance differences between drivers #915

@remcohn

Description

@remcohn

What's your question ?

I am trying to understand the performance differences between various drivers. In my case, im comparing files, sqlite, redis and memory. I have attached my php code below.
The output on my test server is the following:

Driver:      files : read:    966858 write:      7740 miss:     48146 writeread:      7658 attach:    145888 
Driver:     sqlite : read:    958942 write:       212 miss:       430 writeread:       208 attach:    143134 
Driver:      redis : read:    970652 write:      9914 miss:     21310 writeread:      9698 attach:    150714 
Driver:     memory : read:    964752 write:     58000 miss:    109946 writeread:     54004 attach:    142178

I am surprised by the fact that the read performance is pretty much identical between all drivers. And there is very little difference between redis and files, while memory is faster.
Also the reconnect time is also virtually identical between drivers.

The code is running each test for 1 second, and the numbers are the loop counter.

Am i missing something? Am i testing the wrong things?

I am grateful for any explanation :)

Thanks,
Remco

<php
require_once('vendor/autoload.php');

use Phpfastcache\Helper\Psr16Adapter;

$drivers = array ('files', 'sqlite', 'redis', 'memory');
$t = 1;

foreach($drivers as $driver) {
        $cache = new Psr16Adapter($driver);
        $cache->clear();

        printf("Driver: %10s : ", $driver);
        readtest();
        writetest();
        misstest();
        writereadtest();
        attachtest($driver);
        echo "\n";
}

function readtest() {
        global $cache, $t;
        $key = bin2hex(random_bytes(4));
        $cache->clear();
        $cache->set($key, random_bytes(128), 30);
        $t1 = microtime(true);
        $i = 0;
        while (microtime(true) < $t1 + $t) {
                $cache->get($key);
                $i++;
        }
        printf("read: %9d ", $i / $t);
}

function writetest() {
        global $cache, $t;
        $cache->clear();
        $t1 = microtime(true);
        $i = 0;
        while (microtime(true) < $t1 + $t) {
                $cache->set(bin2hex(random_bytes(4)), random_bytes(128), 30);
                $i++;
        }
        printf("write: %9d ", $i / $t);
}

function misstest() {
        global $cache, $t;
        $cache->clear();
        $t1 = microtime(true);
        $i = 0;
        while (microtime(true) < $t1 + $t) {
                $cache->get(bin2hex(random_bytes(4)));
                $i++;
        }
        printf("miss: %9d ", $i / $t);
}

function writereadtest() {
        global $cache, $t;
        $cache->clear();
        $t1 = microtime(true);
        $i = 0;
        while (microtime(true) < $t1 + $t) {
                $key = bin2hex(random_bytes(4));
                $cache->set($key, random_bytes(128), 30);
                $cache->get($key);
                $i++;
        }
        printf("writeread: %9d ", $i / $t);
}

function attachtest($driver) {
        global $cache, $t;

        $key = bin2hex(random_bytes(4));
        $data = random_bytes(128);
        $cache->clear();
        $cache->set($key, $data, 30);

        $t1 = microtime(true);
        $i = 0;
        while (microtime(true) < $t1 + $t) {
                unset($cache);
                $cache = new Psr16Adapter($driver);
                $foo = $cache->get($key);
                if ($foo != $data) die("error\n");
                $i++;
        }
        printf("attach: %9d ", $i / $t);
}

References (optional)

No response

Do you have anything more you want to share? (optional)

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions