Skip to content

Instantly share code, notes, and snippets.

@Linnk
Created September 3, 2015 21:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Linnk/4fb107018511de7c08a9 to your computer and use it in GitHub Desktop.
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.
<?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();
@pootpootman
Copy link

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment