Home  >  Article  >  Backend Development  >  Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)

Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)

慕斯
慕斯forward
2021-06-25 10:22:113318browse

We have learned so much about php7 in PHP. I don’t know how much you know about php7. I believe that a large number of people will not. If you know this part of the knowledge, then don’t worry. This article will lead you to a deeper understanding of this content.

Background of researching PHP7 technology

  1. We need to save costs under the background of the company's increase in revenue and expenditure reduction
  2. The performance of PHP7 compared to the current PHP version 5.X on Meizu Online At least doubled
  3. The daily active users of the community are growing rapidly (the average daily PV of 15 years of data has an annual growth of 348% and the average daily UV has an annual growth of 112%)
  4. The mobile Internet environment requires us to The program can respond to user requests faster to meet better user experience
  5. Desire for knowledge about new technologies (to satisfy a little bit of vanity)

PHP7 performance is small Note

Initial impression of PHP7 performance (3 times + improved than PHP5)

1. Performance comparison - quick sort algorithm (randomly generate 5000 numbers and then sort according to the quick algorithm)

Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)
PHP5.1 The average response time of quick sorting with 5000 numbers is 2587ms
PHP5.2 The average response time of quick sorting with 5000 numbers is 2625ms
PHP5.3 The average response time of quick sorting with 5000 numbers is 2509ms
PHP5.4 5000 number quick sorting average response time 2339ms
PHP7.0 5000 number quick sorting average response time 685ms

2. Performance comparison - WordPress homepage

Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)
PHP5.1 WordPress average response time 505ms
PHP5.2 WordPress average response time 521ms
PHP5.3 WordPress average response time 498ms
PHP5.4 WordPress average response time 470ms
PHP7. 0 WordPress average response time 158ms

3. Performance comparison-Flyme Community APP

Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)
PHP5.4 500 quick sorting TPS 552
PHP7.0 500 Number quick sort TPS 3165
Flyme community APP home page PHP5.4 TPS 1535
Flyme community APP home page PHP7.0 TPS 1975
Flyme community APP section list page PHP5.4 TPS 2237
Flyme community APP section List page PHP7.0 TPS 2387

Several problems encountered in performance testing & solutions

Why can the performance of PHP7 be improved so much?

1. JIT
2. Changes in Zval
3. Internal type zend_string
4. Changes in PHP arrays (HashTable and Zend Array)
5. Function calling mechanism (Function Calling Convention)
6. Through macro definitions and inline functions (inline), let the compiler complete part of the work in advance

Why is the actual business performance improvement of PHP7 only about 30%?

  1. Actual business does not necessarily have very complex calculation logic
  2. Actual business will use Redis and MYSQL, and network and IO bottlenecks affect the overall performance of PHP7
  3. HTTPS performance issues limit the capabilities of PHP7

Redis Proxy issues

The purpose of Redis Proxy is for Redis high availability & distributed caching
Passed The performance test is relatively direct connection to redis. The performance loss of using Proxy is about 10-15% (different businesses may have relatively large differences)

So is there room for optimization of Proxy?

Problems with long and short links between PHP and Redis

PHP7 Redis long connection performance is about 10% higher than short connection performance (different businesses vary greatly)

MYSQL database connection pool Question

Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)

The database connection pool is responsible for allocating, managing and releasing database connections. It allows applications to reuse an existing database connection instead of establishing a new one.
Atlas is database middleware developed and maintained by 360. It is located between the application and MySQL. It implements the client-server protocol of MySQL, communicates with the application as a server, and communicates with MySQL as a client. It shields DB details from applications and reduces the burden on MySQL.

Atlas supports main database downtime without affecting reading, read-write separation, automatic table sharding, security processing, smooth restart, connection pool, etc.
After using the database connection pool, the TPS performance leverage is increased by 80%
Let’s take a look at the effect

Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)

Some details of PHP7 performance optimization

PHP7 Opcache (about 1 times improvement)

Opcache working principle?
  1. PHP is an interpreted language. The Zend engine will interpret the PHP code into executable machine code (Operate Code) and then hand it over to the CPU for execution.
    Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)
  2. How Opcache accelerates
    Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)

  3. Look at the results after adding opcache (request average The response time has been reduced by half.)
    Understand the transformation of PHP7 performance in one minute (performance increased by 4 times)

Compiler GCC4.8 PGO (increase 5%-10%)

PGO is a compilation optimization technology that can be used with compilers such as GCC to improve the compilation efficiency of the compiler.
Although PGO can improve compilation efficiency, it is not widely used.
The reason is very simple:
1. Its complicated dual compilation model and limited usage scenarios make PGO seem useless
2. After the emergence of products like opcache, the performance improvement brought by PGO It's not very obvious.

Open multiple PHP-FPM main processes (increase about 10%)

<source lang="xml" collapse="false" first-line="1">
    #php-fpm.conf 
    listen = /dev/shm/php-fcgi.sock
    #php-fpm2.conf 
    listen = /dev/shm/php-fcgi2.sock

    #/usr/local/php/sbin/php-fpm --fpm-config /usr/local/php/etc/php-fpm.conf
    #/usr/local/php/sbin/php-fpm --fpm-config /usr/local/php/etc/php-fpm2.conf

    #代理
    upstream backend{
        server unix:/dev/shm/php-fcgi.sock;
        server unix:/dev/shm/php-fcgi2.sock;
    }
</source>

HugePage (increase 2%-3%)

The default memory is paged at 4KB, and the virtual address and the memory address need to be converted, and this conversion requires a table lookup.
In order to speed up the table lookup process, the CPU will have a built-in TLB (Translation Lookaside Buffer). Obviously, if the virtual page is smaller, the number of entries in the table will be more.
And the TLB size is limited. The more entries, the higher the Cache Miss of the TLB will be. So if we can enable large memory pages, This TLB Cache Miss can be indirectly reduced.

<source lang="xml" collapse="false" first-line="1">
    opcache.huge_code_pages=1
    sudo sysctl vm.nr_hugepages=128
</source>

Phase performance parameter optimization

PHP partial performance parameter optimization

  1. php.ini configuration

    <source lang="xml" collapse="false" first-line="1">
        opcache.enable=1
        opcache.enable_cli=1
        opcache.memory_consumption=128
        opcache.interned_strings_buffer=8
        opcache.max_accelerated_files=4000
        opcache.revalidate_freq=60
        opcache.save_comments=0
        opcache.fast_shutdown=1
        opcache.huge_code_pages=1
        opcache.file_cache=/dev/shm/opcache/
    </source>
  2. PHP-FPM

    <source lang="xml" collapse="false" first-line="1">
        listen = /dev/shm/php-fcgi.sock
        pm = static
        pm.max_children = 320
        pm.max_requests = 10240
    </source>

Unresolved issues

Nginx HTTPS performance issues

##Background of researching PHP7 technology

    We need to save costs under the background of the company's increase in revenue and expenditure reduction
  1. Compared to the current Meizu, PHP7 The performance of the online PHP version 5. The general environment of mobile Internet requires that our programs can respond to user requests faster to meet better user experience
  2. Desire for knowledge about new technologies (to satisfy a little bit of vanity)
  3. Related learning video sharing:
  4. php video tutorial

The above is the detailed content of Understand the transformation of PHP7 performance in one minute (performance increased by 4 times). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete