search
HomeBackend DevelopmentPHP TutorialMake your PHP 7 faster GCC PGO

We have been working hard to improve the performance of PHP7. Last month we noticed that GCC's PGO can bring nearly 10% performance improvement on WordPress. This makes us very excited.

Make your PHP 7 faster GCC PGO

However, PGO is just as its name says. As mentioned (Profile Guided Optimization, if you are interested, you can Google it), he needs to use some use cases to get feedback, which means that this optimization needs to be bound to a specific scenario.

Your optimization of one scenario may be used in another scenario One scenario backfired. It is not a universal optimization. So we cannot simply include these optimizations, nor can we directly release PHP7 compiled by PGO.

Of course, we are trying to find some common optimizations from PGO, and then Manually Apply to PHP7, but this obviously cannot achieve the effect that special optimization for a scene can achieve, so I decided to write this article to briefly introduce how to use PGO to compile PHP7, so that the PHP7 you compile can be special Make your own independent application faster.

First of all, the first thing to decide is what scenario to use to Feedback GCC. We usually choose: In the scenario you want to optimize: the one with the largest number of visits and the one that takes the most time. The page that consumes the most resources.

Take WordPress as an example. We choose the homepage of WordPress (because the homepage is often the most visited).

Let’s take my machine as an example:

<ol>
<li><span><span>Intel(R) Xeon(R) CPU           X5687  @ 3.60GHz X 16(超线程), </span></span></li>
<li><span> </span></li>
<li><span>48G Memory </span></li>
</ol>

php-fpm uses a fixed 32 worker, opcache adopts the default configuration (be sure to remember to load opcache)

Using wordpress 4.1 as the optimization scenario..

First, let’s test the current performance of WP in PHP7 (ab -n 10000 -c 100):

<ol>
<li><span><span>$ ab -n 10000 -c 100 http:</span><span>//inf-dev-maybach.weibo.com:8000/wordpress/</span><span> </span></span></li>
<li><span> </span></li>
<li>
<span>This is ApacheBench, Version 2.3 $Revision</span><span>: 655654 $> </span>
</li>
<li><span> </span></li>
<li><span>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http:<span>//www.zeustech.net/</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Licensed to The Apache Software Foundation, http:<span>//www.apache.org/</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Benchmarking inf-dev-maybach.weibo.com (be patient) </span></li>
<li><span> </span></li>
<li><span>Completed 1000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 2000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 3000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 4000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 5000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 6000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 7000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 8000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 9000 requests </span></li>
<li><span> </span></li>
<li><span>Completed 10000 requests </span></li>
<li><span> </span></li>
<li><span>Finished 10000 requests </span></li>
<li><span> </span></li>
<li><span>Server Software:        nginx/1.7.12 </span></li>
<li><span> </span></li>
<li><span>Server Hostname:        inf-dev-maybach.weibo.com </span></li>
<li><span> </span></li>
<li><span>Server Port:            8000 </span></li>
<li><span> </span></li>
<li><span>Document Path:          /wordpress/ </span></li>
<li><span> </span></li>
<li><span>Document Length:        9048 bytes </span></li>
<li><span> </span></li>
<li><span>Concurrency Level:      100 </span></li>
<li><span> </span></li>
<li><span>Time taken <span>for</span><span> tests:   8.957 seconds </span></span></li>
<li><span> </span></li>
<li><span>Complete requests:      10000 </span></li>
<li><span> </span></li>
<li><span>Failed requests:        0 </span></li>
<li><span> </span></li>
<li><span>Write errors:           0 </span></li>
<li><span> </span></li>
<li><span>Total transferred:      92860000 bytes </span></li>
<li><span> </span></li>
<li><span>HTML transferred:       90480000 bytes </span></li>
<li><span> </span></li>
<li><span>Requests per second:    1116.48 [#/sec] (mean) </span></li>
<li><span> </span></li>
<li><span>Time per request:       89.567 [ms] (mean) </span></li>
<li><span> </span></li>
<li><span>Time per request:       0.896 [ms] (mean, across all concurrent requests) </span></li>
<li><span> </span></li>
<li><span>Transfer rate:          10124.65 [Kbytes/sec] received </span></li>
</ol>

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 handle so many requests for the homepage per second.

Now, let us start teaching GCC and let it compile and run WordPress 4.1 The faster PHP7 is coming, it first requires GCC 4.0 or above, but I recommend that everyone use GCC-4.8 or above (now GCC-5.1).

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

The next thing is the difference. We must first compile PHP7 for the first time and let it generate an executable file that generates profile data:

<ol><li><span><span>$ make prof-gen <br></span></span></li></ol>

Note that we use prof -gen parameter (this is unique to the Makefile of PHP7, don’t try to do it in other projects:))

Then, let’s start training GCC:

<ol><li><span><span>$ sapi/cgi/php-cgi -T </span><span>100</span><span> /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/</span><span>null</span><span> </span></span></li></ol>

That is, let php-cgi run 100 times on the WordPress homepage , thus generating some profile information in the process.

Then, we start compiling PHP7 for the second time.

<ol>
<li><span><span>$ make prof-clean </span></span></li>
<li><span>$ make prof-<span>use</span><span> && make install </span></span></li>
</ol>

Okay, it’s that simple, PGO compilation is completed, now let’s look at the performance of PHP7 after PGO compilation :

<ol>
<li><span><span>$ ab -n10000 -c </span><span>100</span><span> http:</span><span>//inf-dev-maybach.weibo.com:8000/wordpress/</span><span> </span></span></li>
<li><span> </span></li>
<li><span>This is ApacheBench, Version <span>2.3</span><span> <span>655654</span><span> $> </span></span></span></li>
<li><span> </span></li>
<li><span>Copyright <span>1996</span><span> Adam Twiss, Zeus Technology Ltd, http:</span><span>//www.zeustech.net/</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Licensed to The Apache Software Foundation, http:<span>//www.apache.org/</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Benchmarking inf-dev-maybach.weibo.com (be patient) </span></li>
<li><span> </span></li>
<li><span>Completed <span>1000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>2000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>3000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>4000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>5000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>6000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>7000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>8000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>9000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Completed <span>10000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Finished <span>10000</span><span> requests </span></span></li>
<li><span> </span></li>
<li><span>Server Software:        nginx/<span>1.7</span><span>.</span><span>12</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Server Hostname:        inf-dev-maybach.weibo.com </span></li>
<li><span> </span></li>
<li><span>Server Port:            <span>8000</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Document Path:          /wordpress/ </span></li>
<li><span> </span></li>
<li><span>Document Length:        <span>9048</span><span> bytes </span></span></li>
<li><span> </span></li>
<li><span>Concurrency Level:      <span>100</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Time taken <span>for</span><span> tests:   </span><span>8.391</span><span> seconds </span></span></li>
<li><span> </span></li>
<li><span>Complete requests:      <span>10000</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Failed requests:        <span>0</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Write errors:           <span>0</span><span> </span></span></li>
<li><span> </span></li>
<li><span>Total transferred:      <span>92860000</span><span> bytes </span></span></li>
<li><span> </span></li>
<li><span>HTML transferred:       <span>90480000</span><span> bytes </span></span></li>
<li><span> </span></li>
<li><span>Requests per second:    <span>1191.78</span><span> [#/sec] (mean) </span></span></li>
<li><span> </span></li>
<li><span>Time per request:       <span>83.908</span><span> [ms] (mean) </span></span></li>
<li><span> </span></li>
<li><span>Time per request:       <span>0.839</span><span> [ms] (mean, across all concurrent requests) </span></span></li>
<li><span> </span></li>
<li><span>Transfer rate:          <span>10807.45</span><span> [Kbytes/sec] received </span></span></li>
</ol>

Now it can process 1191.78 QPS per second, the improvement is ~7%. Not bad (Hey, didn’t you say 10%? How come it is 7%? Haha, as I said before, we try Analyze what optimizations PGO has done, and then manually apply some common optimizations to PHP7. So in other words, ~3% of the more common optimizations have been included in PHP7, of course, this work is still continuing).

So it’s that simple. You can use the classic scenes of your own products to train GCC. In a few simple steps, you can get improved. Why not do it:)

thanks

Editor’s note: This article is written by PHP master——Bird Brother @Laruence’s work, original address: http://www.laruence.com/2015/06/19/3063.html

The above has introduced GCC PGO to make your PHP 7 faster, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.