Improving PHP Application Performance with APC

28,009 views

Published on

Overview of performance gains with APC

Published in: Technology
4 Comments
32 Likes
Statistics
Notes
  • Can you please explain why it is not suited for distributed/load balanced environment ?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • good one, thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • the performance of apc stat or not is very different
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Nice starter pack for PHP APC (alternative php cache)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
28,009
On SlideShare
0
From Embeds
0
Number of Embeds
2,784
Actions
Shares
0
Downloads
0
Comments
4
Likes
32
Embeds 0
No embeds

No notes for slide

Improving PHP Application Performance with APC

  1. 1. Improving PHP Application performance with APC Presented by James McLean
  2. 2. Improving PHP Application Performance with APC <ul><li>Who is this clown? </li></ul><ul><li>PHP Introduction </li></ul><ul><li>APC Introduction </li></ul><ul><li>Testing Environment </li></ul><ul><li>Opcodes? </li></ul><ul><li>APC Installation </li></ul><ul><li>APC Configuration </li></ul><ul><li>APC Shared Memory </li></ul><ul><li>APC Vanilla Install Performance </li></ul><ul><li>APC & Stat </li></ul><ul><li>APC & Caching Variables </li></ul><ul><li>Conclusion </li></ul>
  3. 3. Who is this clown? <ul><li>PHP User since early Version 3. </li></ul><ul><li>PHP Developer since early 2000 </li></ul><ul><li>Zend Certified Engineer, PHP5 </li></ul><ul><li>Web Developer at Iugo Pty Ltd, Adelaide. </li></ul><ul><li>irc://irc.freenode.org/#phpc Channel Operator </li></ul><ul><li>Personal Blog: http://blog.phpguy.org </li></ul>
  4. 4. PHP Introduction <ul><li>PHP: Hypertext Preprocessor </li></ul><ul><li>PHP/FI, Created in 1995 by Rasmus Lerdorf </li></ul><ul><li>General Purpose Language </li></ul><ul><li>Ideally suited to Web Development </li></ul><ul><li>Can be used on the command line and for shell scripting, even for GUI apps! </li></ul>
  5. 5. APC Introduction <ul><li>APC: Alternative PHP Cache. </li></ul><ul><li>Free, Open Source Opcode Cache. </li></ul><ul><li>Benefit from simply installing the module, no code changes required. </li></ul><ul><li>Ability to cache variables with code changes. </li></ul>
  6. 6. Testing Environment <ul><li>All tests were performed with the following: </li></ul><ul><li>Intel Core 2 Quad Q9450 @ 3.2Ghz </li></ul><ul><li>8GB DDR2 800Mhz RAM </li></ul><ul><li>APC 3.0.19, PHP 5.2.6 & PHP 5.2.6 From Gentoo Portage </li></ul><ul><li>Custom OO PHP5 Framework </li></ul><ul><li>PDO Used for all Database Access </li></ul><ul><li>ab2 -k -c 50 -t 300 http://devwww.vortexdvd.com/ </li></ul>
  7. 7. Opcodes? <ul><li>Opcodes are, simply, compiled PHP code. </li></ul><ul><li>Discarded after the page has been executed. </li></ul><ul><li>PHP Page Compilation: </li></ul><ul><li>- Scanning / Lexing. Plain Text Code turned into Tokens </li></ul><ul><li>- Parsing. Tokens are collected into Expressions. </li></ul><ul><li>- Compilation. Expressions are translated into Opcodes for Execution. </li></ul><ul><li>- Execution. Opcode stacks are processed, one at a time. </li></ul><ul><li>APC caches the Opcodes for later execution! </li></ul>
  8. 8. APC Installation <ul><li>Quick and Easy to install! </li></ul><ul><li>PECL - http://pecl.php.net </li></ul><ul><li>- # pecl install apc . </li></ul><ul><li>GNU/Linux Package Managers. </li></ul><ul><li>- Gentoo Portage: dev-php5/pecl-apc . </li></ul><ul><li>- Debian/Ubuntu: # pecl install apc </li></ul><ul><li>Windows: http://pecl4win.php.net </li></ul><ul><li>Ensure the module is loaded into php.ini. </li></ul><ul><li>- extension=apc.so </li></ul>
  9. 9. APC Configuration <ul><li>In a basic APC installation, two major configuration options affect the performance of your applications </li></ul><ul><li>apc.shm_size – Shared Memory Size. </li></ul><ul><li>apc.stat – Stat files. </li></ul>
  10. 10. APC Shared Memory <ul><li>apc.shm_size controls how much memory will be set aside for APC to use. </li></ul><ul><li>Memory is cheap! </li></ul><ul><li>Increasing the memory available to APC will not always improve performance signifigantly. </li></ul>
  11. 11. APC Vanilla Install Performance <ul><li>As mentioned before, you can get a performance gain, simply from installing the module. </li></ul><ul><li>No APC Enabled: </li></ul><ul><li>APC Enabled, Vanilla Settings: </li></ul>Requests per second: 71.27 [#/sec] (mean) Time per request: 701.510 [ms] (mean) Time per request: 14.030 [ms] (mean, across all concurrent requests) Transfer rate: 898.79 [Kbytes/sec] received Requests per second: 93.55 [#/sec] (mean) Time per request: 534.454 [ms] (mean) Time per request: 10.689 [ms] (mean, across all concurrent requests) Transfer rate: 1181.05 [Kbytes/sec] received Whoa!
  12. 12. APC & Stat <ul><li>No.. Not that stat... </li></ul>
  13. 13. APC & Stat <ul><li>By Default, PHP checks the modification time of the script and all includes at Execution. </li></ul><ul><li>You can disable stat checking, for extra performance! </li></ul><ul><li>Web Server restarts required when files change. </li></ul>
  14. 14. APC & Stat <ul><li>Stat Testing: </li></ul><ul><li>$ ab2 -k -c 50 -t 300 http://devwww.vortexdvd.com/ </li></ul><ul><li>apc.stat = on </li></ul><ul><li>apc.stat = off </li></ul>Requests per second: 129.46 [#/sec] (mean) Time per request: 386.208 [ms] (mean) Time per request: 7.724 [ms] (mean, across all concurrent requests) Transfer rate: 1634.77 [Kbytes/sec] received Requests per second: 278.58 [#/sec] (mean) Time per request: 179.481 [ms] (mean) Time per request: 3.590 [ms] (mean, across all concurrent requests) Transfer rate: 3516.54 [Kbytes/sec] received EPIC WIN!!
  15. 15. APC & Stat <ul><li>Turning off File Stat yields an excellent performance benefit! </li></ul><ul><li>Best if your site files don't change very often. </li></ul><ul><li>Requires a web-server restart when the files are changed. </li></ul>
  16. 16. APC & Variable Caching <ul><li>APC allows you to cache variables for speed. </li></ul><ul><li>Large variables and variables that dont change often, ideally suited to variable caching. </li></ul><ul><li>Caching Database Query Result Sets can result in extra performance, and save your database! </li></ul>
  17. 17. APC & Variable Caching <ul><li>Custom code to enable or disable use of the Query Cache. </li></ul><ul><li>Query Cache Disabled </li></ul><ul><li>Query Cache Enabled </li></ul>Requests per second: 124.55 [#/sec] (mean) Time per request: 401.436 [ms] (mean) Time per request: 8.029 [ms] (mean, across all concurrent requests) Transfer rate: 1572.57 [Kbytes/sec] received Requests per second: 91.97 [#/sec] (mean) Time per request: 543.680 [ms] (mean) Time per request: 10.874 [ms] (mean, across all concurrent requests) Transfer rate: 1160.01 [Kbytes/sec] received WIN!
  18. 18. Conclusion <ul><li>APC is a great way to get some 'free speed' out of your applications. </li></ul><ul><ul><li>- Benefit from installing the module, vanilla config! </li></ul></ul><ul><ul><li>- Minor config changes for a huge benefit with Stat </li></ul></ul><ul><li>APC doesn't suit everyone and all situations. </li></ul><ul><ul><li>- Does not suit distributed servers or load balanced. </li></ul></ul><ul><li>Overall, APC is worth a look next time you need to improve your PHP application performance. </li></ul>
  19. 19. References <ul><li>http://www.php.net/apc </li></ul><ul><li>http://www.debian-administration.org/articles/574 </li></ul><ul><li>http://blog.phpguy.org/2007/09/20/apc-and-web-application-performance/ </li></ul><ul><li>http://blog.phpguy.org/2007/09/21/apc-and-web-application-performance-take-2/ </li></ul><ul><li>http://blog.libssh2.org/index.php?/archives/92-Understanding-Opcodes.html </li></ul>

×