swoole和workerman性能对比

天道酬勤

swoole是c/c++写的网络通讯扩展,workerman是纯php写的网络框架,两者功能类似。我看到网上很多评论说swoole性能比workerman高不少,甚至还看到知乎有说workerman和swoole比不在一个数量级的。但是为什么我的压测结果却不是这样,不管是我的mac pro还是刚刚阿里云服务器,二者性能差不多甚至有时workerman要比swoole压测性能高一些?以下是我的压测脚本和结果。

环境 阿里云:Ubuntu 18.04.3 LTS; 4核 4G;php7.2;swoole 4.4.15; workerman 3.4.24;

swoole 压测脚本:

<?php
$http = new swoole_http_server("127.0.0.1", "1234");
$http->set(array(
    'worker_num' => 4, 
    'daemonize' => false, 
));
$http->on('request', function ($request, $response) {
    $response->end("hello");
});
$http->start();

workerman 压测脚本:

<?php
require_once __DIR__ . '/workerman/Autoloader.php';
use Workerman\Worker;
$worker = new Worker('http://0.0.0.0:12345');
$worker->count = 4;
$worker->onMessage = function($connection, $data) {
        $connection->send('hello');
};

swoole 结果:

 ab -n1000000 -c1000 -k http://127.0.0.1:1234/
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests

Server Software:        swoole-http-server
Server Hostname:        127.0.0.1
Server Port:            1234

Document Path:          /
Document Length:        5 bytes

Concurrency Level:      1000
Time taken for tests:   11.421 seconds
Complete requests:      1000000
Failed requests:        0
Keep-Alive requests:    1000000
Total transferred:      157000000 bytes
HTML transferred:       5000000 bytes
Requests per second:    87561.23 [#/sec] (mean)
Time per request:       11.421 [ms] (mean)
Time per request:       0.011 [ms] (mean, across all concurrent requests)
Transfer rate:          13424.91 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1  26.0      0    1040
Processing:     2   11   5.2     11     244
Waiting:        2   11   5.2     11     244
Total:          2   11  29.6     11    1281

Percentage of the requests served within a certain time (ms)
  50%     11
  66%     12
  75%     12
  80%     12
  90%     13
  95%     13
  98%     16
  99%     21
 100%   1281 (longest request)

workerman结果:

 ab -n1000000 -c1000 -k http://127.0.0.1:12345/
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests

Server Software:        workerman
Server Hostname:        127.0.0.1
Server Port:            12345

Document Path:          /
Document Length:        5 bytes

Concurrency Level:      1000
Time taken for tests:   10.674 seconds
Complete requests:      1000000
Failed requests:        0
Keep-Alive requests:    1000000
Total transferred:      125000000 bytes
HTML transferred:       5000000 bytes
Requests per second:    93686.80 [#/sec] (mean)
Time per request:       10.674 [ms] (mean)
Time per request:       0.011 [ms] (mean, across all concurrent requests)
Transfer rate:          11436.38 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.7      0      29
Processing:     3   11   2.5     10      26
Waiting:        3   11   2.5     10      26
Total:          3   11   2.7     10      47

Percentage of the requests served within a certain time (ms)
  50%     10
  66%     10
  75%     11
  80%     11
  90%     11
  95%     19
  98%     20
  99%     21
 100%     47 (longest request)

二者性能基本相近,没有特别大的区别。有兴趣的可以自己试下上面的压测脚本,看看结果是不是和我一致。

---------2021-08-03分割线----------
发现这里有个权威的压测网站,有各种框架压测结果对比。有带业务的,有不带的。参见:https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=json&l=zik073-sf

22656 12 0
12个回答

censc

没有带业务的测试是没什么用的,有时候你的电脑某个进程抽了呢。也会影响测试结果的对吧 就这么简单的测试没啥意义 在业务里使用然后对比

  • 天道酬勤 2020-02-07

    上面结果是多次测试各自采用的最佳结果。helloworld对比还是很有意义的,至少说明框架自身性能很好,框架自身不会成为瓶颈。如果连helloworld都不行,那上业务就更不行了啊。

  • censc 2020-02-07

    @6237:那随意吧,反正我只能说这两个用来做做聊天 做做游戏的服务器端都没啥问题

  • lxping 2020-02-07

    @6237: 说swoole比workerman好,是因为评论的人没用过workerman,他只是片面的认为c++比php快,就认为其程序框架也是这样。
    然而,workerman只载入解析一次php文件,然后便常驻内存,极大的减少了磁盘IO及PHP中请求初始化、创建执行环境、词法解析、语法解析、编译opcode、请求关闭等诸多耗时过程。所以纯碎从语言层面比较,更应该是c++和机器码的区别。
    两者都是非常优秀的开源框架,只是看自己更喜欢折腾c++还是php罢了

  • 胡方方 2020-06-25

    @6650 swoole也是常住内存,你说的这些swoole 也是一样

小阳光

两者都可以,各有优点

  • 暂无评论
lobtao

其实性能差不多,workerman更稳定,发现问题,更易维护

  • 暂无评论
641221621

workerman 更简单稳定。 现在swoole 已经出商业版了

  • xiaoxing 2020-04-07

    哈哈,考虑了很久还是用workerman , swoole目前太乱了

  • 641221621 2021-09-28

    是的 生产环境稳定毕竟用的php原生东西 swoole玩了一段时间 坑太多了

Alex黑白

二者都是多进程模型;你的测试是纯粹的http req->resp 无io的场景,
1.workerman的优势是纯php(socket+pcntl+posix+可选的event)扩展实现,源码可读,phper阅读可以心中更有底;不选装event扩展的类unix机器可以使用select模型,少量连接的场景下select的表现略微要优与epoll,但是连接数上来后,epoll的表现会优与select;缺点是耦合比较高,需要集成,如果把整个workerman项目单独提供服务,倒可忽略这个缺点;还有一个缺点就是同步阻塞的io, 比如使用mysql连接
2.swoole的优势是是纯c++实现,作为扩展提供,侵入比较低,借鉴并实现了类似go语言优秀的协程大杀器,提供各种协程客户端,遇到io自动切换,一键协程化,底层自动hook,原来的1+1+1=3s的io场景变成了max(1,1,1)= 1s 的场景;缺点是:对于不懂c++的phper来说,心中可能会没有底;
总结:二者都是php生态下常驻内存cli模式下的优秀产物;建议结合socket+pcntl+posix+event学习workerman源码,swoole高版本可以生产试水,workerman可以作为保底,个人见解,勿喷哈

  • 暂无评论
天道酬勤

这个是一个专门做压测的第三方机构的2020年4月20号的压测结果,压测里包含了带数据库io的,包括单查、多查、多更新等

https://www.techempower.com/benchmarks/#section=test&runid=832828ba-3e18-415d-b492-461260ddc510&hw=ph&test=plaintext&l=zik073-3j

截图

从跑分来每个指标workerman都比swoole略高一些,总体来看看swoole和workerman结果差不多。

不过这仅仅是跑分,真实效果还是要看实际项目。

www

swoole在tcp协议大包收发100万次以上,有时候会发生服务无响应,我用的最新版测试的,不知怎么解决

  • 暂无评论
轩聆烨

我觉得是不是都搞错了对比双方
workman是PHP写的,但本质是用pcntl等扩展实现了功能
swoole是C/C++写的,但实际还是以PHP扩展形式出现
所以其实都是PHP扩展
所以其实比的是pcntl等扩展和swoole扩展的效能
所以workman不慢不是很正常么?

  • 暂无评论
胡方方

swoole 由于支持协程 并发性能要优于workerman吧,单纯的压测应该差不多,使用swoole可以用协程的方式写代码,workerman做不到,所以如果swoole用协程和workerman对比的话,workerman应该不行

小阳光

如果有慢查询,或者网络io较多,swoole的协程应该会更快。可以尝试一下。如果光是打印helloworld 可能都差不多。

  • 暂无评论
tcxl000

Swoole的部署折腾了我一个礼拜,终于能跑通了,再想想还要很多第三方类库和扩展要继续折腾,还是算了。
Gatewayworker直接用XFTP工具把文件夹拖进去,然后start就好了,省心多了。
对于我这种小白来说,不考虑什么集群,什么上千万的并发,什么容器,我选Gatewaywroker。

  • 暂无评论
alpha

workerman里可以用swoole协程,那这样是不是能飞起来?

  • 不败少龙 2021-08-20

    之前有Swoole模块的,好像后面删除了 之前的版本有

年代过于久远,无法发表回答
🔝