Yii2 生产环境性能优化
最近在使用 wrk 进行网站压力测试,文章 里面测试本站时 QPS 只有 15 左右,而刚好是周末,记录一下优化的过程。
首先使用缓存,本站选择了 Redis,Yii2 官方有扩展,安装了就行。
MySQL 方面暂时也没什么好优化了,毕竟是小网站,索引也没加,但是减少连接数也是必要的,另外 Yii2 Debug Toolbar 挺好用的,列出了所有 MySQL 连接并能按耗时排序,代码上修改了一些查询逻辑,基本上数据都存 Redis 了。
本文主要是记录了 Yii2 生产环境上的一些优化设置,官方文档也有,详细的可以去看文档,这里只是做个记录。
PHP 环境设置
-
使用最新版本,目前 PHP7.2
-
启用 Opcache,测试了以下,开启之后 QPS 能达到 50 左右
使用生产环境入口文件,没什么好说的。
使用缓存 Redis,目前 markdown 的渲染还是前台,没用缓存,有空再加上。
开启数据库 Schema 缓存:
return [
// ...
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=mydatabase',
'username' => 'root',
'password' => '',
// Enabling Schema Caching
// Just empty `/app/runtime/cache/` folder or use `php yii cache/flush-schema` to delete cache
'enableSchemaCache' => true,
// Duration of schema cache.
'schemaCacheDuration' => 3600,
// Name of the cache component used to store schema information
'schemaCache' => 'cache',
],
],
];
前端资源文件压缩,资源不多,没做处理。
优化 Session 存储,可以保存在数据库或缓存 (Redis) 中,没做处理。
优化数据库,比如加索引等,没做处理。
数据查询结果返回数组。
优化 Composer Autoloader,避免加载太多第三方类,执行以下命令:
composer dumpautoload -o
后台处理数据,比如消息队列、定时任务等,没做处理。
代码性能分析,Yii2 Debug Toolbar 就挺好用的。
这些都是简单的优化,基本上都可以做到。
对于这次优化,感觉带来提升比较大的还是缓存,比如 Opcache、Redis,而对于一些服务器参数、MySQL 参数、php-fpm.conf 参数等倒是没怎么调整,都是默认设置,要调这些,还是需要花点时间在服务器上测试的,暂时还没去搞。
下面来看下 wrk 测试的一些数据。
优化前:
# wrk 请求
docker run --rm --net=host williamyeh/wrk -c 100 -t 3 -d 2m --latency --timeout 30s https://imhugh.com/
Running 2m test @ https://imhugh.com/
3 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 4.20s 528.76ms 6.50s 83.56%
Req/Sec 8.35 5.29 40.00 68.61%
Latency Distribution
50% 4.19s
75% 4.42s
90% 4.83s
99% 5.31s
2135 requests in 2.00m, 11.12MB read
Requests/sec: 17.78
Transfer/sec: 94.83KB
# top 服务器状态
top - 20:27:28 up 5 days, 6:53, 1 user, load average: 2.03, 1.37, 0.73
Tasks: 121 total, 6 running, 115 sleeping, 0 stopped, 0 zombie
%Cpu(s): 90.3 us, 9.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.7 si, 0.0 st
KiB Mem : 1012524 total, 172920 free, 349592 used, 490012 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 448728 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27204 www-data 20 0 204792 21508 10556 R 17.9 2.1 0:26.65 php-fpm
27205 www-data 20 0 204752 21284 10492 R 17.9 2.1 0:27.46 php-fpm
27196 www-data 20 0 204780 21484 10532 R 17.6 2.1 0:31.92 php-fpm
27213 www-data 20 0 204716 21188 10428 R 17.6 2.1 0:05.20 php-fpm
27214 www-data 20 0 204736 21184 10428 R 17.6 2.1 0:05.08 php-fpm
30827 999 20 0 1140588 204180 7944 S 9.0 20.2 7:45.73 mysqld
2399 root 20 0 593872 45908 22048 S 1.0 4.5 16:14.00 dockerd
5732 systemd+ 20 0 76728 14220 2624 S 1.0 1.4 1:47.70 nginx
优化后:
# wrk 请求
docker run --rm --net=host williamyeh/wrk -c 100 -t 3 -d 2m --latency --timeout 30s https://imhugh.com/
Running 2m test @ https://imhugh.com/
3 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 656.52ms 351.49ms 5.81s 89.87%
Req/Sec 57.62 24.40 141.00 68.77%
Latency Distribution
50% 526.43ms
75% 684.24ms
90% 1.01s
99% 1.88s
18576 requests in 2.00m, 96.99MB read
Non-2xx or 3xx responses: 32
Requests/sec: 154.75
Transfer/sec: 827.43KB
# top 服务器状态
top - 19:26:10 up 7 days, 5:52, 1 user, load average: 3.41, 0.98, 0.33
Tasks: 123 total, 8 running, 115 sleeping, 0 stopped, 0 zombie
%Cpu(s): 73.0 us, 17.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 9.7 si, 0.0 st
KiB Mem : 1012524 total, 118716 free, 338148 used, 555660 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 436692 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11959 www-data 20 0 337168 26148 18748 R 15.0 2.6 0:23.82 php-fpm
12328 www-data 20 0 337104 21092 13964 R 15.0 2.1 0:08.18 php-fpm
12329 www-data 20 0 337104 21092 13964 R 15.0 2.1 0:08.04 php-fpm
9333 www-data 20 0 337168 24288 16896 R 14.6 2.4 0:40.31 php-fpm
11960 www-data 20 0 337168 24300 16896 R 14.6 2.4 0:24.45 php-fpm
3664 999 20 0 45744 5888 2964 R 9.3 0.6 5:41.61 redis-server
4844 systemd+ 20 0 71532 6432 3092 S 8.3 0.6 2:11.66 nginx
2399 root 20 0 610264 48560 17532 S 7.3 4.8 23:05.59 dockerd
4031 root 20 0 315104 14052 12268 S 1.0 1.4 0:12.70 docker-containe
3686 999 20 0 1139548 209204 17052 S 0.3 20.7 1:25.73 mysqld
12330 shen 20 0 42104 3652 3048 R 0.3 0.4 0:00.04 top
可以看到,QPS 从 17 提升到了 154,99% 的请求时间也从 5 秒多提升到 2 秒内,而从 top 统计里面看出,CPU 使用也有所降低,看来优化还是有点作用的,感觉是缓存带来的提升,目前来说就这样吧。