Php Site Optimization


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Php Site Optimization

  1. 1. PHP site optimization Demonstrating a Zend framework websiteperformance improvement and optimization.
  2. 2. Areas of improvementApplication FrameworkDatabaseNetwork LatencyIO ContentionCPU UtilizationNetwork ConnectivityOthers
  3. 3. Performance or Scalability. Both?Hiphop (Facebook implementation of php) is high performing but not scalable
  4. 4. Workload distribution of a page request10% web server40% php processing50% database response10-15% improvement easily achievable using a commonly used alternative stack i.e. Nginx + APC + Htmlcaching + Memcache + CDN
  5. 5. Database performanceQuery simplificationQuery optimizationSchema tuningReplicationShardingClustering
  6. 6. Network LatencyHow to identify?When both page response and CPU usage are slowReasons?DNS Reverse Lookups, TCP Handshakes, High number of hopsTools?Tcpdump, ping, traceroute ..
  7. 7. DNS Reverse LookupsUse ipaddress wherever possible
  8. 8. TCP HandshakesUse persistent connections to remote/local- network services wherever possible
  9. 9. Number of HopsTry to put servers on same switch or in the same LAN.Physical layer and network layer trade offs to be avoided
  10. 10. IO Contention
  11. 11. CPU Utilization
  12. 12. Network Connectivity
  13. 13. ORM optionsActive Record (made popular by Ruby on Rails)Data MapperCollectionDoctrineData access methods used by Yii, CI, Symphony ...
  14. 14. Active Record (AR)It is an object-relational mapping and object- persistence patternIt 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 recordAR instance properties maps to db table record fieldsInstance methods act on a specific record
  15. 15. Active Record Issues
  16. 16. Data Patterns in Zend Framework Zend_Controller_Front Singleton FrontController Zend_Db_Table Table Data Gateway Zend_Log Factory Method Adapter
  17. 17. Profiler with Zend_DB$db = Zend_Db::factory(Pdo_Mysql, array(host =>, username => user1, password => pwd, dbname, ex_db, profiler => true));After query execution using $db:-$prof = $db->getProfiler();$prof->getTotalElapsedSecs();$prof->getTotalNumQueries();
  18. 18. PregStripos is 2 times faster than pregctype_alnum is 5 times faster than pregCasting “if ($var == (int) $var)” is 5 times faster than preg_match(“/^d*$/”, $var)
  19. 19. Magic methods__get, __set, __callUsed by Soap, data tables, java objectsUse sparingly and avoid too much recursion
  20. 20. Code AccelerationAPC, Zend OptimizerIncrease performance by 3-4 times
  21. 21. QueueQueueing is offloading long running tasks to queuing systemJob Queue GearmanMessage Queue ActiveMQ AWS Simple Queue Service
  22. 22. What not to doCaching should be the last thing to do. (Focus on performance optimization)Avoid LIKE queries. (Try Solr, Zend_Lucene)
  23. 23. What to doMinimize require_once. Use autoloaderHorizontal architecture is better than vertical ex. wide inheritance is better than deep level inheritanceLazy loading / Load on DemandUse diagnostic tools Zend studio profiler, Code Tracing
  24. 24. Benchmarking