Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the contents of PHP7 performance optimization?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about the performance optimization of PHP7, which the editor thinks is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Opcache

1. Opcache principle

The normal execution process of PHP is as follows

Request request (nginx,apache,cli, etc.)-> Zend engine reads .php file-> scan its dictionary and expression-> parse file-> create computer code to execute (called Opcode)-> finally execute Opcode- > response return

Each request PHP script will perform the above steps once. If the PHP source code does not change, then the Opcode will not change. Obviously, there is no need to regenerate the Opcode every time. Combined with the ubiquitous caching mechanism in Web, we can cache the Opcode. Wouldn't it be faster to access the cached Opcode directly in the future? the flow chart after enabling Opcode caching is as follows:

The goal of Opcode cache is to avoid repeated compilation and reduce CPU and memory overhead.

2. Opcache configuration

Under php.ini, add:

/ / load opcache (confirm that opcache extension has been installed) zend_extension=opcache.so// enable opcacheopcache.enable = 1max / OPcache shared memory storage size, unit MBopcache.memory_consumption=1024 / / 1G// PHP uses a kind of so-called string residency. The default is 4MBopcache.interned_strings_buffer=32//. This option is used to control the maximum number of PHP files that can be cached in memory. This option must be set large enough. Greater than the sum of all the PHP files in your project, opcache.max_accelerated_files=80000// sets the cache expiration time (in seconds). If it is 0, check opcache.revalidate_freq=3// every time. Literally, "allow faster shutdown" in opcache.fast_shutdown=1// CLI environment, PHP enables OPcacheopcache.enable_cli=1HugePage

1. HugePage principle

By enabling this feature, PHP7 will "move" its own TEXT segment (executor) to Huagepage, and in previous tests, we can steadily see a 2%-3% increase in QPS on Wordpress.

As for what Hugepage is, to put it simply, the default memory is paged by 4KB, and the virtual address and memory address need to be translated, and this translation needs to look up the table. CPU has built-in TLB (Translation Lookaside Buffer) in order to speed up the process of looking up the table. Obviously, if the virtual page is smaller, there will be more entries in the table, and the size of TLB is limited. The more entries, the higher the Cache Miss of TLB. So if we can enable large memory pages, we can indirectly reduce this TLB Cache Miss. As for the detailed introduction, I will not repeat it as soon as Google is searched. Here we will mainly explain how to enable this new feature, which will lead to a significant performance improvement.

2. HugePage configuration

$sudo sysctl vm.nr_hugepages=512 / / Don't be as big as possible, it will take up memory

Allocate 512 reserved large pages of memory:

Cat / proc/meminfo | grep HugeAnonHugePages: 106496 kBHugePages_Total: 512HugePages_Free: 504HugePages_Rsvd: 27HugePages_Surp: 0Hugepagesize: 2048 kB

Then add the following to php.ini:

Opcache.huge_code_pages=1Opcache file cache

1. Opcache file cache introduction

Using opcache to save compiled php files as files to achieve php source code protection and script acceleration will significantly improve performance.

2. Opcache file cache configuration

Add the following to php.ini:

Opcache.file_cache=/tmp

In this way, PHP will Cache some Opcode binary export files under the / tmp directory, which can exist across the PHP life cycle.

Restart php-fpm parent test after configuration

System: centOs 7

Php version: 7.4

Nginx

Laravel: 8.5

Before optimization

Cpu:95%-96%

Memory: 2G/16G

10 minutes 4W concurrency

Failure rate: 24%

Aggregate report

Processing transactions per second

! [PHP7 performance Optimization]

! [PHP7 performance optimization notes]

After optimization

Cpu:20%-40%

Memory: 5.8G/16G (here I HugePage set 2048)

10 minutes 4W concurrency

Failure rate: 0%

The first pressure test

Aggregate report

Processing transactions per second

The second pressure test

Aggregate report

Processing transactions per second

The above is about what PHP7 performance optimization has, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report