Created
September 3, 2015 21:43
-
-
Save Linnk/4fb107018511de7c08a9 to your computer and use it in GitHub Desktop.
Simple performance test to compare Redis with PHP function file_get_contents() while constantly reading a simple “set” of data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Checktime class | |
* | |
* This static class can make “speed” time marks secuentially. You start | |
* your time counter with Checktime::start() and then you can save all | |
* the marks you want by calling Checktime::mark('YOLO'). | |
*/ | |
class Checktime | |
{ | |
static public $start; | |
static public function start() | |
{ | |
$components = explode(' ', microtime()); | |
self::$start = (float) $components[0] + (float) $components[1]; | |
} | |
static public function mark($label = null) | |
{ | |
$components = explode(' ', microtime()); | |
$mark = (float) $components[0] + (float) $components[1]; | |
$time = $mark - self::$start; | |
if (!$label) | |
$label = 'Mark'; | |
echo "$label $time seconds.\n"; | |
} | |
} | |
/** | |
* Parameters | |
*/ | |
define('TOTAL_OPERATIONS', 10000); | |
define('FILENAME', '/tmp/redis_vs_file_get_content_data.txt'); | |
echo 'Testing '.TOTAL_OPERATIONS." operations each for...\n"; | |
/**************************************************************************** | |
* TEST 1. Write directly to hard disk | |
****************************************************************************/ | |
file_put_contents(FILENAME, '', LOCK_EX); | |
Checktime::start(); | |
for ($n = 0; $n < TOTAL_OPERATIONS; $n++) | |
{ | |
file_put_contents(FILENAME, md5(rand()), FILE_APPEND | LOCK_EX); | |
} | |
Checktime::mark('Write disk ->'); | |
/**************************************************************************** | |
* TEST 2. Read directly from hard disk | |
****************************************************************************/ | |
Checktime::start(); | |
for ($n = 0; $n < TOTAL_OPERATIONS; $n++) | |
{ | |
$data = file_get_contents(FILENAME); | |
} | |
Checktime::mark('Read disk ->'); | |
unlink(FILENAME); | |
/**************************************************************************** | |
* TEST 3. Write directly to Redis | |
****************************************************************************/ | |
$key = 'redis_vs_simple_file_get_contents_key'; | |
$data = ''; | |
$redis = new Redis(); | |
$redis->connect('127.0.0.1'); | |
Checktime::start(); | |
for ($n = 0; $n < TOTAL_OPERATIONS; $n++) | |
{ | |
$data .= md5(rand()); | |
$redis->set($key, $data, 60); | |
} | |
Checktime::mark('Write redis ->'); | |
/**************************************************************************** | |
* TEST 4. Read directly from Redis | |
****************************************************************************/ | |
Checktime::start(); | |
for ($n = 0; $n < TOTAL_OPERATIONS; $n++) | |
{ | |
$data = $redis->get($key); | |
} | |
Checktime::mark('Read redis ->'); | |
$redis->close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the script is flawed. It shouldn't do
$data .= md5(rand()); $redis->set($key, $data, 60);
Instead it should do
$data = md5(rand()); $redis->append($key, $data, 60);
-_-!!! Misleadinggg...