The following tutorial column from laravel will 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 basic Hello world!
string is the most basic request process among all projects based on Laravel
. In addition, any http
request 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 a Hello world!
string. Therefore, by comparing the most basic Hello world
request response time, we can see the impact of different optimizations on the Laravel
framework 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
It can be seen that after installing PHP
, under the default configuration, a Hello world!
On average it takes about 140ms
. Next, let’s get started!
Round 1: Laravel cache
Laravel
provides us with a very convenient artisan
command to enable the cache function. Effectively reduces the number of How to make your Laravel return a hello world! in 15 milliseconds reads. The php artisan optimize
command includes the php artisan config:cache
and php artisan route:cache
commands, but one more Files
will 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
Let’s take a look at the response time:
It can be seen that Laravel
’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 on opcache
Extension. Since I installed php8 using the remi
source, it will be easier for me to install the opcache
extension here. For installation of other versions, please Google it yourself.
root@Aliyun-ECS / # yum install php80-php-opcache
After waiting for the installation to complete, we restart php
, 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
ok, the opcache
extension has been enabled Okay, let’s take a look at the response time of Hello world!
:
30ms, which improves the response time by nearly
5 times. Note that the first request will be slower because opcache is 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
swoole Everyone 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, giving
PHP wings. Install the
swoole extension below, please Google for other versions.
root@Aliyun-ECS / # yum install php80-php-pecl-swooleAs 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 => OffThe extension has been enabled, but it cannot be tested yet. Because
swoole is an extension in
cli mode,
php-fpm cannot be used. So we need to implement a
http application in
cli mode. But in fact, we don’t need to manually write the
http application 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 the
laravel-swoole software package and start a http service. It's very simple.
// 引入软件包 root@Aliyun-ECS / # composer require swooletw/laravel-swoole // 发布配置文件 root@Aliyun-ECS / # php artisan vendor:publish --tag=laravel-swooleAfter performing the above two steps, you can find the two configuration How to make your Laravel return a hello world! in 15 millisecondss
swoole_http and
swoole_websocket in the
config directory of the project. A basic
Hello world! test, no need to modify the default configuration, we only add
SWOOLE_HTTP_HOST=0.0.0.0 and ## in the
.env How to make your Laravel return a hello world! in 15 milliseconds of the project #SWOOLE_HTTP_PORT=2020
, which means to start a http
listening program on the 2020
port. 0.0.0.0
means any IP can be accessed remotely. <pre class="brush:php;toolbar:false">// .env
SWOOLE_HTTP_HOST=0.0.0.0
SWOOLE_HTTP_PORT=2020</pre>
The basic configuration modification is completed, we start the
application of laravel-swoole
: <pre class="brush:php;toolbar:false">root@Aliyun-ECS / # php artisan swoole:http start
Starting swoole http server...Swoole http server started: <http:></http:></pre>
At this time we visit
Port, you can test the application expanded using swoole
. Let’s look at the response time of the request:
Good guy! Directly do it within
. The first time here takes a long time because opcache
is turned on and the cache will be written. But the opcache
write cache here is much faster than the one in Round 2
that only opens the opache
extension. This is all the result of swoole
. <h1>
<span class="header-link octicon octicon-link"></span>Conclusion</h1>
<p>I tested it again, only enabling the <code>swoole
extension without enabling the opcache
, 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 having swoole
, opcache
will be useless? I have to ask the big guys for advice on this. Here is a simple comparison:
Through practical comparison, it is found that enabling the opcache
and swoole
extensions 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 @Hesunfly for the answer. Sometimes the extended information viewed using the
php -i
mode 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 ofcli
andfpm
, 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 betweencli
andfpm
."
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!

Laravel performs strongly in back-end development, simplifying database operations through EloquentORM, controllers and service classes handle business logic, and providing queues, events and other functions. 1) EloquentORM maps database tables through the model to simplify query. 2) Business logic is processed in controllers and service classes to improve modularity and maintainability. 3) Other functions such as queue systems help to handle complex needs.

The Laravel development project was chosen because of its flexibility and power to suit the needs of different sizes and complexities. Laravel provides routing system, EloquentORM, Artisan command line and other functions, supporting the development of from simple blogs to complex enterprise-level systems.

The comparison between Laravel and Python in the development environment and ecosystem is as follows: 1. The development environment of Laravel is simple, only PHP and Composer are required. It provides a rich range of extension packages such as LaravelForge, but the extension package maintenance may not be timely. 2. The development environment of Python is also simple, only Python and pip are required. The ecosystem is huge and covers multiple fields, but version and dependency management may be complex.

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

Laravel's popularity includes its simplified development process, providing a pleasant development environment, and rich features. 1) It absorbs the design philosophy of RubyonRails, combining the flexibility of PHP. 2) Provide tools such as EloquentORM, Blade template engine, etc. to improve development efficiency. 3) Its MVC architecture and dependency injection mechanism make the code more modular and testable. 4) Provides powerful debugging tools and performance optimization methods such as caching systems and best practices.

Both Django and Laravel are full-stack frameworks. Django is suitable for Python developers and complex business logic, while Laravel is suitable for PHP developers and elegant syntax. 1.Django is based on Python and follows the "battery-complete" philosophy, suitable for rapid development and high concurrency. 2.Laravel is based on PHP, emphasizing the developer experience, and is suitable for small to medium-sized projects.

PHP and Laravel are not directly comparable, because Laravel is a PHP-based framework. 1.PHP is suitable for small projects or rapid prototyping because it is simple and direct. 2. Laravel is suitable for large projects or efficient development because it provides rich functions and tools, but has a steep learning curve and may not be as good as pure PHP.

LaravelisabackendframeworkbuiltonPHP,designedforwebapplicationdevelopment.Itfocusesonserver-sidelogic,databasemanagement,andapplicationstructure,andcanbeintegratedwithfrontendtechnologieslikeVue.jsorReactforfull-stackdevelopment.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor