walkor / workerman

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

[Feature] Use almost any PHP fw or app with Workerman #824

Open joanhey opened 1 year ago

joanhey commented 1 year ago

Hi Walkor,

I was using a lib internally for more than 2 years, for my legacy apps to run with Workerman v3.5. https://github.com/joanhey/AdapterMan

This week I tested with frameworks, and It's working perfectly, without touch 1 line of code in the fw or app.

We only need an small change to the Front Controller and PHP 8 as minimum.

Tested with:

Now I'm creating PRs in the Techempower benchmark. https://github.com/TechEmpower/FrameworkBenchmarks/pull/7627 https://github.com/TechEmpower/FrameworkBenchmarks/pull/7626 Later I will push Slim and more fws.

I think that this lib will be useful for more people.

If time permits, I'll try to add documentation and upgrade to Workerman v4

walkor commented 1 year ago

It looks amazing. I thought it was impossible to finish the work, but you solved it easily. Thank you so much for your efforts. 👍

joanhey commented 1 year ago

Still fixing small details with the session.

The blog work without any problem. Only the admin area, have small problems with the sessions.

Symfony demo with Workerman, an user authenticated : image

joanhey commented 1 year ago

image image image

joanhey commented 1 year ago

Laravel Orchid admin panel with Workerman, with Laravel the login is working image

Drupal with Workerman, but still fail the login with symfony. image

joanhey commented 1 year ago

Laravel and Symfony login working !!!

walkor commented 1 year ago

That great ! 👍

joanhey commented 1 year ago

https://user-images.githubusercontent.com/249085/197398600-388ebce3-ece9-4dfa-ba98-fc4d61805e07.mp4

joanhey commented 1 year ago

Now I'll create a docker with the symfony-demo. In port 8080 with Workerman and in 8081 with php-fpm.

joanhey commented 1 year ago

Symfony initialization 0ms. And half time per request, once loaded the first time.

https://user-images.githubusercontent.com/249085/197398802-fb2bc1e4-ec3d-4a2b-9187-4e40585e5ddc.mp4

joanhey commented 1 year ago

Working with CakePHP too

joanhey commented 1 year ago

The first results from the bench Without touch a line of code and full ORM.

Laravel 8

Fw Plaintext Json Single query Multiple query Updates Fortunes
Laravel 14,799 14,770 9,263 3,247 1,452 8,354
Laravel Roadrunner 482 478 474 375 359 472
Laravel Swoole 38,824 37,439 21,687 3,958 1,588 16,035
Laravel Laravel s 54,617 49,372 23,677 2,917 1,255 16,696
Laravel Workerman 103,004 99,891 46,001 5,828 1,666 27,158
Laravel with Workerman % gain 596.02% 576.31% 396.61% 79.489% 14.738% 225.09%

In 2 days Symfony. The next week: Laravel 9, Lumen 9, Cakephp and Slim.

walkor commented 1 year ago

Very good. It seems that the performance improvement is significant. 👍

joanhey commented 1 year ago

Yes, it's very significant. And here the results are bad, for using the very slow Laravel ORM.

The next week, we will see the Slim with raw db and optimized db for workerman.

joanhey commented 1 year ago

I don't know any PHP ORM optimised for persistent apps.

joanhey commented 1 year ago

image

The latency is really good !!

image

And later, we will check the memory used. Kumbiaphp use half the memory using Workerman. We will have better numbers with other fws.

joanhey commented 1 year ago

Symfony 6

700% faster in the Fortunes image

Latency

image

Fw Plaintext Json Single query Multiple query Updates Fortunes
Symfony 38,231 37,557 12,578 10,741 3,420 10,741
Symfony Workerman 210,796 197,059 107,050 13,401 4,062 71,092

https://github.com/joanhey/AdapterMan#performance-bench :)

joanhey commented 1 year ago

With CakePHP we have worst results with Workerman. :( I'll investigate if it's an error or directly a problem with CakePHP

joanhey commented 1 year ago

I think that I found the problem, It's related to Cakephp. Changes passed, and in 7 days we have the new results. https://github.com/TechEmpower/FrameworkBenchmarks/pull/7680

Looking the cakephp bootstrap, it check if is CLI SAPI, and load a cli bootstrap.

joanhey commented 1 year ago

Laravel 9 have the same numbers than version 8.

Lumen 9

Fw Plaintext Json Single query Multiple query Updates Fortunes
Lumen 18,998 18,616 10,791 3,496 1,461 9,223
Lumen Swoole 44,861 43,598 24,255 4,178 1,599 16,854
Lumen Laravel s 93,335 82,745 31,567 3,030 1,282 21,130
Lumen Workerman 185,126 177,667 58,729 5,857 1,662 31,430

Very similar numbers to Laravel.

joanhey commented 1 year ago

Trying now with Codeigniter4

joanhey commented 1 year ago

Performance info

Slim3 vs Slim4 without ORM Framework JSON 1-query 20-query Fortunes Updates Plaintext
Slim 3 35,588 29,529 12,579 28,211 1,999 36,247
Slim 4 38,305 34,272 12,579 32,634 2,097 35,251

:+1:

Slim with Workerman

Without ORM Framework JSON 1-query 20-query Fortunes Updates Plaintext
Slim 4 38,305 34,272 12,579 32,634 2,097 35,251
Slim 4 Workerman 129,393 81,889 15,803 73,212 2,456 134,531
Slim 4 Workerman pgsql * 102,926 19,637 92,752 14,875
joanhey commented 1 year ago

CakePHP still slower. Asking for help in the Cakephp slack.

joanhey commented 1 year ago

I'm updating all fw to php 8.2, in the bencmark.

But Workerman 3.5 have problems with dynamic properties. image And fail in some frameworks.

Can we move it from 3.5 to v4, as another protocol ? So we also could optimize it for php 7.

joanhey commented 1 year ago

In the mean time, you or me can add

[\AllowDynamicProperties]

To the Worker class and Connection interface.

walkor commented 1 year ago

In the mean time, you or me can add

[\AllowDynamicProperties]

To the Worker class and Connection interface.

Done

joanhey commented 1 year ago

Walkor, open a twitter for Workerman (and Webman).

walkor commented 1 year ago

Hi joanhey, here are the twitter accounts.

https://twitter.com/workermanphp https://twitter.com/webmanphp

joanhey commented 1 year ago

Lumen v9 600% faster image

walkor commented 1 year ago

Great 👍

joanhey commented 1 year ago

When you have a moment, Could we talk in private ? Thank you

walkor commented 1 year ago

Ok, my email walkor@workerman.net .

joanhey commented 1 year ago

The Symfony people ask me to add the new FrankenPHP. So I added Swoole and FrankenPHP using the phpruntime too.

The symfony results are ready: image

So we'll show the json and single query:

Json image

Latency image

Single query image Latency image

The link to visualize the results: https://www.techempower.com/benchmarks/#section=test&runid=124fd2af-3030-4315-8876-e1db1fa91193&test=json&f=zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-v2qiv3-zik0zj-zik0zj-cn3

joanhey commented 1 year ago

https://twitter.com/adaptermanphp/status/1600530557368156160

joanhey commented 1 year ago

Testing Adapterman, without worker mode. Directly including file.php in the fork. And for now it's OK. When I make more tests, I'll send you the info.

joanhey commented 1 year ago

Hmmm, Could you add https://github.com/joanhey/AdapterMan in https://www.workerman.net/related_projects We need more marketing and hands.

walkor commented 1 year ago

Done. And I published an article in the community about your project. https://www.workerman.net/q/9831

Tinywan commented 1 year ago

nice

gumphp commented 1 year ago

niubility

iYting commented 1 year ago

wonderful

xbchome commented 1 year ago

👍

joanhey commented 1 year ago

Some people ask me, that fail to serve statics files.

Only to clarify this: Initially Adapterman want to be an APP SERVER not a web server.

The idea is change the php-fpm with Adapterman + Workerman. Also like do now Laravel Octane.

Of course, we are open to ideas to add a web server. But I think that will be not scalable.

joanhey commented 1 year ago

I'll add this to the README, and also add a Nginx config example.

joanhey commented 1 year ago

Easy examle, to show the execution time. But useful to debug anything.

$http_worker->onMessage = static function ($connection, $request) {

    $start = microtime(true);

    $connection->send(run());

    // Here you can check anything    
    Worker::safeEcho($_SERVER['REQUEST_METHOD'] ."\t". $_SERVER['REQUEST_URI'] ."\t". round((microtime(1) - $start) * 1000, 4) . ' ms' . PHP_EOL); // . print_r($_SESSION);

};
joanhey commented 1 year ago

Workerman without worker mode, including the .php file in each request, from the plain php benchmark.

image

Using your exec_php_file()

function exec_php_file($file)
{
    ob_start();
    // Try to include php file.
    try {
        include $file;
    } catch (\Exception $e) {
        echo $e;
    }
    return ob_get_clean();
}

I need to change the catch Exception to catch Throwable.

https://github.com/TechEmpower/FrameworkBenchmarks/pull/7798

joanhey commented 1 year ago

Bench in my local PC, with the json.php test without worker mode. With 100 concurrent connections.

image

yangguangwuwu commented 1 year ago

Great solution

euii commented 1 year ago

Looks like an interesting solution and I'm very interested in trying it out.

joanhey commented 1 year ago

Shared Nothing mode, like with PHP-FPM. Using the same .php files.

image

Framework JSON 1-query Multiple queries Fortunes Updates Plaintext
php php-fpm 187,747 97,658 12,784 79,309 2,010 195,283
php workerman 822,930 134,475 15,648 124,923 4,683 1,161,016

link to the results

joanhey commented 1 year ago

We need to add the protocol to Workerman v4.2 and/or 5

So we can work together with the new session, add more adapters and functionalities.

The problem is: the name of the protocol ? phphttp, php, phpruntime(not recommended), ....

joanhey commented 1 year ago

The new session still use a random GC, that affect the performance of n requests. We need to change it, to use a Timer to clean the expired sessions. Like do debian/ubuntu. They use by default session.gc_probability=0, and use a cron or a systemd.timer to run phpcleansession

https://www.getpagespeed.com/server-setup/php/cleanup-php-sessions-like-a-pro

`