October 22, 2024
Chicago 12, Melborne City, USA
PHP

SWOOLE usage with PHP as backend


I use PHP/Nginx as backend for my App. I was afraid that when many users are calling functions into PHP, it will slow down as PHP does not support Async function calls. I check the below MacOS terminal command to get max calls/second by following command :

wrk -t4 -c100 http://103.22.110.32:80/a.php 

results:

Running 10s test @ http://103.22.110.32:80/a.php
  4 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   111.87ms   30.31ms 430.54ms   93.69%
    Req/Sec   207.00     46.30   313.00     79.08%
  8159 requests in 10.05s, 1.58MB read
  Non-2xx or 3xx responses: 8159
Requests/sec:    812.05
Transfer/sec:    160.98KB

I assume that this means that max concurrent calls are ~800 call/sec. ie if i have eg 80k users , it will wait for ~ 100 second.
I checked that SWOOLE can support Async call, making PHP superfast. speed can increase to > 60,000 calls/sec easilty.

Original PHP back end code :

 $tasknum = $request->get['Task'];
    switch ($tasknum) {
        case "task":
            Code for task A
            break;
        case "taskb":
           Code for task B
            break;
        case "taskc":
            Code for task c
            break;
        }

I have used below code for SWOOLE :

use Swoole\Http\Server;
use Swoole\Coroutine;
require_once 'vendor/autoload.php';

 

$server = new Server("0.0.0.0", 81);

$server->on('Request', function ($request, $response) {
    // Handle incoming requests
    $tasknum = $request->get['Task'];
    switch ($tasknum) {
        case "task":
            $response->header("Content-Type", "text/plain");
            $response->end("Task A executed successfully.");
            break;
        case "taskb":
            $response->header("Content-Type", "text/plain");
            $response->end("Task A executed successfully.");
            break;
        case "taskc":
            $response->header("Content-Type", "text/plain");
            $response->end("Task A executed successfully.");
            break;
        case "a":
            $response->header("Content-Type", "text/plain");
            $response->end("ppppirnt aaaa");
            break;
    }
});

$server->start();

Question :

  • is this correct approach for to use Swoole , or eg, i can use Croutine ?
  • how to call PHP API call with Task =taskc , eg.
  • when to start Swoole server, and does it stop , or it is working indefinitely



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video