Profiling PHP Connections - Pool vs No Pool

Creating connection in efficient way is important, specifically if you are writing a consumber website that targets thousands of concurrent visitors. In this post I would share some performance statistics of creating connections using connection pool and without using connection pool.

To see the performance difference, I created 100 connections without using connection pool and another 100 connections using connection pool. Here I show you the average time of creating one connection. 

I repeated same process 10 times, here are the states:

Time to get one connection at localhost, in milliseconds.
Without Connection Pool   |   With Connection Pool 
                                 9.37    |    0.99       ms 
                               11.06    |    1.02       ms 
                               10.48    |    0.77       ms 
                               10.82    |    0.81       ms 
                               10.13    |    0.81       ms 
                               11.49    |    0.83       ms 
                               10.82    |    0.83       ms 
                               10.44    |    0.84       ms 
                               10.67    |    1.07       ms 
                               10.71    |    0.84       ms 

There is approximately 10 times performance improvement when connection is created using from connection pool. Here is the PHP code:

define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "mypassword");
define("DEFAULT_DB_NAME", "test_db");

echo "Time to get one connection at localhost, in milliseconds \n ";
echo "Without Connection Pool | With Connection Pool \n ";

for ($index = 0; $index < 10; $index++) {
$t1 = round(microtime(true) * 1000);
for ($i = 0; $i < 100; $i++) {
$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
}
echo " " . ((round(microtime(true) * 1000)) - $t1) / 100 ;

/******************** USING CONNECTION POOL ****************************/

$t1 = round(microtime(true) * 1000);
for ($i = 0; $i < 100; $i++) {
$con = new mysqli("p:" . DB_HOST, DB_USER, DB_PASSWORD);
}
echo " | " . ((round(microtime(true) * 1000)) - $t1) / 100 . " ms ";
echo "\n";
}

Comments

  1. Its very good (y) if it has been tested by all means. :)
    Usman Awan

    ReplyDelete

Post a Comment