Home PHP Framework Swoole How swoole improves php performance

How swoole improves php performance

Dec 07, 2019 pm 02:19 PM
swoole

How swoole improves php performance

Network request handling in Swoole is event-based and takes full advantage of the underlying epoll/kqueue implementation, making it very easy to serve millions of requests.

Swoole4 uses a new coroutine kernel engine, and now it has a full-time development team, so we are entering an unprecedented period in PHP history, providing unique possibilities for rapid performance improvements. (Recommended learning: swoole video tutorial)

Coroutine

Swoole4 or higher has a built-in coroutine with high availability. You can use fully synchronous code to achieve asynchronous performance, the PHP code does not have any additional keywords, and coroutine scheduling is automatically performed under the hood.

Developers can understand coroutines as ultra-lightweight threads, and you can easily create thousands of coroutines in a process.

It only takes 0.2 seconds to read massive data from MySQL with 10,000 concurrent requests

$s = microtime(true);
Co\run(function() {
    for ($c = 100; $c--;) {
        go(function () {
            $mysql = new Swoole\Coroutine\MySQL;
            $mysql->connect([
                'host' => '127.0.0.1',
                'user' => 'root',
                'password' => 'root',
                'database' => 'test'
            ]);
            $statement = $mysql->prepare('SELECT * FROM `user`');
            for ($n = 100; $n--;) {
                $result = $statement->execute();
                assert(count($result) > 0);
            }
        });
    }
});
echo 'use ' . (microtime(true) - $s) . ' s';
Copy after login

Hybrid server

You can Create multiple services on an event loop: TCP, HTTP, Websocket and HTTP2, and can easily host tens of thousands of requests.

function tcp_pack(string $data): string
{
    return pack('N', strlen($data)) . $data;
}
function tcp_unpack(string $data): string
{
    return substr($data, 4, unpack('N', substr($data, 0, 4))[1]);
}
$tcp_options = [
    'open_length_check' => true,
    'package_length_type' => 'N',
    'package_length_offset' => 0,
    'package_body_offset' => 4
];
---------------------------------------------------------------------------------------------------------------
$server = new Swoole\WebSocket\Server('127.0.0.1', 9501, SWOOLE_BASE);
$server->set(['open_http2_protocol' => true]);
// http && http2
$server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
    $response->end('Hello ' . $request->rawcontent());
});
// websocket
$server->on('message', function (Swoole\WebSocket\Server $server, Swoole\WebSocket\Frame $frame) {
    $server->push($frame->fd, 'Hello ' . $frame->data);
});
// tcp
$tcp_server = $server->listen('127.0.0.1', 9502, SWOOLE_TCP);
$tcp_server->set($tcp_options);
$tcp_server->on('receive', function (Swoole\Server $server, int $fd, int $reactor_id, string $data) {
    $server->send($fd, tcp_pack('Hello ' . tcp_unpack($data)));
});
$server->start();
Copy after login

The above is the detailed content of How swoole improves php performance. 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 Article Tags

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)

Which one is better, swoole or workerman? Which one is better, swoole or workerman? Apr 09, 2024 pm 07:00 PM

Which one is better, swoole or workerman?

How to use Swoole to implement a high-performance HTTP reverse proxy server How to use Swoole to implement a high-performance HTTP reverse proxy server Nov 07, 2023 am 08:18 AM

How to use Swoole to implement a high-performance HTTP reverse proxy server

How to use swoole coroutine in laravel How to use swoole coroutine in laravel Apr 09, 2024 pm 06:48 PM

How to use swoole coroutine in laravel

How does swoole_process allow users to switch? How does swoole_process allow users to switch? Apr 09, 2024 pm 06:21 PM

How does swoole_process allow users to switch?

Which one has better performance, swoole or java? Which one has better performance, swoole or java? Apr 09, 2024 pm 07:03 PM

Which one has better performance, swoole or java?

How to restart the service in swoole framework How to restart the service in swoole framework Apr 09, 2024 pm 06:15 PM

How to restart the service in swoole framework

Swoole Advanced: How to Optimize Server CPU Utilization Swoole Advanced: How to Optimize Server CPU Utilization Nov 07, 2023 pm 12:27 PM

Swoole Advanced: How to Optimize Server CPU Utilization

Swoole in action: How to use coroutines for concurrent task processing Swoole in action: How to use coroutines for concurrent task processing Nov 07, 2023 pm 02:55 PM

Swoole in action: How to use coroutines for concurrent task processing

See all articles