phpfastcache icon indicating copy to clipboard operation
phpfastcache copied to clipboard

Performance differences between drivers

Open remcohn opened this issue 2 years ago • 2 comments

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

remcohn avatar Jul 17 '24 09:07 remcohn

Hello curious contributor ! Since it seems to be your first contribution, make sure that you've been:

  • Reading and searching out our WIKI
  • Reading and agreed with our Code Of Conduct
  • Reading and understood our Coding Guideline
  • Reading our README If everything looks unclear to you, tell us what 😄 The Phpfastcache Team

github-actions[bot] avatar Jul 17 '24 09:07 github-actions[bot]

Hello,

it mostly depend on the server physical and and software configuration and how phpfastcache runs in a race condition.

Server SSD and HDD-based will behave differently for file-based drivers. However when running heavily queried applications, memory-based drivers will undoubtedly offers better performances.

Geolim4 avatar Jul 28 '24 15:07 Geolim4