Laravel application performance tuning experience

This is an afterthought. After going through many pitfalls in the tuning process, we finally improved and implemented the preliminary performance test plan, and summarized some practical skills in the Laravel development process through real test data.

0x00 origin

Recently, a colleague reported that the response of the application written by Laravel is a bit slow, and more than 20 concurrent CPUs are full... In order to solve the slow problem, even some interfaces are written in nodejs.

And my first reaction was how could a popular framework be so bad? There must be something wrong with using it. In order to find out, I started this tour of Laravel application performance tuning.

0x01 Tuning Tips

The optimization techniques used in this performance testing solution are mainly based on the Laravel framework itself and the tools it provides.

  1. Turn off application debug app.debug=false
  2. cache configuration information php artisan config:cache
  3. cache routing information php artisan router:cache
  4. Class map loading optimization php artisan optimize
  5. Autoload optimization composer dumpautoload
  6. Load only necessary middleware as needed
  7. Use just-in-time compiler (JIT), such as: HHVM, OPcache
  8. Using PHP 7.x

In addition to the above optimization techniques, there are many coding practices that can improve the performance of Laravel applications, which will not be explained in this article for the time being. (You can also follow my follow-up articles)

1. Turn off application debug

Open the .env file in the application root directory and set debug to false.

APP_DEBUG=false

2. Cache configuration information

php artisan config:cache

Running the above command can merge all configuration information in the config folder into one  bootstrap/cache/config.php file, reducing the number of files loaded at runtime.

php artisan config:clear

Running the above command can clear the cache of configuration information, that is, delete the  bootstrap/cache/config.php file

3. Cache routing information

php artisan route:cache

Running the above command will generate the file  bootstrap/cache/routes.php. Route caching can effectively improve the registration efficiency of routers, and the effect is more obvious in large-scale applications.

php artisan route:clear

Running the above command will clear the route cache, that is, delete the  bootstrap/cache/routes.php file.

4. Class map loading optimization

php artisan optimize --force

运行以上命令能够把常用加载的类合并到一个文件中,通过减少文件的加载来提高运行效率。这个命令会生成 bootstrap/cache/compiled.php 和 bootstrap/cache/services.json 两个文件。

通过修改 config/compile.php 文件可以添加要合并的类。

在生产环境中不需要指定 --force 参数文件也可以自动生成。

php artisan clear-compiled

运行以上命令会清除类映射加载优化,也就是删除 bootstrap/cache/compiled.php 和 bootstrap/cache/services.json 两个文件。

5. 自动加载优化

composer dumpautoload -o

Laravel 应用程序是使用 composer 来构建的。这个命令会把 PSR-0 和 PSR-4 转换为一个类映射表来提高类的加载速度。

注意:php artisan optimize --force 命令里已经做了这个操作。

6. 根据需要只加载必要的中间件

Laravel 应用程序内置了并开启了很多的中间件。每一个 Laravel 的请求都会加载相关的中间件、产生各种数据。在 app/Http/Kernel.php 中注释掉不需要的中间件(如 session 支持)可以极大的提升性能。

7. 使用即时编译器

HHVM 和 OPcache 都能轻轻松松的让你的应用程序在不用做任何修改的情况下,直接提高 50% 或者更高的性能。

8. 使用 PHP 7.x

只能说 PHP 7.x 比起之前的版本在性能上有了极大的提升。

嗯,限于你的真实企业环境,这个也许很长时间内改变不了,算我没说。

0x02 测试方案

我们使用简单的 Apache ab 命令仅对应用入口文件进行测试,并记录和分析数据。

  1. 仅对应用的入口文件 index.php 进行测试,访问 “/” 或者 “/index.php” 返回框架的欢迎页面。更全面的性能测试需要针对应用的更多接口进行测试。
  2. 使用 Apache ab 命令。ab -t 10 -c 10 {url}。该命令表示对 url 同时发起 10 个请求,并持续 10 秒钟。命令中具体的参数设置需要根据要测试的服务器性能进行选择。
  3. 为了避免机器波动导致的数据错误,每种测试条件会执行多次 ab 命令,并记录命令执行结果,重点关注每秒处理的请求数及请求响应时间,分析并剔除异常值。
  4. 每次对测试条件进行了调整,需要在浏览器上对欢迎页进行访问,确保没有因为测试条件修改而访问出错。如果页面访问出错会导致测试结果错误。

服务器环境说明

所有脱离具体环境的测试数据都没有意义,并且只有在相近的条件下才可以进行比较。

  1. 这套环境运行在 Mac 上,内存 8G,处理器 2.8GHz,SSD 硬盘。
  2. 测试服务器是使用 Homestead 搭建的。虚拟机配置为单核 CPU、2G 内存。
  3. 服务器 PHP 版本为 7.1,未特殊说明,则标识开启了 OPcache。
  4. 测试的 Laravel 应用程序采用 5.2 版本编写。app\Http\routes.php 中定义了 85 个路由。
  5. 测试过程中除了虚拟机、终端及固定的浏览器窗口外,没有会影响机器的程序运行。

以上的数据,大家在自己进行测试时可以参考。

0x03 测试过程及数据

1. 未做任何优化

1.1 操作

  • 按照以下检查项执行相应的操作。
  • 运行 ab -t 10 -c 10 http://myurl.com/index.php

基础检查项

  • .env 文件中 APP_DEBUG=true
  • 不存在 bootstrap/cache/config.php
  • 不存在 bootstrap/cache/routes.php
  • 不存在 bootstrap/cache/compiled.php 和 bootstrap/cache/services.json
  • app/Http/Kernel.php 中开启了大部分的中间件
  • 浏览器访问 Laravel 应用程序欢迎页确保正常访问

1.2 数据记录

2. 关闭应用debug

2.1 操作

  • 在步骤 1 基础上修改 .env 文件中 APP_DEBUG=false
  • 浏览器访问 Laravel 应用程序欢迎页确保正常访问。
  • 运行 ab -t 10 -c 10 http://myurl.com/index.php

2.2 数据记录

2.3 对比结果

与步骤 1 结果比较发现:关闭应用 debug 之后,每秒处理请求数从 26-34 上升到 33-35,请求响应时间从 大部分 300ms 以上下降到 290ms 左右,效果不太明显,但确实有一定的提升。

注意:这部分与应用中的日志等使用情况有比较大的关联。

3. 开启缓存配置信息

3.1 操作

  • 在步骤 2 基础上,运行 php artisan config:cache,确认生成 bootstrap/cache/config.php
  • 浏览器访问 Laravel 应用程序欢迎页确保正常访问。
  • 运行 ab -t 10 -c 10 http://myurl.com/index.php

3.2 数据记录

3.3 对比结果

与步骤 2 结果比较发现:开启配置信息缓存之后,每秒处理请求数从 33-35 上升到 36-38,请求响应时间从 290ms 左右下降到 260ms 左右,效果不太明显,但确实有一定的提升。

4. 开启缓存路由信息

4.1 操作

  • 在步骤 3 基础上,运行 php artisan route:cache,确认生成 bootstrap/cache/routes.php
  • 浏览器访问 Laravel 应用程序欢迎页确保正常访问。
  • 运行 ab -t 10 -c 10 http://myurl.com/index.php

4.2 数据记录

4.3 对比结果

与步骤 3 结果比较发现:开启路由信息缓存之后,每秒处理请求数从 36-38 上升到 60 左右,请求响应时间从 260ms 下降到 160ms 左右,效果显著,从 TPS 看,提升了 70%。

5. 删除不必要的中间件

5.1 操作

  • 在步骤 4 基础上,注释掉不必要的中间件代码。
  • 浏览器访问 Laravel 应用程序欢迎页确保正常访问。
  • 运行 ab -t 10 -c 10 http://myurl.com/index.php

注意:这次测试中我注释掉了所有的中间件。实际情况中应该尽量只留下必要的中间件。

5.2 数据记录

5.3 对比结果

与步骤 4 结果比较发现:删除了不必要的中间件之后,每秒处理请求数从 60 左右上升到 90 左右,请求响应时间从 160ms 下降到 110ms 左右,效果非常明显,从 TPS 看,提升了 50%。

6. 开启类映射加载优化

6.1 操作

  • 在步骤 5 基础上,运行 php artisan optimize --force,确认生成 bootstrap/cache/compiled.php 和 bootstrap/cache/services.json
  • 浏览器访问 Laravel 应用程序欢迎页确保正常访问。
  • 运行 ab -t 10 -c 10 http://myurl.com/index.php

6.2 数据记录

6.3 对比结果

与步骤 5 结果比较发现:做了类映射加载优化之后,每秒处理请求数从 90 上升到 110,请求响应时间从 110ms 下降到 100ms 以下,效果还是比较明显的。

7. 关闭 OPcache

7.1 操作

  • 在步骤 6 基础上,关闭 PHP 的 OPcache,并重启服务器。通过 phpinfo() 的 Zend OPcache 确认 OPcache 已经关闭。
  • 浏览器访问 Laravel 应用程序欢迎页确保正常访问。
  • 运行 ab -t 10 -c 10 http://myurl.com/index.php

7.2 数据记录

7.3 对比结果

与步骤 6 结果比较发现:关闭 OPcache 之后,每秒处理请求数从 110 下降到 15,请求响应时间从 100ms 以下上升到 650ms 以上。开启与关闭 OPcache,数据上竟有几倍的差别。

此后,我重新开启了 PHP 的 OPcache,数据恢复到步骤 6 水平。

0x04 踩过的坑

1. [LogicException] Unable to prepare route [/] for serialization. Uses Closure.

在运行 php artisan route:cache 命令时报这个错误。

原因:路由文件中处理“/”时使用了闭包的方式。要运行该命令,路由的具体实现不能使用闭包方式。

修改方案:将路由的具体实现放到控制器中来实现。

2. [Exception] Serialization of 'Closure' is not allowed.

在运行 php artisan route:cache 命令时报这个错误。

原因:路由文件中定义了重复的路由。

修改方案:排查路由文件中的重复路由并修改。尤其要注意 resource 方法很可能导致与其方法重复。

3. [RuntimeException] Invalid filename provided.

在运行 php artisan optimize --force 命名时报这个错误。

原因:在加载需要编译的类时没有找到相应的文件。5.2 版本的 vendor/laravel/framework/src/Illuminate/Foundation/Console/Optimize/config.php 中定义了要编译的文件路径,但不知道为什么 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/ActiveRecords.php 没有找到,所以报了这个错误。

修改方案:暂时注释掉了以上 config.php 中的 ../ActiveRecords.php 一行。

4. InvalidArgumentException in FileViewFinder.php line 137: View [welcome] not found.

在运行 php artisan config:cache 之后,浏览器上访问 Laravel 应用程序欢迎页报这个错误。

原因:Laravel 应用程序服务器是通过 Homestead 在虚拟机上搭建的。而这个命令我是在虚拟机之外运行的,导致生成的 config.php 中的路径是本机路径,而不是虚拟机上的路径。所以无法找到视图文件。

修改方案:ssh 到虚拟机内部运行该命令。

0x05 实践技巧

坑也踩了,测试也做过了。这里针对这次经历做个实践技巧的简单总结。

1. 有效的 Laravel 应用程序优化技巧

  1. 关闭应用debug app.debug=false
  2. 缓存配置信息 php artisan config:cache
  3. 缓存路由信息 php artisan router:cache
  4. 类映射加载优化 php artisan optimize(包含自动加载优化 composer dumpautoload
  5. 根据需要只加载必要的中间件
  6. 使用即时编译器(JIT),如:HHVM、OPcache

2. 编写代码时注意事项

  1. 路由的具体实现放到控制器中。
  2. 不定义重复的路由,尤其注意 resouce 方法。
  3. 弄清各中间件的作用,删除不必要的中间件引用。

0x06 下一步

以上的调优技巧及编码注意事项主要针对框架本身,在真正的业务逻辑编码中有很多具体的优化技巧,在此没有讨论。

后续的优化重点将会放在具体编码实践上:

  1. 使用 Memcached 来存储会话 config/session.php
  2. 使用专业的缓存驱动器
  3. 数据库请求优化
  4. 为数据集书写缓存逻辑
  5. 前端资源合并 Elixir


转自:https://segmentfault.com/a/1190000011569012

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325759137&siteId=291194637