Showing posts with label memcache. Show all posts
Showing posts with label memcache. Show all posts

Saturday 22 April 2017

Benchmark memcached vs memcache vs redis

My setup:
Fedora 25,Nginx 1.10.2,redis 3.1.2,memcache 3.0.9,memcached 3.0.3,php-fpm 7.0.17.

My Benchmark script(php)
<?php
################# memcached ###################################################
    
    $k=0;
    $t_time=0;
    echo "
Benchmark memcached :
"; $memcached = new Memcached; $memcached->addServer("127.0.0.1", 11211)or die ("Cannot connect"); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $memcached->set($a1[$i], $a2[$a1[$i]],20) or die ("Cannot save data"); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value = $memcached->get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; ################# memcache ################################################### $k=0; $t_time=0; echo "
Benchmark memcache :
"; $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die ("Cannot connect"); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $memcache->set($a1[$i], $a2[$a1[$i]], false, 20) or die ("Cannot save data"); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value = $memcache->get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; ################# redis ################################################### $k=0; $t_time=0; echo "
Benchmark redis :
"; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $redis -> set($a1[$i],$a2[$a1[$i]]); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value=$redis -> get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; function timer_start() { $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; return $time; } function timer_end($start) { $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; return $time-$start; } function get_random_string($valid_chars, $length) { // start with an empty random string $random_string = ""; // count the number of chars in the valid chars string so we know how many choices we have $num_valid_chars = strlen($valid_chars); // repeat the steps until we've created a string of the right length for ($i = 0; $i < $length; $i++) { // pick a random number from 1 up to the number of valid chars $random_pick = mt_rand(1, $num_valid_chars); // take the random character out of the string of valid chars // subtract 1 from $random_pick because strings are indexed starting at 0, and we started picking at 1 $random_char = $valid_chars[$random_pick-1]; // add the randomly-chosen char onto the end of our string so far $random_string .= $random_char; } // return our finished random string return $random_string; } ?>
Benchmark memcached : 
Total Set time: 0.69209885597229
Loop 0 (Get) time: 0.68401622772217
Loop 1 (Get) time: 0.67029213905334
Loop 2 (Get) time: 0.77216291427612
Loop 3 (Get) time: 0.68823003768921
Loop 4 (Get) time: 0.68713402748108
Loop 5 (Get) time: 0.66251420974731
Loop 6 (Get) time: 0.66882991790771
Loop 7 (Get) time: 0.63542795181274
Loop 8 (Get) time: 0.66616487503052
Loop 9 (Get) time: 0.66425609588623
Total 10 loop Get time 6.7990283966064 

Benchmark memcache : 
Total Set time: 0.72946310043335
Loop 0 (Get) time: 0.55730509757996
Loop 1 (Get) time: 0.46489191055298
Loop 2 (Get) time: 0.68988108634949
Loop 3 (Get) time: 0.73450708389282
Loop 4 (Get) time: 0.68325591087341
Loop 5 (Get) time: 0.67392992973328
Loop 6 (Get) time: 0.7195930480957
Loop 7 (Get) time: 0.74770784378052
Loop 8 (Get) time: 0.70041298866272
Loop 9 (Get) time: 0.70169019699097
Total 10 loop Get time 6.6731750965118 

Benchmark redis : 
Total Set time: 0.69859886169434
Loop 0 (Get) time: 0.69667816162109
Loop 1 (Get) time: 0.65093517303467
Loop 2 (Get) time: 0.68474006652832
Loop 3 (Get) time: 0.7402458190918
Loop 4 (Get) time: 0.6503758430481
Loop 5 (Get) time: 0.66205310821533
Loop 6 (Get) time: 0.70370101928711
Loop 7 (Get) time: 0.646409034729
Loop 8 (Get) time: 0.66896295547485
Loop 9 (Get) time: 0.67996406555176
Total 10 loop Get time 6.784065246582 
The results shows for simple Set and Get,memcached is quite comparable to redis.
And the result is not repeatable. Different results occurrs at different run.

For other functions,more extensive tests/benchmarks have to perform on both.

Tuesday 17 February 2015

Benchmark redis 2.8.19 with memcache 1.4.22

Having read a few articles on comparing redis with memcache. I decided to benchmark myself.

My setup:
Fedora 21,Nginx 1.6.2,redis 2.8.19,memcache 1.4.22,php-fpm 5.6.4.

My Benchmark script(php)
<?php
    ################# redis ###################################################
    $k=0;
    $t_time=0;
    echo "Benchmark redis : 
"; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $redis -> set($a1[$i],$a2[$a1[$i]]); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value=$redis -> get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; ################# memcache ################################################### $k=0; $t_time=0; echo "
Benchmark memcache :
"; $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die ("Cannot connect"); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $memcache->set($a1[$i], $a2[$a1[$i]], false, 20) or die ("Cannot save data"); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value = $memcache->get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; function timer_start() { $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; return $time; } function timer_end($start) { $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; return $time-$start; } function get_random_string($valid_chars, $length) { $random_string = ""; $num_valid_chars = strlen($valid_chars); for ($i = 0; $i < $length; $i++) { $random_pick = mt_rand(1, $num_valid_chars); $random_char = $valid_chars[$random_pick-1]; $random_string .= $random_char; } return $random_string; } ?>
Benchmark redis : 
Total Set time: 0.12764096260071
Loop 0 (Get) time: 0.175616979599
Loop 1 (Get) time: 0.18264698982239
Loop 2 (Get) time: 0.13450312614441
Loop 3 (Get) time: 0.1352870464325
Loop 4 (Get) time: 0.13448214530945
Loop 5 (Get) time: 0.13312005996704
Loop 6 (Get) time: 0.15526604652405
Loop 7 (Get) time: 0.40630602836609
Loop 8 (Get) time: 0.13408994674683
Loop 9 (Get) time: 0.13564801216125
Total 10 loop Get time 1.726966381073 

Benchmark memcache : 
Total Set time: 0.14455795288086
Loop 0 (Get) time: 0.12623596191406
Loop 1 (Get) time: 0.13038897514343
Loop 2 (Get) time: 0.1564610004425
Loop 3 (Get) time: 0.11394095420837
Loop 4 (Get) time: 0.11402487754822
Loop 5 (Get) time: 0.11300802230835
Loop 6 (Get) time: 0.11119389533997
Loop 7 (Get) time: 0.1115710735321
Loop 8 (Get) time: 0.11155104637146
Loop 9 (Get) time: 0.19941091537476
Total 10 loop Get time 1.28778672218322
The results shows for simple Set and Get, redis is faster. For SET, redis is faster by 12% For GET, memcache is faster by 25%

For other functions,more extensive tests/benchmarks have to perform on both.

Tuesday 9 April 2013

How to display multiple memcache servers statistics

To display multiple memcache servers statistics, you can run below php script.


<?php
$servers = array(
    array('127.0.0.1', 11211, 33),
    array('192.168.0.55', 11211, 67)
);

$memcache = new Memcache;
$memcache->addServer('192.168.0.23',11211);
$memcache->addServer('192.168.0.55',11211);
$return = $memcache->getExtendedStats();
$unique_key=array();
foreach ($return as $key => $value) {
    foreach ($value as $key2 => $value2) {       
        array_push($unique_key,$key2);
    }
}

$unique_key=array_unique($unique_key);   
echo "<table border='2' align=\"center\" width=\"100%\" >";
echo "<tr><th/>";
foreach ($return as $key => $value) {   
    echo "<th>$key</th>";   
}

echo "</tr>";
foreach ($unique_key as $key0) {   
    echo "<tr align=\"center\"><td>$key0</td>";   
    foreach ($return as $key => $value) {
        echo "<td>".$return[$key][$key0]."</td>";       
    }
    echo "</tr>";
}
echo "</table>";

?>

Monday 8 April 2013

Upgrade PECL Memcache extension

The following instructions will download latest PECL Memcache extension. Goto PECL Memcache website. Download the tgz file using wget, and install as follows.

$ mkdir /tmp/temp
$ cd /tmp/temp
$ wget http://pecl.php.net/get/memcache-3.0.8.tgz
$ tar -xzvf memcache-3.0.8.tgz
$ cd memcache-3.0.8/
$ phpize
$ ./configure
$ make
$ make test
$ make install
$ service httpd restart

How to display memcache statistics at bash shell

To display memcache statistics at command prompt, you can enter below command. This command will display memcache statistics like "top", updating the statistic every 2s.

watch "echo stats|nc 127.0.0.1 11211"

How to display memcache statistics with php

To display memcache statistics, you can create a php script as shown below. This scripts uses "getStats" command to display the statistics. You can also format it into a html table.

<?php
$memcache_host="127.0.0.1";
$memcache_port="11211";
$memcache = new Memcache;
$memcache->connect($memcache_host,$memcache_port) or die ("Cannot connect");
$return=($memcache->getStats());
foreach ($return as $key => $value) {
    echo $key." : ".$value."<br>";   
}
?>

How to use memcache with php


Previously I posted an article on how to install Memcache. This time I will show how to use Memcache with PHP. This program will start off with a SQL query statement, hashing the statement and check if the data is stored in memcache.If found, the data will be retrieve from memcache. If it is not found in memcache, then it will query from MySQL database.


<?php
require('connect_db.php'); 
$memcache_host="127.0.0.1";
$memcache_port="11211";
$memcache = new Memcache;
$memcache->connect($memcache_host,$memcache_port) or die ("Cannot connect");

$version = $memcache->getVersion();
echo "Version: ".$version;

$query="select * from exampleDB limit 1;";
$key = md5($query); 
$get_result = $memcache->get($key);

if ($get_result) {   
    print_r($get_result);
    echo "Data retrieved from memcache";
}
else {    
    $result = mysql_query($query);
    if (!$result) {
        die('Invalid query: ' . mysql_error()) 
      }
    $row = mysql_fetch_array($result);
    print_r($row);  
    $memcache->set($key, $row, MEMCACHE_COMPRESSED, 10); 
    echo "Data retrieved from database";
}  
?>

Wednesday 3 April 2013

Install memcache on CENTOS6


This guide will explain how to install memcache.

To install the server:

yum search memcached
yum install memcached php-pear
chkconfig memcached on
service memcached start

To install php,pecl,memcache extension:

pecl search memcache

The latest stable version is memcache 2.2.7.

You need to install dependencies before installing memcache:
yum install php-devel gcc zlib zlib-devel
To install php extension:
pecl install memcache

Insert the extension in php.ini:
vi /etc/php.ini

Insert below line to php.ini:
extension=memcache.so


To create php test script:

vi /var/www/html/memcache.php

<?php

$memcache = new Memcache;
$memcache->;connect('127.0.0.1', 11211) or die ("Cannot connect");

$version = $memcache->getVersion();
echo "Version: ".$version."
\n"; $tmp_object = new stdClass; $tmp_object->test1 = 'value1'; $tmp_object->test2 = 472; $memcache->set('key', $tmp_object, false, 20) or die ("Cannot save data"); echo "Stored data in the memcache for 20 seconds
\n"; $get_result = $memcache->get('key'); echo "Data from the cache:
\n"; var_dump($get_result); ?>

search iomeweekly