How to speed up laravel through swoole

不言
Release: 2023-04-03 08:06:01
Original
2937 people have browsed it

This article mainly introduces how to use swoole to accelerate laravel. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

Let’s review it again, which will cause PHP to be slow. Among these factors, the characteristics of the parsing language can be said to be the culprit. In addition, all files loaded during the request are released after each request, so it appears to be slower.

Later we got opcache. If we use this, the request time will be shortened to about half of the original, but we will find that it still seems to take a lot of time.

Why is this? The reason is that opcache only saves the time of parsing the file. When actually running, we still need to run the same code again. For details, we can see the following figure:

Let's assume that if our code is run once and is not released, wouldn't it be able to save the repeated time the next time it is run? Indeed.

Let’s start with the topic:

1. Install laravel-swoole extension

composer require swooletw/laravel-swoole
Copy after login

2. The swoole extension is of course essential

pecl install swoole
Copy after login

Need to be added to php.ini after installing the swoole extension, no need to go into details

3. Add it to the service provider array in config/app.php The service provider:

SwooleTW\Http\LaravelServiceProvider::class,
Copy after login

4. Now, you can execute the following command to start the Swoole HTTP service.

php artisan swoole:http start
Copy after login

ab Test

Test environment: ubuntu 18.04, 4-core 8-thread 2.7~3.5GHz cpu, 8G memory, 120G SSD

Test results :

ab parameters: ab -n 1000 -c 100

We found that Time per request is 2.512ms. Of course, this is just a simple request. No database queries are involved. Generally speaking, the performance is better than opcache, but this is a wild approach after all. We don’t know how many pitfalls there are. We don’t know yet whether this is used in production environments, but this idea is actually really good. Not bad. Finally, I look forward to PHP officially taking this as a research direction.

The above is the detailed content of How to speed up laravel through swoole. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template