Optimizing LAMPhp Applications
-
Upload
piyush-goel -
Category
Technology
-
view
953 -
download
1
description
Transcript of Optimizing LAMPhp Applications
![Page 1: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/1.jpg)
Optimizing LAMPhp Applications
RootConf, 26th May 2012
Piyush GoelCapillary Technologies [email protected]
![Page 2: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/2.jpg)
Optimization
Ø “Premature Optimization is root of all Evil”
- Donald Knuth
Ø Direction and Goal Oriented
26/05/2012 RootConf
Ø Takes Effort and Time
Ø No Silver Bullets, only guidelines and practices
![Page 3: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/3.jpg)
Web Applications are like Onions !!
26/05/2012 RootConf
![Page 4: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/4.jpg)
Web Applications are like Onions !!
26/05/2012 RootConf
Layered Architecture Rocks !!
![Page 5: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/5.jpg)
26/05/2012 RootConf
![Page 6: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/6.jpg)
Apache
Ø Apache 1.3
Ø Pre-forked Model
Ø 1 process per request
Ø Robust: One crashed process dosen’t affect the others
Ø Not as fast as some other possible designs - Really ??
26/05/2012 RootConf
Ø Apache 2.0
Ø Multi-Processing Modules
Ømpm_prefork
Ømpm_worker
![Page 7: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/7.jpg)
Tuning Apache
• MaxClients
• StartServers
• Avoid thundering effect
• MaxRequestsPerChild
• Profiling first
• AllowOverride
26/05/2012 RootConf
• AllowOverride
• HostnameLookups
• TCP Buffering
• Compression : mod_deflate/mod_gzip
• May cause spike in CPU
![Page 8: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/8.jpg)
Tuning Apache Contd
• KeepAlive
• N objects per page
• t1 seconds for TCP connection
• t2 seconds per object
• K seconds timeout
• Non-KeepAlive : N * (t1 + t2)
26/05/2012 RootConf
• Non-KeepAlive : N * (t1 + t2)
• KeepAlive : N * (t2) + t1 + K• For KeepAlive to perform better
K < t1 * ( N -1 )
Ref: http://www.slideshare.net/techdude/building-scalable-php-applications
![Page 9: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/9.jpg)
Tuning Apache Contd…
Ø Offload Static Files
Ø Apache + PHP not really good at static files
Ø Use Nginx or Lighttpd ( or anything else )
26/05/2012 RootConf
![Page 10: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/10.jpg)
26/05/2012 RootConf
![Page 11: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/11.jpg)
PHP
Ø Opcode CachingØ eAccelerator
Ø APC
Ø Zend_Cache
Ø Minimize Includes
Ø 1 include: 1 stat and realpath system call
26/05/2012 RootConf
Ø 1 include: 1 stat and realpath system call
Ø Relative paths : too many stat system calls
Ø Minimal include_path
ØReduce logging : prevents disk writes
Ø error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR
![Page 12: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/12.jpg)
PHP Optimization contd…
Ø Output Buffering
Ø ob_start / ob_flush
26/05/2012 RootConf
Ø Compression
Ø ZLIB module
Ø ob_gzhandler
![Page 13: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/13.jpg)
26/05/2012 RootConf
![Page 14: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/14.jpg)
MySQL under the hood
26/05/2012 RootConf
![Page 15: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/15.jpg)
MySQL Tuning
Ø Hardware Config:Ø Typical workloads are Memory/IO Bound
Ø Size of working data set
Ø Storage Engine:
Ø MyISAM vs InnoDB
Ø InnoDB works well in most cases, unless you need full text search
26/05/2012 RootConf
Ø InnoDB works well in most cases, unless you need full text search
Ø Cache static data:Ø Memcached
Ø Swap Space:
Ø Actually made us
![Page 16: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/16.jpg)
MySQL Tuning Contd…Ø key_buffer_size
Ø innodb_buffer_pool_size
Ø innodb_log_file_size
Ø innodb_log_buffer_size
Ø innodb_flush_log_at_trx_commit
Ø thread_cache
Ø query_cache_size
26/05/2012 RootConf
Ø query_cache_size
Refer:
1) http://dev.mysql.com
2) http://www.mysqlperformanceblog.com/2006/09/29/what-to-tune-in-mysql-server-after-installation/
![Page 17: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/17.jpg)
MySQL Tuning contd…
Ø Slow Query Logs:
1) Most important
2) Regular Monitoring
3) log_queries_not_using_indexes
Ø Analyze Tables periodically
26/05/2012 RootConf
Ø Optimize Table
Ø Abrupt query behaviors
Ø Avoid big deletes
Ø Use soft deletes
Ø Explain & Percona’s query visualizer
![Page 18: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/18.jpg)
26/05/2012 RootConf
Tools and Practices
![Page 19: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/19.jpg)
Tools and Practices
Profiling:
Ø Profile regularly
Ø XDebug / Advanced PHP Debugger ( APD )
Ø KCachegrind / Webgrind
Ø Instrument Code with Timers
Load Testing / Benchmarking
Apache Bench ( ab )
26/05/2012 RootConf
Ø Apache Bench ( ab )
Ø httperf
Ø JMeter ( personal favorite )
Strong Monitoring and Alerting
Ø Store exhaustively, Alert Judiciously
Ø Ganglia, Cacti
Ø Nagios etc
![Page 20: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/20.jpg)
Tools & Practices
1) vmstat : Extremely useful for finding bottlenecks
2) iostat : Disk activity, useful for db systems
3) pmap : Process memory dump
4) strace: Analyzing system calls
5) sar : System activity reporter, good for postanalysis
26/05/2012 RootConf
5) sar : System activity reporter, good for postanalysis
6) netstat : Complete network dump
7) htop : Prefer over top
8) cut / tr : Analyzing Apache Access Logs
![Page 21: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/21.jpg)
What It shouldn’t be !!
26/05/2012 RootConf
SysAdmins vs Developers
![Page 22: Optimizing LAMPhp Applications](https://reader033.fdocuments.in/reader033/viewer/2022061222/54bf422b4a795906428b458c/html5/thumbnails/22.jpg)
Personal Rant/Advice
26/05/2012 RootConf
Understand what’s happening under the hood