


PHP concurrency performance tuning practice (performance increased by 104%)
##Business background
Framework and corresponding Environment
- laravel5.7, mysql5.7, redis5, nginx1.15
- centos 7.5 bbr
- docker, docker-compose
- Alibaba Cloud 4C and 8G
php has enabled opcache, laravel has also run the optimize command for optimization, and composer has also run the dump-autoload command.
The first thing to declare is that there must be minor problems in the system environment ( It is impossible to improve such a large performance without problems), but these problems, if you do not use appropriate tools, may not be discovered in your lifetime.
This article focuses on how to discover these problems, and how to find them The idea.
We first find a suitable API or function in the system to amplify the problem.
This API was originally designed to do health checks for nginx load balancing. Use ab -n 100000 -c 1000 for stress testing and find that the qps can only reach 140 times per second.
We know that Laravel's performance is notoriously bad, but it is not to this extent. It seems that the api should not be so low. So I decided to find out.
public function getActivateStatus() { try { $result = \DB::select('select 1'); $key = 1; if ($result[0]->$key !== 1) { throw new \Exception("mysql 检查失败"); } } catch (\Exception $exception) { \Log::critical("数据库连接失败: {$exception->getMessage()}", $exception->getTrace()); return \response(null, 500); } try { Cache::getRedis()->connection()->exists("1"); } catch (\Exception $exception) { \Log::critical("缓存连接失败: {$exception->getMessage()}", $exception->getTrace()); return \response(null, 500); } return \response(null, 204); }
Problem manifestations and troubleshooting ideas
top
The top command found that the system CPU occupied 100% Among them, user mode accounts for 80% and kernel mode accounts for 20%. It seems that there is no big problem. There is one place that looks strange. The result of running the top command
# is that part of the php-fpm process is in Sleep state, but the CPU usage still reaches nearly 30%.
When a process is in Sleep state, it still occupies a lot of CPU. Don't doubt whether it is a problem with the process. Let's take a look at the man page of the Ttop command.
%CPU -- CPU usage The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.
roughly means that this occupation is the last time When the screen is refreshed, the process CPU is occupied. Since the top command collects information, Linux may forcefully schedule the process (for example, it is used to collect process information with top), so at this moment (the moment the screen is refreshed) a certain Some php-fpm processes are in sleep state, which is understandable, so it should not be a problem with php-fpm.
pidstatFirst select a php-fpm process, and then Use pidstat to check the detailed running status of the process
Nothing unusual was found during the process, and the running results are basically the same as the top command.Keep the stress test pressure and run vmstate to check. Except for the context switch (context switch) which is a bit high, I don't see many abnormalities. Since the docker, redis, and mysql we use are all running on the same machine, a CS of about 7,000 is still a reasonable range, but the IN (interruption) is a bit too high, reaching about 14,000. There must be something triggering it Interrupt.
We know that interrupts include hard interrupts and soft interrupts. Hard interrupts are interrupt signals sent by hardware such as network cards and mice, and the CPU immediately stops what it is doing. Process interrupt signals. Soft interrupts are issued by the operating system and are often used for forced scheduling of processes.
Both vmstat and pidstat are only new performance detection tools. We cannot see who issued the specific interrupt. . We read the system's interrupt information from the read-only file /proc/interrupts to get what exactly caused the increase in interrupts. Use the watch -d command to determine the most frequently changing interrupts.
watch -d cat /proc/interrupts
We found that Rescheduling interrupts change the fastest among them. This is the rescheduling interrupt (RES). This interrupt type means to wake up the idle CPU to schedule new tasks to run. This is the mechanism used by the scheduler to distribute tasks to different CPUs in multi-processor systems (SMP). It is also commonly called Inter-Processor Interrupts (IPI).
Combining the commands in vmstat, we can determine that one of the reasons for low qps is caused by too many processes competing for the CPU. We are not sure what it is yet, so further investigation is needed.
strace
strace can view system calls. We know that when using system calls, the system falls into kernel mode. This process will generate soft interrupts. By viewing the php-fpm system Call, verify our conjecture
果然, 发现大量的stat系统调用, 我们猜想, 是opcache在检查文件是否过期导致的. 我们通过修改opcache的配置, 让opcache更少的检查文件timestamp, 减少这种系统调用
opcache.validate_timestamps="60" opcache.revalidate_freq="0"
再次执行ab命令进行压测
果然qps直接涨到了205, 提升非常明显, 有接近 46% 的提升
perf
现在任然不满足这个性能, 希望在更多地方找到突破口. 通过
perf record -g perf report -g
看到系统的分析报告
我们看到, 好像这里面有太多tcp建立相关的系统调用(具体是不是我还不清楚, 请大神指正, 但是看到send, ip, tcp啥的我就怀疑可能是tcp/ip相关的问题).
我们怀疑两种情况
与mysql, redis重复大量的建立TCP连接, 消耗资源
大量请求带来的tcp连接
先说第一个, 经过检查, 发现数据库连接使用了php-fpm的连接池, 但是redis连接没有, redis用的predis, 这个是一个纯PHP实现, 性能不高, 换成了phpredis:
打开laravel的config/database.php文件, 修改redis的driver为phpredis, 确保本机已安装php的redis扩展. 另外由于Laravel自己封装了一个Redis门面, 而恰好redis扩展带来的对象名也叫Redis. 所以需要修改Laravel的Redis门面为其他名字, 如RedisL5.
再次进行压测
达到了喜人的286qps, 虽然和其他主打高性能的框架或者原生php比, 还有很高的提升空间(比如Swoole), 但是最终达到了104%的提升, 还是很有意义的
总结
我们通过top, 发现系统CPU占用高, 且发现确实是php-fpm进程占用了CPU资源, 判断系统瓶颈来自于PHP.
接着我们通过pidstat, vmstat发现压测过程中, 出现了大量的系统中断, 并通过 watch -d cat /proc/interrupts 发现主要的中断来自于重调度中断(RES)
通过strace查看具体的系统调用, 发现大量的系统调用来自于stat, 猜测可能是opcache频繁的检查时间戳, 判断文件修改. 通过修改配置项, 达到了46%的性能提升
最后再通过perf, 查看函数调用栈, 分析得到, 可能是大量的与redis的TCP连接带来不必要的资源消耗. 通过安装redis扩展, 以及使用phpredis来驱动Laravel的redis缓存, 提升性能, 达到了又一次近50%的性能提升.
最终我们完成了我们的性能提升104%的目标
推荐教程:网站高并发架设基础教程
The above is the detailed content of PHP concurrency performance tuning practice (performance increased by 104%). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
