Showing posts with label fedora. Show all posts
Showing posts with label fedora. 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.

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.

Thursday 29 January 2015

Install Nginx (and PHP) server on Fedora

Nginx [engine x] is a free high-perfomance HTTP and reverse proxy server, as well as a mail proxy server, written by Igor Sysoev.

Lets start the installation process:
Stop and disable existing apache.

# systemctl stop httpd.service
# systemctl disable httpd.service 

Install php-fpm

# yum install php-fpm -y

Enable and start php-fpm service:

# systemctl enable php-fpm.service
# systemctl start php-fpm.service

To install Nginx

# yum install nginx -y

Enable and start Nginx service

# systemctl enable nginx.service
# systemctl start nginx.service

Test Nginx

Open up your web browser and navigate to http://localhost/ (example). You will see the default nginx welcome screen.

Configure Nginx

Open the file /etc/nginx/nginx.conf in vi

# vi /etc/nginx/nginx.conf

Set the worker_processes (No. Of CPU’s in your system you want to utilise). To see the no. Of CPU’s, you can use the command “lscpu”. For default you can set to 1.
worker_processes 1;

Example of working nginx.conf:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    index   index.html index.htm;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    #Change below value to suit your need:
    fastcgi_read_timeout 6000s;

    server {
        listen       80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }

        location ~ [^/]\.php(/|$) {
           fastcgi_split_path_info ^(.+?\.php)(/.*)$;
           if (!-f $document_root$fastcgi_script_name) {
              return 404;
           }

           fastcgi_pass 127.0.0.1:9000;
           fastcgi_index index.php;
           include fastcgi_params;           
        }
    }
}

Save and close the file. Restart Nginx service:

# systemctl restart nginx.service

Test PHP

Create a sample “phpinfo.php” file in the nginx document root folder:

# vi /usr/share/nginx/html/phpinfo.php

Append the lines as shown below:



Save and close the file. Restart Nginx service:

# systemctl restart nginx.service

Navigate to http://localhost/testphp.php (example). It will display all the details about PHP,build,setups.

Wednesday 16 April 2014

Install opcache for php on Fedora

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request #1. OpCode Caches are a performance enhancing extension for PHP. They do this by injecting themselves into the execution life-cycle of PHP and caching the results of the compilation phase for later reuse. It is not uncommon to see a 3x performance increase just by enabling an OpCode cache #2.

To install on Fedora:

yum install php-opcache 

To turn on the opcache module, restart httpd service:

/bin/systemctl restart  httpd.service 

You will be able to see opcache running information in phpinfo.php.

Friday 4 April 2014

"Network error: Connection refused" on Fedora

I have just installed Fedora. When I tried to putty(ssh) in, a PuTTY fatal Error was displayed, "Network error: Connection refused".

When I check the ssh status, it said not found.


[root@F20-64 ~]# service sshd status
Redirecting to /bin/systemctl status  sshd.service
sshd.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)


The openssh server was not installed on fresh Fedora.
So I install the server.

[root@F20-64 ~]# yum install openssh-server

Checking the status again:

[root@F20-64 ~]# service sshd status
Redirecting to /bin/systemctl status  sshd.service
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled)
   Active: inactive (dead)

Enable ssh server upon restart.

[root@F20-64 ~]# chkconfig sshd on
Note: Forwarding request to 'systemctl enable sshd.service'.
ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service'

Start the ssh server service.

[root@F20-64 ~]# service sshd start
Redirecting to /bin/systemctl start  sshd.service

Thursday 11 April 2013

Install APC (php) on Fedora 18

The Alternative PHP Cache (APC) is a bytecode optimization opcode cache for PHP. This free cache leads to faster runtime execution as it stores the final, compiled result in shared memory. To install APC, you can setup using YUM.
[root@localhost ~]# yum install php-pecl-apc
Using YUM method, it will create /etc/php.d/apc.ini , enabling apc extension module "extension = apc.so". If apc.ini is not found, you can insert "extension = apc.so" into php.ini. After apache restart(service httpd restart), you will see apc enabled in phpinfo.php. APC,cache,phpinfo

Wednesday 10 April 2013

Create barcodes on CENTOS/FEDORA Linux

To create barcode for Linux, you need to install "barcode" rpm. Suppose you want to create a 13 numbers barcode, and save it to postscript file, and convert the postscript file to pdf, you can do this.

[root@centos6-1 temp]# yum install barcode
[root@centos6-1 temp]# barcode -o test.ps -e EAN -b 9557447805435
[root@centos6-1 temp]# ps2pdf test.ps test.pdf

search iomeweekly