Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joanhey opened this issue Oct 17, 2022 · 70 comments
Open

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

joanhey opened this issue Oct 17, 2022 · 70 comments

Comments

@joanhey
Copy link
Contributor

joanhey commented Oct 17, 2022

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:

  • Symfony
  • Laravel
  • Yii2
  • Slim
  • KumbiaPHP
  • ...

Now I'm creating PRs in the Techempower benchmark.
TechEmpower/FrameworkBenchmarks#7627
TechEmpower/FrameworkBenchmarks#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
Copy link
Owner

walkor commented Oct 18, 2022

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
Copy link
Contributor Author

joanhey commented Oct 20, 2022

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
Copy link
Contributor Author

joanhey commented Oct 20, 2022

image
image
image

@joanhey
Copy link
Contributor Author

joanhey commented Oct 22, 2022

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
Copy link
Contributor Author

joanhey commented Oct 23, 2022

Laravel and Symfony login working !!!

@walkor
Copy link
Owner

walkor commented Oct 23, 2022

That great ! 👍

@joanhey
Copy link
Contributor Author

joanhey commented Oct 23, 2022

Grabacion_microsoft-edge-dev_20221023164232.mp4

@joanhey
Copy link
Contributor Author

joanhey commented Oct 23, 2022

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

@joanhey
Copy link
Contributor Author

joanhey commented Oct 23, 2022

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

Grabacion_microsoft-edge-dev_20221023164700.mp4

@joanhey
Copy link
Contributor Author

joanhey commented Oct 27, 2022

Working with CakePHP too

@joanhey
Copy link
Contributor Author

joanhey commented Nov 4, 2022

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
Copy link
Owner

walkor commented Nov 5, 2022

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

@joanhey
Copy link
Contributor Author

joanhey commented Nov 5, 2022

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
Copy link
Contributor Author

joanhey commented Nov 5, 2022

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

@joanhey
Copy link
Contributor Author

joanhey commented Nov 6, 2022

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
Copy link
Contributor Author

joanhey commented Nov 7, 2022

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
Copy link
Contributor Author

joanhey commented Nov 8, 2022

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

@joanhey
Copy link
Contributor Author

joanhey commented Nov 8, 2022

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

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

@joanhey
Copy link
Contributor Author

joanhey commented Nov 10, 2022

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
Copy link
Contributor Author

joanhey commented Nov 11, 2022

Trying now with Codeigniter4

@joanhey
Copy link
Contributor Author

joanhey commented Nov 12, 2022

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

👍

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
  • Without ORM and db class optimized for Workerman

@joanhey
Copy link
Contributor Author

joanhey commented Nov 14, 2022

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

@joanhey
Copy link
Contributor Author

joanhey commented Nov 16, 2022

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
Copy link
Contributor Author

joanhey commented Nov 17, 2022

In the mean time, you or me can add
#[\AllowDynamicProperties]
To the Worker class and Connection interface.

walkor added a commit that referenced this issue Nov 17, 2022
walkor added a commit that referenced this issue Nov 17, 2022
@walkor
Copy link
Owner

walkor commented Nov 17, 2022

In the mean time, you or me can add
#[\AllowDynamicProperties]
To the Worker class and Connection interface.

Done

@joanhey
Copy link
Contributor Author

joanhey commented Nov 17, 2022

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

@walkor
Copy link
Owner

walkor commented Nov 21, 2022

Hi joanhey, here are the twitter accounts.

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

@joanhey
Copy link
Contributor Author

joanhey commented Nov 29, 2022

Lumen v9
600% faster
image

@joanhey
Copy link
Contributor Author

joanhey commented Dec 16, 2022

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
Copy link
Contributor Author

joanhey commented Dec 17, 2022

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.

TechEmpower/FrameworkBenchmarks#7798

@joanhey
Copy link
Contributor Author

joanhey commented Dec 17, 2022

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

image

@yangguangwuwu
Copy link

Great solution

@euii
Copy link

euii commented Dec 23, 2022

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

@joanhey
Copy link
Contributor Author

joanhey commented Dec 27, 2022

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
Copy link
Contributor Author

joanhey commented Dec 31, 2022

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
Copy link
Contributor Author

joanhey commented Dec 31, 2022

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

`

@walkor
Copy link
Owner

walkor commented Jan 1, 2023

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

which protocol?

@joanhey
Copy link
Contributor Author

joanhey commented Jan 1, 2023

The new Http protocol from v4, it's using Request and Response classes.
Never populate or read the Super Globals Variables from PHP ($_GET, $_POST, $_SESSION, ...), necessary to use with any php app.
I can try to do it with these classes, but I think it's easier to have a new protocol like the Http from v3.
We can start with the same in v3, to improve it later. Starting by reusing the new SessionHandler class.

@493226876
Copy link

I have a very serious problem with the token of larravel in use. My logins in other places are consistent, which means that the token will not change, and my authentication session authentication will not be secure

@493226876
Copy link

After using the worker man acceleration task larravel project, I can directly log in to other computers after logging in to one computer, such as the session global sharing. What should I do

@joanhey
Copy link
Contributor Author

joanhey commented Jan 2, 2023

@493226876 please use the adapterman issues

@walkor
Copy link
Owner

walkor commented Jan 2, 2023

@joanhey

Workerman removed the super global variables filling since 4.0 because the super global variables cannot be used in asynchronous environment and co scheduling environment.

It is easy to fill in super global variables in version 4.0 which codes like below.

$worker->onMessage = function($connection, $request){
    $_GET = $request->get();
    $_POST = $request->post();
    $_SESSION = $request->session()->all();
    $connection->send(run());
};

Workerman should not have two similar http protocols, which will confuse developers.
Except AdapterMan, other workerman projects do not need super global variables, so it is unwise to add an another similar http protocol to the workerman. I prefer that AdapterMan is compatible with the http protocol of workerman 4.0, or AdapterMan uses its own http protocol.

@493226876
Copy link

How to use it? I log in to sesssionID on different computers all the time

@493226876
Copy link

The sesssionID printed by my browser refreshed in different environments frequently appears the same as that of other networks

@joanhey
Copy link
Contributor Author

joanhey commented Jan 2, 2023

@493226876 Please use the issues in Adapterman

Precisely the problem is that Laravel is the only fw, that don't use the $_SESSION.
We are talking with Laravel devs, to clean the session in each request, but from the Laravel side, it's not a problem from Adapterman or Workerman. But we will release the solution.
This week I'll try it with the Shared Nothing mode, that for sure will work, but the performance will be in the middle of php-fpm and worker mode.

@493226876
Copy link

493226876 commented Jan 2, 2023 via email

@493226876
Copy link

OK, thank you for telling me. I always thought that it was a problem where I set up the session. The session has always been repeated. It was originally a mechanism problem. If you have any trouble, please let me know. Thank you

@joanhey
Copy link
Contributor Author

joanhey commented Jan 2, 2023

Please use the isssues in Adapterman.
And I'll talk with you, not here.

@493226876
Copy link

Ok, I will follow your latest news, look forward to the new release, and worship the boss!

@lizhichao
Copy link

This is powerful, simple and easy to use

@walkor walkor pinned this issue Jan 16, 2023
@493226876
Copy link

@joanhey Hello, is the Shared Nothing mode available

@joanhey
Copy link
Contributor Author

joanhey commented Jan 17, 2023

No, and still not working like Shared Nothing for now.

@joanhey
Copy link
Contributor Author

joanhey commented Mar 13, 2023

Release v0.6
Use Workerman v4.1.x and recipe for ThinkPHP.

Still we need a Laravel dev, to help with close the session when finish each request. (Laravel don't use $_SESSION)

@493226876
Copy link

493226876 commented Mar 21, 2023 via email

@jhdxr
Copy link
Contributor

jhdxr commented Apr 13, 2023

Laraver still hasn't solved the problem of closing the session every time a request is made

@493226876 There is a tryGcSessions, and here is an example from AdapterMan. You can try to modify your own start.php.

And the most important thing, YOU SHOULD USE ADAPTERMAN ISSUES to ask/discuss this.
你应该在ADAPTERMAN ISSUES咨询/讨论相关的问题

Still we need a Laravel dev, to help with close the session when finish each request. (Laravel don't use $_SESSION)

@joanhey , I just commented on this several days ago (joanhey/AdapterMan#5 (comment)), actually laravel do have a way to clean up after each request, with the help of Laravel Octane.

@ashokkumar88
Copy link

ashokkumar88 commented Aug 14, 2023

Trying now with Codeigniter4

@joanhey Is it working with Codeigniter 4?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests