Home> PHP Framework> Laravel> body text

How to make your Laravel return a 'hello world!' in 15 milliseconds

藏色散人
Release: 2021-03-26 08:59:25
forward
1970 people have browsed it

The following tutorial column fromlaravelwill introduce to you how to make your Laravel return a "hello world!" within 15 milliseconds. I hope it will be helpful to friends who need it. help!

First of all, I think that returning the most basicHello world!string is the most basic request process among all projects based onLaravel. In addition, anyhttprequest in the project will carry more time-consuming operations such as business logic and database queries, and the execution time of these logics is uncontrollable and non-comparable. In other words, any other request will not take less time than returning aHello world!string. Therefore, by comparing the most basicHello worldrequest response time, we can see the impact of different optimizations on theLaravelframework from startup to end of execution.

Recommended:The latest five Laravel video tutorials

Test parameters

Parameters Version
Server 1c processor, 1G memory, 1M bandwidth
PHP Version 8.0
Process Management PHP-FPM

Default configuration response time

让你的Laravel在 15 毫秒内返回一个

It can be seen that after installingPHP, under the default configuration, aHello world!On average it takes about140ms. Next, let’s get started!

Round 1: Laravel cache

Laravelprovides us with a very convenientartisancommand to enable the cache function. Effectively reduces the number of How to make your Laravel return a hello world! in 15 milliseconds reads. Thephp artisan optimizecommand includes thephp artisan config:cacheandphp artisan route:cachecommands, but one moreFileswill appear. cache. Execute the following 5 commands in sequence:

root@Aliyun-ECS / # php artisan optimize root@Aliyun-ECS / # php artisan config:cache root@Aliyun-ECS / # php artisan event:cache root@Aliyun-ECS / # php artisan route:cache root@Aliyun-ECS / # php artisan view:cache
Copy after login

Let’s take a look at the response time:

让你的Laravel在 15 毫秒内返回一个

It can be seen thatLaravel’s cache is Basic request, no noticeable impact.

Round 2: Turn on opcache

This time, I decided to use the method with the most obvious speed-up effect:Turn onopcacheExtension. Since I installedphp8using theremisource, it will be easier for me to install theopcacheextension here. For installation of other versions, please Google it yourself.

root@Aliyun-ECS / # yum install php80-php-opcache
Copy after login

After waiting for the installation to complete, we restartphp, and then check whether the extension has been installed:

root@Aliyun-ECS / # systemctl restart php80-php-fpm root@Aliyun-ECS / # php -i|grep opcache.enable opcache.enable => On => On opcache.enable_cli => On => On opcache.enable_How to make your Laravel return a hello world! in 15 milliseconds_override => Off => Off
Copy after login

ok, theopcacheextension has been enabled Okay, let’s take a look at the response time ofHello world!:

让你的Laravel在 15 毫秒内返回一个

##OHHHHHH! The effect is so obvious that it suddenly drops to within

30ms, which improves the response time by nearly5 times. Note that the first request will be slower becauseopcacheis writing the cache. After one access, the speed will skyrocket. Are you satisfied here? Look at the title of the article, we need to further increase our efforts!

Round 3: Turn on swoole

swooleEveryone knows that the module loads the application into memory in advance so that when processing the request , reducing the How to make your Laravel return a hello world! in 15 milliseconds reading and loading process, givingPHPwings. Install theswooleextension below, please Google for other versions.

root@Aliyun-ECS / # yum install php80-php-pecl-swoole
Copy after login
As usual, after installation, check whether the installation is successful:

root@Aliyun-ECS / # systemctl restart php80-php-fpm root@Aliyun-ECS / # php -i|grep swoole.enable swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off
Copy after login
The extension has been enabled, but it cannot be tested yet. Because

swooleis an extension inclimode,php-fpmcannot be used. So we need to implement ahttpapplication inclimode. But in fact, we don’t need to manually write thehttpapplication ourselves. There are big guys in the community who have already written it. As the saying goes, "Forefathers plant trees, and future generations enjoy the shade." We introduce thelaravel-swoolesoftware package and start ahttpservice. It's very simple.

// 引入软件包 root@Aliyun-ECS / # composer require swooletw/laravel-swoole // 发布配置文件 root@Aliyun-ECS / # php artisan vendor:publish --tag=laravel-swoole
Copy after login
After performing the above two steps, you can find the two configuration How to make your Laravel return a hello world! in 15 millisecondss

swoole_httpandswoole_websocketin theconfigdirectory of the project. A basicHello world!test, no need to modify the default configuration, we only addSWOOLE_HTTP_HOST=0.0.0.0and ## in the.envHow to make your Laravel return a hello world! in 15 milliseconds of the project #SWOOLE_HTTP_PORT=2020, which means to start ahttplistening program on the2020port.0.0.0.0means any IP can be accessed remotely.

// .env SWOOLE_HTTP_HOST=0.0.0.0 SWOOLE_HTTP_PORT=2020
Copy after login
The basic configuration modification is completed, we start the

http

application oflaravel-swoole:

root@Aliyun-ECS / # php artisan swoole:http start Starting swoole http server...Swoole http server started: <http:></http:>
Copy after login
At this time we visit

2020

Port, you can test the application expanded usingswoole. Let’s look at the response time of the request:

让你的Laravel在 15 毫秒内返回一个Good guy! Directly do it within

15ms

. The first time here takes a long time becauseopcacheis turned on and the cache will be written. But theopcachewrite cache here is much faster than the one inRound 2that only opens theopacheextension. This is all the result ofswoole.

Conclusion

I tested it again, only enabling the swooleextension without enabling theopcache, and found that the response time was the same as two The response time is the same for both extensions enabled. In other words, after havingswoole,opcachewill be useless? I have to ask the big guys for advice on this. Here is a simple comparison:

让你的Laravel在 15 毫秒内返回一个

Through practical comparison, it is found that enabling theopcacheandswooleextensions at the same time has the fastest response time. Fast.

Other questions

  • PHP-FPM process management, why is the master process created? Unscientific

Thanks

Thanks@Hesunflyfor the answer. Sometimes the extended information viewed using thephp -imode on the command line is inconsistent with the extended information viewed usingphpinfo()on the page. Here is a quote from@Hesunfly's original words:
"Some distributions do share the configurations ofcliandfpm, such as for mac The php installed by brew only has onephp.ini. But when I install it under centos and ubuntu, I usually distinguish betweencliandfpm."
How to make your Laravel return a hello world! in 15 milliseconds

The above is the detailed content of How to make your Laravel return a 'hello world!' in 15 milliseconds. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!