Optimizing LAMPhp Applications



      RootConf, 26th May 2012

        Piyush Goel
      Capillary Technologies
      piyush.goel@capillary.co.in
Optimization

     “Premature Optimization is root of all Evil”
                               - Donald Knuth

     Direction and Goal Oriented


     Takes Effort and Time


     No Silver Bullets, only guidelines and practices




  26/05/2012 RootConf
Web Applications are like Onions !!




 26/05/2012 RootConf
Web Applications are like Onions !!




                       Layered Architecture Rocks !!

 26/05/2012 RootConf
26/05/2012 RootConf
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 ??


    Apache 2.0
         Multi-Processing Modules
              mpm_prefork
              mpm_worker




 26/05/2012 RootConf
Tuning Apache

• MaxClients
• StartServers
    • Avoid thundering effect
• MaxRequestsPerChild
    • Profiling first
• AllowOverride
• HostnameLookups
• TCP Buffering
• Compression : mod_deflate/mod_gzip
    • May cause spike in CPU


  26/05/2012 RootConf
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)
 • KeepAlive : N * (t2) + t1 + K
 • For KeepAlive to perform better

                         K < t1 * ( N -1 )

 Ref: http://www.slideshare.net/techdude/building-scalable-php-applications


  26/05/2012 RootConf
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
26/05/2012 RootConf
PHP

   Opcode Caching
        eAccelerator
        APC
        Zend_Cache

   Minimize Includes
        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



 26/05/2012 RootConf
PHP Optimization contd…

      Output Buffering
          ob_start / ob_flush




      Compression
          ZLIB module
          ob_gzhandler


  26/05/2012 RootConf
26/05/2012 RootConf
MySQL under the hood




  26/05/2012 RootConf
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


   Cache static data:
       Memcached

   Swap Space:
       Actually made us


 26/05/2012 RootConf
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


 Refer:
     1) http://dev.mysql.com
     2) http://www.mysqlperformanceblog.com/2006/09/29/what-to-
     tune-in-mysql-server-after-installation/




 26/05/2012 RootConf
MySQL Tuning contd…

    Slow Query Logs:
    1) Most important
    2) Regular Monitoring
    3) log_queries_not_using_indexes
    Analyze Tables periodically


    Optimize Table
        Abrupt query behaviors


    Avoid big deletes
        Use soft deletes


    Explain & Percona’s query visualizer

 26/05/2012 RootConf
Tools and Practices




26/05/2012 RootConf
Tools and Practices

 Profiling:
         Profile regularly
         XDebug / Advanced PHP Debugger ( APD )
         KCachegrind / Webgrind
         Instrument Code with Timers


 Load Testing / Benchmarking
         Apache Bench ( ab )
         httperf
         JMeter ( personal favorite )


 Strong Monitoring and Alerting
         Store exhaustively, Alert Judiciously
         Ganglia, Cacti
         Nagios etc


   26/05/2012 RootConf
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
  6) netstat : Complete network dump
  7) htop : Prefer over top
  8) cut / tr : Analyzing Apache Access Logs




  26/05/2012 RootConf
What It shouldn’t be !!




                       SysAdmins vs Developers
 26/05/2012 RootConf
Personal Rant/Advice




          Understand what’s happening under the hood


  26/05/2012 RootConf
THANKS !


Capillary is looking for awesome DevAdmins!
                      piyush.goel@capillary.co.in
                      career-tech@capillary.co.in




26/05/2012 RootConf

Optimizing LAMPhp Applications

  • 1.
    Optimizing LAMPhp Applications RootConf, 26th May 2012 Piyush Goel Capillary Technologies piyush.goel@capillary.co.in
  • 2.
    Optimization “Premature Optimization is root of all Evil” - Donald Knuth Direction and Goal Oriented Takes Effort and Time No Silver Bullets, only guidelines and practices 26/05/2012 RootConf
  • 3.
    Web Applications arelike Onions !! 26/05/2012 RootConf
  • 4.
    Web Applications arelike Onions !! Layered Architecture Rocks !! 26/05/2012 RootConf
  • 5.
  • 6.
    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 ?? Apache 2.0 Multi-Processing Modules mpm_prefork mpm_worker 26/05/2012 RootConf
  • 7.
    Tuning Apache • MaxClients •StartServers • Avoid thundering effect • MaxRequestsPerChild • Profiling first • AllowOverride • HostnameLookups • TCP Buffering • Compression : mod_deflate/mod_gzip • May cause spike in CPU 26/05/2012 RootConf
  • 8.
    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) • KeepAlive : N * (t2) + t1 + K • For KeepAlive to perform better K < t1 * ( N -1 ) Ref: http://www.slideshare.net/techdude/building-scalable-php-applications 26/05/2012 RootConf
  • 9.
    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
  • 10.
  • 11.
    PHP Opcode Caching eAccelerator APC Zend_Cache Minimize Includes 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 26/05/2012 RootConf
  • 12.
    PHP Optimization contd… Output Buffering ob_start / ob_flush Compression ZLIB module ob_gzhandler 26/05/2012 RootConf
  • 13.
  • 14.
    MySQL under thehood 26/05/2012 RootConf
  • 15.
    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 Cache static data: Memcached Swap Space: Actually made us 26/05/2012 RootConf
  • 16.
    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 Refer: 1) http://dev.mysql.com 2) http://www.mysqlperformanceblog.com/2006/09/29/what-to- tune-in-mysql-server-after-installation/ 26/05/2012 RootConf
  • 17.
    MySQL Tuning contd… Slow Query Logs: 1) Most important 2) Regular Monitoring 3) log_queries_not_using_indexes Analyze Tables periodically Optimize Table Abrupt query behaviors Avoid big deletes Use soft deletes Explain & Percona’s query visualizer 26/05/2012 RootConf
  • 18.
  • 19.
    Tools and Practices Profiling: Profile regularly XDebug / Advanced PHP Debugger ( APD ) KCachegrind / Webgrind Instrument Code with Timers Load Testing / Benchmarking Apache Bench ( ab ) httperf JMeter ( personal favorite ) Strong Monitoring and Alerting Store exhaustively, Alert Judiciously Ganglia, Cacti Nagios etc 26/05/2012 RootConf
  • 20.
    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 6) netstat : Complete network dump 7) htop : Prefer over top 8) cut / tr : Analyzing Apache Access Logs 26/05/2012 RootConf
  • 21.
    What It shouldn’tbe !! SysAdmins vs Developers 26/05/2012 RootConf
  • 22.
    Personal Rant/Advice Understand what’s happening under the hood 26/05/2012 RootConf
  • 23.
    THANKS ! Capillary islooking for awesome DevAdmins! piyush.goel@capillary.co.in career-tech@capillary.co.in 26/05/2012 RootConf