Tutorial PHP: Apa itu PHP Swoole?

Apakah Swoole itu? Swoole adalah sebuah extension PHP yang memungkinkan progammer PHP menulis sebuah code yang high-performance, scalable, concurent, dan asyncronous. Baik itu TCP, UDP, Unix Socket, HTTP1, HTTP2 dan Websocket.

Secara sederhana, bisa dibilang, Swoole adalah jawaban hinaan dari komunitas NodeJS yang mencari kemampuan asyncronous dan scalable di PHP.

Contoh HTTP Server bawaan Swoole:

<?php
$http = new swoole_http_server("127.0.0.1", 9501);

$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$http->start();

kemudian contoh Swoole Websocket:

<?php
$server = new swoole_websocket_server("127.0.0.1", 9502);

$server->on('open', function($server, $req) {
    echo "connection open: {$req->fd}\n";
});

$server->on('message', function($server, $frame) {
    echo "received message: {$frame->data}\n";
    $server->push($frame->fd, json_encode(["hello", "world"]));
});

$server->on('close', function($server, $fd) {
    echo "connection close: {$fd}\n";
});

$server->start();

dan contoh asyncronous task:

<?php
$server = new swoole_server("127.0.0.1", 9502);
$server->set(array('task_worker_num' => 4));
$server->on('receive', function($server, $fd, $from_id, $data) {
    $task_id = $server->task("Async");
    echo "Dispath AsyncTask: [id=$task_id]\n";
});
$server->on('task', function ($server, $task_id, $from_id, $data) {
    echo "New AsyncTask[id=$task_id]\n";
    $server->finish("$data -> OK");
});
$server->on('finish', function ($server, $task_id, $data) {
    echo "AsyncTask[$task_id] finished: {$data}\n";
});
$server->start();

Hasil Benchmark Swoole

Klaim lebih cepat dibanding setup PHP biasa, baik menggunakan Apache maupun NGINX/PHP-FPM, bisa dilihat dan dicoba sendiri dari repo Github: https://gist.github.com/nkt/e49289321c744155484c

Contoh hasilnya:

a. PHP + Swoole

Running 10s test @ http://127.0.0.1:1337/
  4 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     7.02ms    6.94ms  82.86ms   85.27%
    Req/Sec   827.17    552.85     2.38k    66.54%
  22543 requests in 10.07s, 3.61MB read
  Socket errors: connect 151, read 22680, write 86, timeout 0
Requests/sec:   2238.19
Transfer/sec:    367.20KB

dan jika dengan NodeJS:

Running 10s test @ http://127.0.0.1:1337/
  4 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    20.34ms    4.50ms 125.04ms   97.89%
    Req/Sec     3.09k     1.93k    6.06k    55.00%
  122838 requests in 10.02s, 23.66MB read
  Socket errors: connect 151, read 69, write 73, timeout 0
Requests/sec:  12263.37
Transfer/sec:      2.36MB

Menarik kan? Nah pada seri tutorial PHP kali ini kita akan membahas PHP Swoole dan Laravel.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: