Table of Contents
Round 3: Turn on swoole" >Round 3: Turn on swoole
Home PHP Framework Laravel How to make your Laravel return a 'hello world!' in 15 milliseconds

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

Mar 25, 2021 pm 05:19 PM
laravel

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

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

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
Copy after login

Let’s take a look at the response time:

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

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 opcacheExtension. 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
Copy after login

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
Copy after login

ok, the opcache extension has been enabled Okay, let’s take a look at the response time of Hello world!:

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

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

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-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

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-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_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. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// .env SWOOLE_HTTP_HOST=0.0.0.0 SWOOLE_HTTP_PORT=2020</pre><div class="contentsignin">Copy after login</div></div> The basic configuration modification is completed, we start the

http

application of laravel-swoole: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">root@Aliyun-ECS / # php artisan swoole:http start Starting swoole http server...Swoole http server started: &lt;http:&gt;&lt;/http:&gt;</pre><div class="contentsignin">Copy after login</div></div>At this time we visit

2020

Port, you can test the application expanded using swoole. 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 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:

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

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 using phpinfo() on the page. Here is a quote from @Hesunfly's original words:
"Some distributions do share the configurations of cli and fpm, such as for mac The php installed by brew only has one php.ini. But when I install it under centos and ubuntu, I usually distinguish between cli and fpm."
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Comparison of the latest versions of Laravel and CodeIgniter Comparison of the latest versions of Laravel and CodeIgniter Jun 05, 2024 pm 05:29 PM

The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

How do the data processing capabilities in Laravel and CodeIgniter compare? How do the data processing capabilities in Laravel and CodeIgniter compare? Jun 01, 2024 pm 01:34 PM

Compare the data processing capabilities of Laravel and CodeIgniter: ORM: Laravel uses EloquentORM, which provides class-object relational mapping, while CodeIgniter uses ActiveRecord to represent the database model as a subclass of PHP classes. Query builder: Laravel has a flexible chained query API, while CodeIgniter’s query builder is simpler and array-based. Data validation: Laravel provides a Validator class that supports custom validation rules, while CodeIgniter has less built-in validation functions and requires manual coding of custom rules. Practical case: User registration example shows Lar

Which one is more beginner-friendly, Laravel or CodeIgniter? Which one is more beginner-friendly, Laravel or CodeIgniter? Jun 05, 2024 pm 07:50 PM

For beginners, CodeIgniter has a gentler learning curve and fewer features, but covers basic needs. Laravel offers a wider feature set but has a slightly steeper learning curve. In terms of performance, both Laravel and CodeIgniter perform well. Laravel has more extensive documentation and active community support, while CodeIgniter is simpler, lightweight, and has strong security features. In the practical case of building a blogging application, Laravel's EloquentORM simplifies data manipulation, while CodeIgniter requires more manual configuration.

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Laravel vs CodeIgniter: Which framework is better for large projects? Laravel vs CodeIgniter: Which framework is better for large projects? Jun 04, 2024 am 09:09 AM

When choosing a framework for large projects, Laravel and CodeIgniter each have their own advantages. Laravel is designed for enterprise-level applications, offering modular design, dependency injection, and a powerful feature set. CodeIgniter is a lightweight framework more suitable for small to medium-sized projects, emphasizing speed and ease of use. For large projects with complex requirements and a large number of users, Laravel's power and scalability are more suitable. For simple projects or situations with limited resources, CodeIgniter's lightweight and rapid development capabilities are more ideal.

Laravel vs CodeIgniter: Which framework is better for small projects? Laravel vs CodeIgniter: Which framework is better for small projects? Jun 04, 2024 pm 05:29 PM

For small projects, Laravel is suitable for larger projects that require strong functionality and security. CodeIgniter is suitable for very small projects that require lightweight and ease of use.

Which is the better template engine, Laravel or CodeIgniter? Which is the better template engine, Laravel or CodeIgniter? Jun 03, 2024 am 11:30 AM

Comparing Laravel's Blade and CodeIgniter's Twig template engine, choose based on project needs and personal preferences: Blade is based on MVC syntax, which encourages good code organization and template inheritance. Twig is a third-party library that provides flexible syntax, powerful filters, extended support, and security sandboxing.

Laravel - Artisan Console Laravel - Artisan Console Aug 27, 2024 am 10:51 AM

Laravel - Artisan Console - Laravel framework provides three primary tools for interaction through command-line namely: Artisan, Ticker and REPL. This chapter explains about Artisan in detail.

See all articles