众所周知,workerman是基于php cli的,由于php cli模式下无法使用php自带的header、sesion、cookie等函数,这导致将传统的php项目无法直接在workerman容器下直接运行。
我一度以为让传统业务在workerman中运行,就必须更改框架甚至业务代码以适配workerman,直到joanhey发了一个issue,打破了我的认知。
他们发布了一个名叫AdapterMan的项目,它可以做到不更改传统框架代码的情况下让你的传统php项目放到workerman中正常运行,并且他们公司已经在生产环境用了2年。
注意,是零代码改动直接让laravel、lumen、Slim等框架的项目在workerman上运行。
目前他们已经在laravel、lumen、Slim、Symfony、CakePHP、Yii2、KumbiaPHP 等做了初步压力测试,性能有很大的提升。
以下是压测结果
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% |
Symfony 6
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 |
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 |
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 |
Lumen v9
接入代码类似
<?php
require_once __DIR__ . '/vendor/autoload.php';
use AdaptermanAdapterman;
use WorkermanWorker;
Adapterman::init();
$http_worker = new Worker('http://0.0.0.0:8080');
$http_worker->count = 8;
$http_worker->name = 'AdapterMan';
$http_worker->onWorkerStart = static function () {
//init();
require __DIR__.'/start.php';
};
$http_worker->onMessage = static function ($connection, $request) {
$connection->send(run());
};
Worker::runAll();
项目地址:
https://github.com/joanhey/AdapterMan 强烈建议大家为其点赞(点星星)
相关链接:
https://github.com/walkor/workerman/issues/824
相关博文
使用workerman加速任意项目