Speed Tests


Gaining speed is the most important target when using persistent database connections. This section presents some simple methods to compute how much speed you can gain by using persistent database connections.

The following is a benchmark script for testing the performance gain of persistent database connections:

 <?php # checking time function gettime() {      $tmp = microtime();      $parts = explode(" ",$tmp);      $floattime = (float)$parts[0] + (float)$parts[1];      return $floattime; } # processing the benchmark several times for  ($i=0; $i<5; $i++) {      # staring with persistent connection      $starttime = gettime();      $connect = pg_pconnect("dbname=db user=hs");      $endtime = gettime();      $persistent = $endtime - $starttime;      # checking non-persistent connection      $starttime = gettime();      $connect = pg_connect("dbname=db user=hs");      $endtime = gettime();      $no_persistent = $endtime - $starttime;      # printing the result      echo "non-persistent connection took: $no_persistent <br>";      echo "persistent took: $persistent <br>";      $gain = $no_persistent / $persistent;      echo "This is $gain times faster.<br><br>"; } ?> 

You can use this to find out how much performance you can gain by using persistent database connections.

Let's go through the code quickly. gettime computes the current time we will use to find out how long the authentication process takes. A loop is processed, which establishes a persistent and a non-persistent connection to the database every time the loop is processed . The amount of time needed to connect to the database is measured and compared. We execute the script by using our favorite Web browser:

 non-persistent connection took: 0.024909973144531 persistent took: 0.00016999244689941 This is 146.53576437588 times faster. non-persistent connection took: 0.024687051773071 persistent took: 0.00014996528625488 This is 164.61844197138 times faster. non-persistent connection took: 0.024754047393799 persistent took: 0.00013101100921631 This is 188.94631483167 times faster. non-persistent connection took: 0.020581007003784 persistent took: 0.00013399124145508 This is 153.59964412811 times faster. non-persistent connection took: 0.024953961372375 persistent took: 0.00013101100921631 This is 190.47224749773 times faster. 

The results are extremely impressive ”using persistent connections can be up to 190 times faster (in our example). The reason for a result like that seems obvious, because the authentication process of PostgreSQL is far more complex than finding the right connection out of only a few possibilities. We have tested the script several times and the results have always been nearly the same. Only the first time we executed the script led to different results ”as discussed in the next section.



PostgreSQL Developer's Handbook2001
PostgreSQL Developer's Handbook2001
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net