Showing posts with label speed. Show all posts
Showing posts with label speed. Show all posts

Tuesday 16 April 2013

How to install Varnish cache on Fedora 18

Varnish is a web accelerator in front of your web server, and it significantly speeds up web content delivery. This guide will show you how to install Varnish cache using YUM on Fedora 18. First, install Varnish and its dependencies:

[root@localhost ~]# yum install varnish


 After above installation, there are 2 config files to setup. "/etc/varnish/default.vcl" and "/etc/varnish/varnish.params".

If you are installing Varnish with Apache Web Server on the same hardware(localhost). Insert below config to "/etc/varnish/default.vcl".

[root@localhost ~]# vi /etc/varnish/default.vcl

backend default {
 .host = "127.0.0.1";
 .port = "80";
}

 By default, Varnish is installed to listen to port 6081. The admin port is installed on port 6082.

[root@localhost ~]# vi /etc/varnish/varnish.params


 Here is my varnish.params:

# Varnish environment configuration description. This was derived from
# the old style sysconfig/defaults settings


# Set this to 1 to make systemd reload try to switch vcl without restart.
RELOAD_VCL=1


# Main configuration file. You probably want to change it.
VARNISH_VCL_CONF=/etc/varnish/default.vcl


# Default address and port to bind to. Blank address means all IPv4
# and IPv6 interfaces, otherwise specify a host name, an IPv4 dotted
# quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=192.168.1.5
VARNISH_LISTEN_PORT=6081


# Admin interface listen address and port
#VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082

# Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret

# The minimum and maximum number of worker threads to start
VARNISH_MIN_THREADS=5
VARNISH_MAX_THREADS=1000

# Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120

# Backend storage specification, see Storage Types in the varnishd(5)
# man page for details.
VARNISH_STORAGE="file,/var/lib/varnish/varnish_storage.bin,1G"

# Default TTL used when the backend does not specify one
VARNISH_TTL=120

# User and group for the varnishd worker processes
VARNISH_USER=varnish
VARNISH_GROUP=varnish
After you have edited the config files, set the machine to start Varnish on reboot. And start the cache service.

[root@localhost ~]# systemctl enable varnish.service
[root@localhost ~]# systemctl start varnish.service


To access the cache, you can type the IP address of the server on the web browser. Assume the Varnish and Apache Web server is at 192.168.0.5.

http://192.168.0.5:6081

 To bypass varnish cache, you can still reach web server (Apache) at:

http://192.168.0.5

Tuesday 2 April 2013

Benchmark PHP mysql vs PHP mysqli

In PHP coding, many of my php scripts are still using php mysql extension. Before taking the time to migrate to php mysqli extension, I have decided to run a simple benchmark between the two. I have written 2 scripts and time it. The first script is "test_mysql.php", the second script is "test_mysqli.php". The result is, the first script took an average of 1.01 sec, the second script took 0.86 sec. "test_mysqli.php" is 17.87% faster than "test_mysql.php".

The scripts were tested on Linux 3.8.4, PHP 5.4.12, MySQL 5.5.30.

First script, test_mysql.php

<?php
//-----Start Timer-------------------------------------------------//       
    $time = microtime();
    $time = explode(" ", $time);
    $time = $time[1] + $time[0];
    $top = $time;
//-----Start Timer-------------------------------------------------//   
for($j=0;$j<1000;$j++)
{  
    $link = mysql_connect($host,$user,$password) or die('Error in Server information');   
    mysql_select_db($db,$link) or die('Can not Select Databasse');   
    $query="select * from com_list where field1='value1';";
    $res = mysql_query($query);  //1 is ok
    if (!$res) {
        die('Invalid query: ' . mysql_error()); }

    while($row = mysql_fetch_array($res)){
    $i++;
    }
    mysql_free_result($res);
    mysql_close($link);
}
    echo "Total lines :".$i;
//-----End Timer-------------------------------------------------//   
    $time = microtime();
    $time = explode(" ", $time);
    $time = $time[1] + $time[0];
    $bottom = $time;
//-----End Timer-------------------------------------------------//
    $loadtime = ($bottom-$top);
    echo ("<br> <br>This page generated in $loadtime seconds");  
?>
Second script, test_mysqli.php
<?php
//-----Start Timer-------------------------------------------------//       
    $time = microtime();
    $time = explode(" ", $time);
    $time = $time[1] + $time[0];
    $top = $time;
//-----Start Timer-------------------------------------------------//       
for($j=0;$j<1000;$j++)
{  
    $link = mysqli_connect($host,$user,$password,$db) or die('Error in Server information');
    $query="select * from com_list where field1='value1';";
    $res = mysqli_query($link,$query);  //1 is ok
    if (!$res) {
        die('Invalid query: ' . mysql_error()); }
    while($row = mysqli_fetch_array($res)){        
    $i++;
    }   
    mysqli_free_result($res);
    mysqli_close($link);
}
    echo "Total lines :".$i;
//-----End Timer-------------------------------------------------//       
    $time = microtime();
    $time = explode(" ", $time);
    $time = $time[1] + $time[0];
    $bottom = $time;
//-----End Timer-------------------------------------------------//           
    $loadtime = ($bottom-$top);
    echo ("<br> <br>This page generated in $loadtime seconds");  
?>



search iomeweekly