In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what is the reason for running Laravel on Swoole". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the reason for running Laravel on Swoole" can help you solve the problem.
Swoole is a production-level asynchronous programming framework developed for PHP. It is a pure C development extension that allows PHP developers to write high-performance, extensible concurrent TCP, UDP, Unix socket, HTTP, WebSocket services in PHP without having too much knowledge of non-blocking Itemo programming and low-level Linux kernels. You can think of Swoole as NodeJS, but there will be higher performance for PHP.
Why run Laravel on Swoole?
The following figure shows the life cycle of PHP. As you can see, every time you run a PHP script, PHP needs to initialize the module and start the Zend engine for your runtime environment. And compile the PHP script to OpCodes for execution by the Zend engine.
However, such a lifecycle needs to be executed every time it is requested. Because the environment created by a single request is destroyed immediately after the completion of the request execution.
In other words, in the traditional PHP lifecycle, a lot of time is wasted creating and destroying resources for script execution. Imagine a framework like Laravel, how many files need to be loaded in each request? At the same time, it also wastes a lot of Istroke O operations.
So what if we use Swoole to build an application-level Server, and all script files can be saved in memory after loading once? That's why we need to try running Laravel on Swoole. Swoole can provide powerful performance while Laravel can provide elegant code structure. These two are a perfect match!
Installation
The following are the main features of swooletw/laravel-swoole:
Run the Laravel/Lumen application in Swoole
Excellent performance up to 30x
Sandboxie mode isolation application container
Support for running WebSocket servers in Laravel applications
Support for Socket.io protocol
Support sharing of Swoole tables across processes
Install using Composer:
$composer require swooletw/laravel-swoole
This package depends on Swoole. Before using this package, make sure that your machine has the correct Swoole installed. Quickly install (linux) using the following command:
Pecl install swoole
After installing this extension, you need to edit php.ini to add extension=swoole.so.
Php-I | grep php.ini # check the php.ini file locationsudo echo "extension=swoole.so" > > php.ini # add the extension=swoole.so to the end of php.iniphp-m | grep swoole # check if the swoole extension has been enabled
Visit the official website for more information.
Note: Swoole now only supports Linux and OSX. The Windows server does not support it yet.
Then, add the service provider:
If you use Laravel, add the service provider to the config/app.php service provider array:
['providers' = > [SwooleTW\ Http\ LaravelServiceProvider::class,],]
If you use Lumen, add the following code to bootstrap/app.php:
$app- > register (SwooleTW\ Http\ LumenServiceProvider::class)
This package supports automatic packet discovery. If you are running Laravel version 5.5 or later, you can skip this step.
Set up and run
Now, you can execute the following command to start the Swoole HTTP service.
$php artisan swoole:http start
Then you can see the following message:
Starting swoole http server...Swoole http server started:
You can now enter the Laravel application by visiting http://127.0.0.1:1215.
Benchmark test
Test with the MacBook Air 13-inch (produced in 2015) and clean Lumen 5.5 project:
Benchmark tool: wrk
Wrk-T4-C100 http://your.app
Nginx based on FPM
Running 10s test @ http://lumen.app:9999 4 threads and 100connections Thread Stats Avg Stdev Max + /-Stdev Latency 1.14s 191.03ms 1.40s 90.31% Req/Sec 22.65 10.65 50.00 65.31% 815 requests in 10.07s, 223.65KB readRequests/sec: 80.93Transfer/sec: 22.21KB
Swoole HTTP service
Running 10s test @ http://127.0.0.1:1215 4 threads and 100connections Thread Stats Avg Stdev Max + /-Stdev Latency 11.58ms 4.74ms 68.73ms 81.63% Req/Sec 2.19k 357.43 2.90k 69.50% 87879 requests in 10.08s 15.67MB readRequests/sec: 8717.00Transfer/sec: 1.55MB, that's all for "what's the reason for running Laravel on Swoole?" Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.