PHP site optimization




 Demonstrating a Zend framework website
performance improvement and optimization.
Areas of improvement
Application Framework
Database
Network Latency
IO Contention
CPU Utilization
Network Connectivity
Others
Performance or Scalability. Both?
Hiphop (Facebook implementation of php) is high
 performing but not scalable
Workload distribution of a page
              request
10% web server
40% php processing
50% database response


10-15% improvement easily achievable using a
 commonly used alternative stack i.e. Nginx +
 APC + Htmlcaching + Memcache + CDN
Database performance
Query simplification
Query optimization
Schema tuning


Replication
Sharding
Clustering
Network Latency
How to identify?
When both page response and CPU usage are
 slow
Reasons?
DNS Reverse Lookups, TCP Handshakes, High
 number of hops
Tools?
Tcpdump, ping, traceroute ..
DNS Reverse Lookups
Use ipaddress wherever possible
TCP Handshakes
Use persistent connections to remote/local-
 network services wherever possible
Number of Hops
Try to put servers on same switch or in the same
  LAN.
Physical layer and network layer trade offs to be
 avoided
IO Contention
CPU Utilization
Network Connectivity
ORM options
Active Record (made popular by Ruby on Rails)
Data Mapper
Collection
Doctrine
Data access methods used by Yii, CI,
 Symphony ...
Active Record (AR)
It is an object-relational mapping and object-
   persistence pattern
It binds a business object with a relational record
   (row)
AR class maps to a db table (or view)
AR instance maps to a db table record
AR instance properties maps to db table record
 fields
Instance methods act on a specific record
Active Record Issues
Data Patterns in Zend Framework
 Zend_Controller_Front
   Singleton
   FrontController
 Zend_Db_Table
   Table Data Gateway
 Zend_Log
   Factory Method
   Adapter
Profiler with Zend_DB
$db = Zend_Db::factory('Pdo_Mysql',
      array('host' => '127.0.0.1',
     'username' => 'user1',
     'password' => 'pwd',
     'dbname', 'ex_db',
     'profiler' => true));
After query execution using $db:-
$prof = $db->getProfiler();
$prof->getTotalElapsedSecs();
$prof->getTotalNumQueries();
Preg
Stripos is 2 times faster than preg
ctype_alnum is 5 times faster than preg
Casting “if ($var == (int) $var)” is 5 times faster
 than preg_match(“/^d*$/”, $var)
Magic methods
__get, __set, __call
Used by Soap, data tables, java objects
Use sparingly and avoid too much recursion
Code Acceleration
APC, Zend Optimizer
Increase performance by 3-4 times
Queue
Queueing is offloading long running tasks to
 queuing system
Job Queue
  Gearman
Message Queue
  ActiveMQ
  AWS Simple Queue Service
What not to do
Caching should be the last thing to do. (Focus on
 performance optimization)
Avoid LIKE queries. (Try Solr, Zend_Lucene)
What to do
Minimize require_once. Use autoloader
Horizontal architecture is better than vertical
  ex. wide inheritance is better than deep level
    inheritance
Lazy loading / Load on Demand
Use diagnostic tools
  Zend studio profiler, Code Tracing
Benchmarking

Php Site Optimization

  • 1.
    PHP site optimization Demonstrating a Zend framework website performance improvement and optimization.
  • 2.
    Areas of improvement ApplicationFramework Database Network Latency IO Contention CPU Utilization Network Connectivity Others
  • 3.
    Performance or Scalability.Both? Hiphop (Facebook implementation of php) is high performing but not scalable
  • 4.
    Workload distribution ofa page request 10% web server 40% php processing 50% database response 10-15% improvement easily achievable using a commonly used alternative stack i.e. Nginx + APC + Htmlcaching + Memcache + CDN
  • 5.
    Database performance Query simplification Queryoptimization Schema tuning Replication Sharding Clustering
  • 6.
    Network Latency How toidentify? When both page response and CPU usage are slow Reasons? DNS Reverse Lookups, TCP Handshakes, High number of hops Tools? Tcpdump, ping, traceroute ..
  • 7.
    DNS Reverse Lookups Useipaddress wherever possible
  • 8.
    TCP Handshakes Use persistentconnections to remote/local- network services wherever possible
  • 9.
    Number of Hops Tryto put servers on same switch or in the same LAN. Physical layer and network layer trade offs to be avoided
  • 10.
  • 11.
  • 12.
  • 13.
    ORM options Active Record(made popular by Ruby on Rails) Data Mapper Collection Doctrine Data access methods used by Yii, CI, Symphony ...
  • 14.
    Active Record (AR) Itis an object-relational mapping and object- persistence pattern It binds a business object with a relational record (row) AR class maps to a db table (or view) AR instance maps to a db table record AR instance properties maps to db table record fields Instance methods act on a specific record
  • 15.
  • 16.
    Data Patterns inZend Framework Zend_Controller_Front Singleton FrontController Zend_Db_Table Table Data Gateway Zend_Log Factory Method Adapter
  • 17.
    Profiler with Zend_DB $db= Zend_Db::factory('Pdo_Mysql', array('host' => '127.0.0.1', 'username' => 'user1', 'password' => 'pwd', 'dbname', 'ex_db', 'profiler' => true)); After query execution using $db:- $prof = $db->getProfiler(); $prof->getTotalElapsedSecs(); $prof->getTotalNumQueries();
  • 18.
    Preg Stripos is 2times faster than preg ctype_alnum is 5 times faster than preg Casting “if ($var == (int) $var)” is 5 times faster than preg_match(“/^d*$/”, $var)
  • 19.
    Magic methods __get, __set,__call Used by Soap, data tables, java objects Use sparingly and avoid too much recursion
  • 20.
    Code Acceleration APC, ZendOptimizer Increase performance by 3-4 times
  • 21.
    Queue Queueing is offloadinglong running tasks to queuing system Job Queue Gearman Message Queue ActiveMQ AWS Simple Queue Service
  • 22.
    What not todo Caching should be the last thing to do. (Focus on performance optimization) Avoid LIKE queries. (Try Solr, Zend_Lucene)
  • 23.
    What to do Minimizerequire_once. Use autoloader Horizontal architecture is better than vertical ex. wide inheritance is better than deep level inheritance Lazy loading / Load on Demand Use diagnostic tools Zend studio profiler, Code Tracing
  • 24.