测试环境介绍
机器是一台基于 vmware 的虚拟机
Default
CPU: cat /proc/cpuinfo 看到为 8 核 CPU,
型号是 Intel(R) Xeon(R) CPU E5-2430 v2 @ 2.50GHz
MEM: free 命令看到内存大小为 16G
nginx 版本为 1.11.5, worker_processes 8
php 版本为 7.0.12, fpm 进程 100 个
1
2
3
4
5
CPU: cat /proc/cpuinfo 看到为 8 核 CPU,
型号是 Intel(R) Xeon(R) CPU E5-2430 v2 @ 2.50GHz
MEM: free 命令看到内存大小为 16G
nginx 版本为 1.11.5, worker_processes 8
php 版本为 7.0.12, fpm 进程 100 个
程序是yii2-app-basic , HelloController.php 文件内容如下:
PHP
namespace app\controllers;
class HelloController extends \yii\web\Controller
{
public function actionWorld()
{
return 'Hello, world!';
}
}
1
2
3
4
5
6
7
8
9
10
namespaceapp\controllers;
classHelloControllerextends\yii\web\Controller
{
publicfunctionactionWorld()
{
return'Hello, world!';
}
}
测试命令: ab -n10000 -c100 ‘http://yii2.app.com:8090/index.php?r=/hello/world’
测试结果:
Opcache
CPU占用
Requests per second(吞吐率)
未开启
99%
520
开启
99%
5480
用 strace 追踪 php-fpm 的系统调用(strace 会严重影响性能,追踪系统调用时用的测试命令是:ab -n1000 -c100 ‘http://yii2.app.com:8090/index.php?r=/hello/world’,总请求减少为之前的10%),结果如下:
Default
未开启 Opcache php-fpm的系统调用
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
29.99 0.512695 3 174053 fstat
18.60 0.318016 5 59611 munmap
13.98 0.238919 4 58059 3 open
13.56 0.231822 4 59235 mmap
9.70 0.165759 3 59559 close
4.83 0.082623 8 11010 4 lstat
3.46 0.059209 2 28014 read
1.14 0.019432 4 5102 100 stat
0.76 0.012930 13 1000 accept
0.66 0.011317 6 2000 chdir
0.50 0.008500 8 1005 write
0.50 0.008488 8 1001 getcwd
0.44 0.007527 8 1000 shutdown
0.41 0.006957 2 3000 setitimer
0.40 0.006795 7 1000 poll
0.37 0.006369 4 1610 rt_sigaction
0.31 0.005215 3 2000 times
0.19 0.003238 2 2000 recvfrom
0.11 0.001857 2 1001 rt_sigprocmask
开启 Opcache php-fpm的系统调用
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
26.92 0.041058 1 28015 read
15.73 0.023990 5 5160 100 stat
8.44 0.012872 6 2000 chdir
6.78 0.010336 3 3000 setitimer
6.04 0.009209 4 2132 fcntl
5.49 0.008373 8 1000 accept
5.29 0.008068 13 615 4 lstat
3.63 0.005544 3 1618 close
3.48 0.005308 3 2000 times
3.01 0.004586 5 1001 rt_sigprocmask
2.90 0.004427 4 1005 write
2.65 0.004042 4 1001 getcwd
2.39 0.003651 2 1610 rt_sigaction
2.29 0.003491 2 2000 recvfrom
1.93 0.002950 3 1000 shutdown
1.65 0.002516 3 1000 poll
0.81 0.001241 3 397 mmap
0.38 0.000582 6 100 clone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
未开启Opcachephp-fpm的系统调用
%timesecondsusecs/callcallserrorssyscall
--------------------------------------------------------------
29.990.5126953174053fstat
18.600.318016559611munmap
13.980.2389194580593open
13.560.231822459235mmap
9.700.165759359559close
4.830.0826238110104lstat
3.460.059209228014read
1.140.01943245102100stat
0.760.012930131000accept
0.660.01131762000chdir
0.500.00850081005write
0.500.00848881001getcwd
0.440.00752781000shutdown
0.410.00695723000setitimer
0.400.00679571000poll
0.370.00636941610rt_sigaction
0.310.00521532000times
0.190.00323822000recvfrom
0.110.00185721001rt_sigprocmask
开启Opcachephp-fpm的系统调用
%timesecondsusecs/callcallserrorssyscall
--------------------------------------------------------------
26.920.041058128015read
15.730.02399055160100stat
8.440.01287262000chdir
6.780.01033633000setitimer
6.040.00920942132fcntl
5.490.00837381000accept
5.290.008068136154lstat
3.630.00554431618close
3.480.00530832000times
3.010.00458651001rt_sigprocmask
2.900.00442741005write
2.650.00404241001getcwd
2.390.00365121610rt_sigaction
2.290.00349122000recvfrom
1.930.00295031000shutdown
1.650.00251631000poll
0.810.0012413397mmap
0.380.0005826100clone
测试结果分析:
Opcache 开启前后 cpu 使用率都达到了 100% 说明系统瓶颈在 cpu。开启 Opcache 后系统调用少了很多,特别是 fstat,mumap,open,mmap,开启后,这几个系统调用可以忽略不计。Opcache 省去了每次加载和解析 PHP 脚本的开销,一次加载解析后后续请求不用去读源码,因此少了这么多系统调用。
结论:提高PHP程序的性能,最重要也最有效的方法就是开启 Opcache。
其它: 最初 fpm 配置是监听端口,吞吐率在开启Opcache前只有340左右;开启Opcache后在900左右,cpu占有80%,无法达到100%;如果请求过多,则会出现超时错误。后来改 fpm 监听 unix sock,性能一下子上来了。Yii2 开启 debug 模式后,吞吐率为 1200 左右。