PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.36k stars 452 forks source link

Performance differences between drivers #915

Closed remcohn closed 1 month ago

remcohn commented 1 month ago

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

github-actions[bot] commented 1 month ago

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

Geolim4 commented 1 month ago

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.