Php Site Optimization
-
Upload
amit-kejriwal -
Category
Technology
-
view
787 -
download
1
Transcript of Php Site Optimization
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
Active Record (made popular by Ruby on Rails)
Data Mapper
Collection
Doctrine
Data access methods used by Yii, CI, Symphony ...
ORM options
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