Nginx + PHP导致的性能问题
  pW73IErmAOtD 2023年11月02日 50 0

一. 准备

1. 在data目录下,使用rz上传项目zip包

Nginx + PHP导致的性能问题_nginx

2. 解压

Nginx + PHP导致的性能问题_nginx_02

3. 进入到解压后的目录nginx-php-cpu,查看Makefile操作步骤

Nginx + PHP导致的性能问题_docker_03

4. 配置docker镜像加速

我们使用DaoCloud进行加速,具体DaoCloud怎么获取镜像源地址,请看这篇Docker从理论到实践(三)------配置你的DaoCloud的Docker镜像源加速器

接着修改docker.service,记得字母不要写错,否则docker会重启失败

[root@localhost nginx-php-cpu]# vim /usr/lib/systemd/system/docker.service 

ExecStart=/usr/bin/dockerd --registry-mirror=http://f1361db2.m.daocloud.io
ADD_REGISTRY='--registry-mirror=http://2e28129a.m.daocloud.io'

结束之后记得要重启下docker

systemctl daemon-reload
systemctl restart docker

5. 先查看Makefile文件,根据Makefile去build和run

cat Makefile

Nginx + PHP导致的性能问题_docker_04

6. build操作

docker build -t pertest/nginx:cpu -f Dockerfile.nginx .    #去下载Nginx的镜像包
docker build -t pertest/php-fpm:cpu -f Dockerfile.php-fpm . #下载PHP

7. 查看镜像包信息

docker images

Nginx + PHP导致的性能问题_php_05

8. 运行

运行nginx,命名为nginx,端口进行转发,Nginx默认的端口是80,可以把虚拟端口映射到一个实际的端口18080,可以用这个端口进行访问,

docker run --name nginx -p 18080:80 -id pertest/nginx:cpu    #启动Nginx,端口从镜像的80映射到本地的18080端口

Nginx + PHP导致的性能问题_nginx_06

运行PHP

docker run --name phpfpm -itd --network container:nginx pertest/php-fpm:cpu    #启动PHP

Nginx + PHP导致的性能问题_php_07

使用docker ps查看启动容器

Nginx + PHP导致的性能问题_docker_08

9. 验证

在本机输入192.168.0.110:18080,如果能看到信息,说明已经启动成功了

Nginx + PHP导致的性能问题_php_09

 

二. 编写脚本

1. 在jmeter中新建阶梯型线程组,添加HTTP请求,启动20个线程,最后查看TPS和响应时间

Nginx + PHP导致的性能问题_docker_10

Nginx + PHP导致的性能问题_php_11

运行后,可以看到平均响应时间过长,响应时间持续升高,TPS比较低

Nginx + PHP导致的性能问题_docker_12

Nginx + PHP导致的性能问题_nginx_13

Nginx + PHP导致的性能问题_php_14

三. 定位

1. 使用top看一下CPU使用率,可以看到%user已经快跑满了,且都是PHP进程导致的

Nginx + PHP导致的性能问题_docker_15

2. 使用perf top查看,如果看到的都是十六进制的数据,是因为perf的版本有点低

perf top

Nginx + PHP导致的性能问题_docker_16

3. 使用perf record等待30s后停止,生成报告

perf record -g -p 38577   #-g 开启函数的调用跟踪,-p pid

Nginx + PHP导致的性能问题_nginx_17

 

================================centos7开始================================

4. 将报告拷贝到docker中的PHP的/tmp目录下

Nginx + PHP导致的性能问题_php_18

docker cp perf.data phpfpm:/tmp

Nginx + PHP导致的性能问题_php_19

5. 进入docker容器里面

docker exec -it phpfpm bash

Nginx + PHP导致的性能问题_nginx_20

6. 升级Linux-perf(centos8上是4.9以上不用升级)

apt-get update && apt-get install -y linux-perf linux-tools procps

7. 打开报告:通过方向键切换到php-fpm,再回车展开php-fpm的调用关系,调用关系最终找到了sqrt这个函数

perf_4.9 report

Nginx + PHP导致的性能问题_php_21

================================centos7结束================================

 

6. 查看PHP源码看是不是有这个函数

grep sqrt -r pertest/    #-r 对目录下的文件进程递归查找

Nginx + PHP导致的性能问题_nginx_22

然后通过vim pertest/index.php定位到代码中的sqrt,可以看到是由于该代码导致了性能问题

Nginx + PHP导致的性能问题_php_23

四. 使用修复后的代码fixe_index.php运行

Nginx + PHP导致的性能问题_docker_24

Nginx + PHP导致的性能问题_php_25

Nginx + PHP导致的性能问题_nginx_26

可以看到,用户态的CPU使用率不是那么高了

 


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
pW73IErmAOtD