swoole和workerman性能对比

0

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)

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

已邀请:

censc

赞同来自:

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

张先生

赞同来自:

两者都可以,各有优点

lobtao - 年轻没有失败,勤奋创造未来

赞同来自:

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

641221621 - workerman!

赞同来自:

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

Alex黑白 - 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 可能都差不多。

要回复问题请先登录注册