Make your PHP 7 faster (gcc pgo)

Source: Internet
Author: User

Make your PHP 7 faster (gcc pgo)

We have been committed to improving the performance of PHP 7. Last month we noticed that the gcc pgo can improve the performance by nearly 10% on Wordpress, which made us very excited.

However, PGO, as the name suggests (Google can be used for Profile Guided Optimization), needs some use cases for feedback, that is, the Optimization needs to be bound to a specific scenario.

Your Optimization for one scenario may be counterproductive in another scenario. it is not a general optimization. therefore, we cannot simply include these optimizations or directly release the php7after PGO compilation.

Of course, we are trying to find some common optimizations from PGO and Apply them to PHP7 manually, but this obviously cannot achieve the effect of special optimization for a scenario, so I decided to write this article to briefly introduce how to use PGO to compile PHP7, so that the PHP7 compiled by you can specifically make your own independent applications faster.

First, we need to decide what scenarios to use for Feedback GCC. We generally choose: In the scenario you want to optimize: the most visited, the most time-consuming, the most resource-consuming page.

Take Wordpress as an example. We select the Wordpress homepage (because the homepage is usually the most visited ).

Let's take my machine as an example.:

 
 
  1. Intel (R) Xeon (R) CPU X5687 @ 3.60 GHz X 16 (hyper-threading ),
  2.  
  3. 48G Memory

Php-fpm uses 32 fixed workers, and opcache uses the default configuration (remember to load opcache)

Wordpress 4.1 is used as the optimization scenario ..

First, we will test the current performance of WP in PHP 7 (AB-n 10000-c 100 ):

 
 
  1. $ ab -n 10000 -c 100 http://inf-dev-maybach.weibo.com:8000/wordpress/ 
  2.  
  3. This is ApacheBench, Version 2.3 <$Revision: 655654 $> 
  4.  
  5. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ 
  6.  
  7. Licensed to The Apache Software Foundation, http://www.apache.org/ 
  8.  
  9. Benchmarking inf-dev-maybach.weibo.com (be patient) 
  10.  
  11. Completed 1000 requests 
  12.  
  13. Completed 2000 requests 
  14.  
  15. Completed 3000 requests 
  16.  
  17. Completed 4000 requests 
  18.  
  19. Completed 5000 requests 
  20.  
  21. Completed 6000 requests 
  22.  
  23. Completed 7000 requests 
  24.  
  25. Completed 8000 requests 
  26.  
  27. Completed 9000 requests 
  28.  
  29. Completed 10000 requests 
  30.  
  31. Finished 10000 requests 
  32.  
  33. Server Software:        nginx/1.7.12 
  34.  
  35. Server Hostname:        inf-dev-maybach.weibo.com 
  36.  
  37. Server Port:            8000 
  38.  
  39. Document Path:          /wordpress/ 
  40.  
  41. Document Length:        9048 bytes 
  42.  
  43. Concurrency Level:      100 
  44.  
  45. Time taken for tests:   8.957 seconds 
  46.  
  47. Complete requests:      10000 
  48.  
  49. Failed requests:        0 
  50.  
  51. Write errors:           0 
  52.  
  53. Total transferred:      92860000 bytes 
  54.  
  55. HTML transferred:       90480000 bytes 
  56.  
  57. Requests per second:    1116.48 [#/sec] (mean) 
  58.  
  59. Time per request:       89.567 [ms] (mean) 
  60.  
  61. Time per request:       0.896 [ms] (mean, across all concurrent requests) 
  62.  
  63. Transfer rate:          10124.65 [Kbytes/sec] received 

It can be seen that Wordpress 4.1 is currently on this machine, and the QPS of the homepage can reach 1116.48. That is, it can process so many requests to the homepage every second,

Now, let's start to teach GCC and let him compile PHP7 that runs Wordpress4.1 faster. First, we need GCC 4.0 or later versions, but I suggest you use a version of GCC-4.8 or above (all GCC-5.1 now ).

The first step is to download the source code of PHP 7, and then do./configure. There is no difference between these.

Next there is a difference. We need to first compile PHP7 so that it can generate an executable file that generates profile data:

 
 
  1. $ make prof-gen

Note that the prof-gen parameter is used (this is unique to the Makefile of PHP7. Do not try this on other projects too :))

Then, let's start training GCC:

 
 
  1. $ sapi/cgi/php-cgi -T 100 /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/null 

That is, let php-cgi run the wordpress homepage 100 times to generate some profile information during this process.

Then, we start to compile php7for the second time.

 
 
  1. $ make prof-clean 
  2. $ make prof-use && make install 

Okay, that's simple. PGO compilation is complete. Now let's take a look at the performance of PHP7 after PGO Compilation:

 
 
  1. $ ab -n10000 -c 100 http://inf-dev-maybach.weibo.com:8000/wordpress/ 
  2.  
  3. This is ApacheBench, Version 2.3 <$Revision: 655654 $> 
  4.  
  5. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ 
  6.  
  7. Licensed to The Apache Software Foundation, http://www.apache.org/ 
  8.  
  9. Benchmarking inf-dev-maybach.weibo.com (be patient) 
  10.  
  11. Completed 1000 requests 
  12.  
  13. Completed 2000 requests 
  14.  
  15. Completed 3000 requests 
  16.  
  17. Completed 4000 requests 
  18.  
  19. Completed 5000 requests 
  20.  
  21. Completed 6000 requests 
  22.  
  23. Completed 7000 requests 
  24.  
  25. Completed 8000 requests 
  26.  
  27. Completed 9000 requests 
  28.  
  29. Completed 10000 requests 
  30.  
  31. Finished 10000 requests 
  32.  
  33. Server Software:        nginx/1.7.12 
  34.  
  35. Server Hostname:        inf-dev-maybach.weibo.com 
  36.  
  37. Server Port:            8000 
  38.  
  39. Document Path:          /wordpress/ 
  40.  
  41. Document Length:        9048 bytes 
  42.  
  43. Concurrency Level:      100 
  44.  
  45. Time taken for tests:   8.391 seconds 
  46.  
  47. Complete requests:      10000 
  48.  
  49. Failed requests:        0 
  50.  
  51. Write errors:           0 
  52.  
  53. Total transferred:      92860000 bytes 
  54.  
  55. HTML transferred:       90480000 bytes 
  56.  
  57. Requests per second:    1191.78 [#/sec] (mean) 
  58.  
  59. Time per request:       83.908 [ms] (mean) 
  60.  
  61. Time per request:       0.839 [ms] (mean, across all concurrent requests) 
  62.  
  63. Transfer rate:          10807.45 [Kbytes/sec] received 

Now we can process 1191.78 QPS per second. The increase is ~ 7%. Not long enough (sorry, didn't you say 10%? Why is it 7%? Haha, as I said before, we try to analyze what PGO has done, and then Apply some General optimizations to PhP 7 manually. That is to say, that ~ 3% of the more general optimizations have been included in PHP7, and of course this work is still going on ).

So it's that simple. You can use the classic scenario of your product to train GCC. It's just a few steps to improve it. Why not :)

Thanks

Editor's note: This article is the PHP God -- Bird brother @ Laruence works, the original address: http://www.laruence.com/2015/06/19/3063.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.