Friday 27 February 2015

Install MongoDB with PHP on Fedora

MongoDB is an open-source database used by companies of all sizes, across all industries and for a wide variety of applications. It is an agile database that allows schemas to change quickly as applications evolve, while still providing the functionality developers expect from traditional databases, such as secondary indexes, a full query language and strict consistency.

This tutorial looks into how to install latest MongoDB with PHP on Fedora.

Create a /etc/yum.repos.d/mongodb.repo file:
vi /etc/yum.repos.d/mongodb.repo
If you are running a 64-bit system, use the following configuration:
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
else for 32-bit system, use:
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/
gpgcheck=0
enabled=1
To install the latest stable version of MongoDB, issue the following command:
yum install -y mongodb-org
Install php,mongo driver:
pecl install mongo
create a mongo (php) ini
vi /etc/php.d/mongo.ini
And include the driver:
extension=mongo.so
Enable mongod on startup
systemctl enable mongod
start mongod:
service mongod start
Open mongo client to test.
mongo
create database test:
use test
Lets do an insert:
db.collection1.insert({"name":"desc"})
And lets do an query:
db.collection1.find();
Lets proceed with using mongodb in php. Restart php-fpm:
systemctl restart php-fpm.service
We shell create a php script:
<?php
$connection = new Mongo();
$dbname = $connection->selectDB('test');
$collection = $dbname->collection1;
$arr = array(
        'name' => 'MongoDB',
        'desc' => 'MongoDB is a document database.'
);
$collection->insert($arr);
$result = $collection->find();
foreach ($result as $document) {
        echo $document["name"]."\n";
        echo $document["desc"]."\n";
}
?>
You can run above script in web server or just run:
php test_mongo.php

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.

Monday 16 February 2015

Install phpredis

After installing redis, let further install the php package for apache or nginx.
To download and compile phpredis:
git clone https://github.com/phpredis/phpredis
cd phpredis
phpize
./configure
make && make install
Include the compiled extension to php.ini:
touch /etc/php.d/redis.ini
echo extension=redis.so > /etc/php.d/redis.ini
If you use Apache (httpd), restart by:
systemctl restart httpd.service

If you use nginx, restart it by:
systemctl restart php-fpm.service
systemctl restart nginx.service

Install latest Redis cache

Redis is an open source advanced key-value cache. It is high performance and often compared to memcached.

This post we look into how to install latest Redis cache onto (say) Fedora 21. It is quite simple, and lets start:

Goto http://redis.io/download, and download latest version using wget.
wget http://download.redis.io/releases/redis-2.8.19.tar.gz
untar/unzip the package
tar -xzvf redis-2.8.19.tar.gz
compile the bundle
cd redis-2.8.19
make
It is good to do a "make test" to test the functionality on your system. For this test you need tcl.

If you don't have tcl, install it by:
yum install tcl
Run make test:
make test
If functionality test passed it will show:
\o/ All tests passed without errors!
Lets install the excutables into /usr/local/bin/.
make install
For Fedora systemctl system, lets create a redis service script.
vi /usr/lib/systemd/system/redis.service

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis.conf --daemonize no
ExecStop=/usr/local/bin/redis-shutdown
User=redis
Group=redis

[Install]
WantedBy=multi-user.target
Move sample redis.conf to /etc.
cp /root/download/redis-2.8.19/redis.conf /etc/redis.conf
Create redis-shutdown script.
vi /usr/local/bin/redis-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x

REDIS_CLI=/usr/local/bin/redis-cli

# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=redis
fi

# Get the proper config file based on service name
CONFIG_FILE="/etc/$SERVICE_NAME.conf"

# Use awk to retrieve port from config file
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE`

# Just in case, use default port
if [ "$SERVICE_NAME" = redis ]; then
    PORT=${PORT:-6379}
else
    PORT=${PORT:-26739}
fi

# shutdown the service properly
$REDIS_CLI -p $PORT SHUTDOWN NOSAVE
Enable and start redis service.
systemctl enable redis.service
systemctl start redis.service
Lets try the redis-cli. Set a key and get a key:
[root@fc21 temp]# redis-cli
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"

search iomeweekly