企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
看到哔哩哔哩有个视频对比golang和webman helloword压力测试性能,webman比goloang低了很多,低我理解,golang毕竟多线程自带协程,而且webman是框架,golang是http标准库,不在一个层次,但是感觉不会低这么多才对。还有我觉得应该是golang的http标准库和workerman对比才公平。 所以我特地买了台aliyun服务器测试下,顺便把swoole workerman也压测了下,结果也发到了群里,既然测试了就记录下来给大家参考下,代码都有大家可以自行测试。 ## 环境配置: * 4核(vCPU) 4 GiB Ubuntu 20.04 64位 * PHP 7.4.3 * Go version go1.13.8 linux/amd64 ## 压测命令 `ab -n100000 -c200 http://127.0.0.1:xxx/`没开keepalive `ab -n100000 -c200 -k http://127.0.0.1:xxx/`开了keepalive ## 进程数(线程数数): 对go不熟悉,不知道go怎么开多进程,为了公平起见我默认workerman webman swoole 全部1个进程,go就是教程里的helloword代码,应该也是一个进程,是不是多线程我不知道。 ## 先贴结果: | | 没开keepalive | 开了keepalive | | --- | --- | --- | | golang | 19995 | 98546 | | workerman | 30120 | 125986 | | webman | 29301 | 85938 | | swoole | 25836 | 73304 | | swoole+协程 | 27093 | 54596 | 结果是workeman压测性能高于golang,webman短连接高于golang,keepalive长连接略低于golang。 swoole短连接高于golang,keepalive低于golang。workerman和webman不管是短连接还是keepliave都高于swoole。 ## go代码 ~~~ package main import ( "net/http" ) func handler(w http.ResponseWriter,r *http.Request) { w.Write([]byte("hello")) } func main() { http.HandleFunc("/",handler) http.ListenAndServe(":8080",nil) } ~~~ ## workerman代码 ~~~php require_once __DIR__ . '/vendor/autoload.php'; use Workerman\Worker; $worker = new Worker('http://0.0.0.0:12345'); $worker->onMessage = function($connection, $request) { // 不加-k参数时要用close才行? $connection->close('hello'); }; Worker::runAll(); ~~~ ~~~php require_once __DIR__ . '/vendor/autoload.php'; use Workerman\Worker; $worker = new Worker('http://0.0.0.0:12345'); $worker->onMessage = function($connection, $request) { // 不加-k时用send $connection->send('hello'); }; Worker::runAll(); ~~~ workerman这里不太好,ab测试时-k 参数需要自己区别处理,所以需要两个脚本 ## webman代码 ~~~php <?php namespace app\controller; use support\Request; class Index { public function index(Request $request) { return 'hello'; } } ~~~ ## swoole代码 ~~~php <?php $http = new Swoole\Http\Server('0.0.0.0', 12346, SWOOLE_BASE); $http->on('Request', function ($request, $response) { $response->end('hello'); }); $http->start(); ~~~ ## swoole协程代码 ~~~php <?php use Swoole\Coroutine\Http\Server; use function Swoole\Coroutine\run; run(function () { $server = new Server('127.0.0.1', 9502, false); $server->handle('/', function ($request, $response) { $response->end("hello"); }); $server->start(); }); ~~~ ## 不开keep-alive结果截图: [截图](https://www.workerman.net/upload/img/20220510/106279e1e6de54.png) [截图](https://www.workerman.net/upload/img/20220510/106279e1ef3e76.png) [截图](https://www.workerman.net/upload/img/20220510/106279e1f6cbcd.png) [截图](https://www.workerman.net/upload/img/20220510/106279e2005f2e.png) [截图](https://www.workerman.net/upload/img/20220510/106279e2074aba.png) ## 开keep-alive结果截图 [截图](https://www.workerman.net/upload/img/20220510/106279e2200440.png) [截图](https://www.workerman.net/upload/img/20220510/106279e228d8f0.png) [截图](https://www.workerman.net/upload/img/20220510/106279e22e224d.png) [截图](https://www.workerman.net/upload/img/20220510/106279e2388f45.png) [截图](https://www.workerman.net/upload/img/20220510/106279e23ef282.png) ## 最后 我知道肯定会有人说helloword压测没有意义,但是我觉得还有一定意义的,毕竟代表了框架的极限性能啊。 [https://learnku.com/laravel/t/63523](https://learnku.com/laravel/t/63523) 另外这里也有一个golang框架与webman的比较,带简单业务的,大家也可以参考的