walkor / workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
http://www.workerman.net
MIT License
11.09k stars 2.26k forks source link

how workerman is faster than swoole itself ? #616

Closed kocoten1992 closed 3 years ago

kocoten1992 commented 3 years ago

Hello, I saw benchmark on https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=plaintext

workerman: 25% swoole: 15%

I dont believe it so I wrote a script (below) to test it myself:

<?php

use Workerman\Worker;

require_once __DIR__ . '/vendor/autoload.php';

// #### http worker ####
$http_worker = new Worker('http://0.0.0.0:2345');

// 4 processes
$http_worker->count = 4;

// Emitted when data received
$http_worker->onMessage = function ($connection, $request) {
    //$request->get();
    //$request->post();
    //$request->header();
    //$request->cookie();
    //$request->session();
    //$request->uri();
    //$request->path();
    //$request->method();

    // Send data to client
    $connection->send("Hello World");
};

// Run all workers
Worker::runAll();
<?php
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

$server = new Swoole\HTTP\Server("127.0.0.1", 9501);
$server->set(['worker_num' => 4]);

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

$server->on("request", function (Request $request, Response $response) {
    $response->end("Hello World\n");
});

$server->start();

Result

Running 5s test @ http://127.0.0.1:9501
  4 threads and 4 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    83.34us  274.08us  11.56ms   99.18%
    Req/Sec    14.84k     4.04k   18.97k    58.33%
  301141 requests in 5.10s, 47.39MB read
Requests/sec:  59050.57
Transfer/sec:      9.29MB

Running 5s test @ http://127.0.0.1:2345
  4 threads and 4 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    80.86us  378.40us   9.77ms   97.93%
    Req/Sec    25.65k     2.17k   28.28k    71.50%
  510596 requests in 5.01s, 64.28MB read
Requests/sec: 102014.51
Transfer/sec:     12.84MB

It is true - but how :smiley: ? You are using swoole under the hood - so how is this possible ?

mondagPHP commented 3 years ago

it is true. workerman is better to traditional phper!🤣🤣🤣🤣🤣

walkor commented 3 years ago

You are using swoole under the hood - so how is this possible ?

Although workerman supports swoole as the underlying driver, but the above benchmark workerman did not use swoole.

I don't know why workerman performs better than swoole. It may be that swoole's internal implementation is more complex, which affects its performance.

fakharak commented 3 years ago

You are testing Swoole only for http (plain requests / response) layer, without using coroutines to multiplex other I/O like Database, Redis, System Process/ Commands.

Plus, after frameworks achieves performance upto a certain level, we seek a best trade-off (equilibrium) between Performance and Featrues. Swoole certainily has all futuristic scalable modern Features required modern Applications like IoT, Real-time Network Communication, In-memory / Memory-first Architecture, Games and other Micro-services based on Communicating Sequential Process (CSP) model of Concurrent Programming.

In my personal opinion, the questions to ask before choosing "Workerman" over "Swoole" are:

  1. Does the Workerman allows writing elegant, modern and lighting fast Coroutines (also called as "Green Threads', or "Fibers") in order to perform operations asynchronously on each user request ? The real power (and, Performance) of Swoole comes with its Coroutines and Channels. This is what Benchmarks can not show.

  2. Does the Workerman creates multiple Reactor-threads within each Service ? in order to maintain a balance between low-load (or, slow) connections and high-load connections ? OR, it just works like NodeJS (with single-Reactor Thread per server) where a slow connection and/or response causes all user connections (users requests) to wait. Swoole's Master Process initiates multiple Reactor-threads.

  3. Does the Workerman allows an elegant way for CPU-intensive (or, long running tasks) to be spawned in a separate "Task Worker Process"? in order to be able to run such tasks asynchronously and parallely ? so that CPU-intensive tasks do not hinder I/O-bound operations. Swoole allows that bifurcation with one line elegant code.

  4. Does the "Workerman" provides an elegant way to spin background Processes (Daemons), dynamically (at run time) ? to enable Parallel Processing (with a Time ticker with millisecond intervals) replacing highly inefficient process of creating inefficient Crons.

  5. Does the Workerman provides client-side implementations for all of the Network Protocols ? that Swoole provides.