swoole是c/c++寫的網(wǎng)絡(luò)通訊擴(kuò)展,workerman是純php寫的網(wǎng)絡(luò)框架,兩者功能類似。我看到網(wǎng)上很多評論說swoole性能比workerman高不少,甚至還看到知乎有說workerman和swoole比不在一個(gè)數(shù)量級的。但是為什么我的壓測結(jié)果卻不是這樣,不管是我的mac pro還是剛剛阿里云服務(wù)器,二者性能差不多甚至有時(shí)workerman要比swoole壓測性能高一些?以下是我的壓測腳本和結(jié)果。
環(huán)境 阿里云: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 結(jié)果:
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結(jié)果:
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)
二者性能基本相近,沒有特別大的區(qū)別。有興趣的可以自己試下上面的壓測腳本,看看結(jié)果是不是和我一致。
---------2021-08-03分割線----------
發(fā)現(xiàn)這里有個(gè)權(quán)威的壓測網(wǎng)站,有各種框架壓測結(jié)果對比。有帶業(yè)務(wù)的,有不帶的。參見:https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=json&l=zik073-sf
沒有帶業(yè)務(wù)的測試是沒什么用的,有時(shí)候你的電腦某個(gè)進(jìn)程抽了呢。也會(huì)影響測試結(jié)果的對吧 就這么簡單的測試沒啥意義 在業(yè)務(wù)里使用然后對比
上面結(jié)果是多次測試各自采用的最佳結(jié)果。helloworld對比還是很有意義的,至少說明框架自身性能很好,框架自身不會(huì)成為瓶頸。如果連helloworld都不行,那上業(yè)務(wù)就更不行了啊。
@6237: 說swoole比workerman好,是因?yàn)樵u論的人沒用過workerman,他只是片面的認(rèn)為c++比php快,就認(rèn)為其程序框架也是這樣。
然而,workerman只載入解析一次php文件,然后便常駐內(nèi)存,極大的減少了磁盤IO及PHP中請求初始化、創(chuàng)建執(zhí)行環(huán)境、詞法解析、語法解析、編譯opcode、請求關(guān)閉等諸多耗時(shí)過程。所以純碎從語言層面比較,更應(yīng)該是c++和機(jī)器碼的區(qū)別。
兩者都是非常優(yōu)秀的開源框架,只是看自己更喜歡折騰c++還是php罷了
二者都是多進(jìn)程模型;你的測試是純粹的http req->resp 無io的場景,
1.workerman的優(yōu)勢是純php(socket+pcntl+posix+可選的event)擴(kuò)展實(shí)現(xiàn),源碼可讀,phper閱讀可以心中更有底;不選裝event擴(kuò)展的類unix機(jī)器可以使用select模型,少量連接的場景下select的表現(xiàn)略微要優(yōu)與epoll,但是連接數(shù)上來后,epoll的表現(xiàn)會(huì)優(yōu)與select;缺點(diǎn)是耦合比較高,需要集成,如果把整個(gè)workerman項(xiàng)目單獨(dú)提供服務(wù),倒可忽略這個(gè)缺點(diǎn);還有一個(gè)缺點(diǎn)就是同步阻塞的io, 比如使用mysql連接
2.swoole的優(yōu)勢是是純c++實(shí)現(xiàn),作為擴(kuò)展提供,侵入比較低,借鑒并實(shí)現(xiàn)了類似go語言優(yōu)秀的協(xié)程大殺器,提供各種協(xié)程客戶端,遇到io自動(dòng)切換,一鍵協(xié)程化,底層自動(dòng)hook,原來的1+1+1=3s的io場景變成了max(1,1,1)= 1s 的場景;缺點(diǎn)是:對于不懂c++的phper來說,心中可能會(huì)沒有底;
總結(jié):二者都是php生態(tài)下常駐內(nèi)存cli模式下的優(yōu)秀產(chǎn)物;建議結(jié)合socket+pcntl+posix+event學(xué)習(xí)workerman源碼,swoole高版本可以生產(chǎn)試水,workerman可以作為保底,個(gè)人見解,勿噴哈
這個(gè)是一個(gè)專門做壓測的第三方機(jī)構(gòu)的2020年4月20號的壓測結(jié)果,壓測里包含了帶數(shù)據(jù)庫io的,包括單查、多查、多更新等
從跑分來每個(gè)指標(biāo)workerman都比swoole略高一些,總體來看看swoole和workerman結(jié)果差不多。
不過這僅僅是跑分,真實(shí)效果還是要看實(shí)際項(xiàng)目。
swoole在tcp協(xié)議大包收發(fā)100萬次以上,有時(shí)候會(huì)發(fā)生服務(wù)無響應(yīng),我用的最新版測試的,不知怎么解決
我覺得是不是都搞錯(cuò)了對比雙方
workman是PHP寫的,但本質(zhì)是用pcntl等擴(kuò)展實(shí)現(xiàn)了功能
swoole是C/C++寫的,但實(shí)際還是以PHP擴(kuò)展形式出現(xiàn)
所以其實(shí)都是PHP擴(kuò)展
所以其實(shí)比的是pcntl等擴(kuò)展和swoole擴(kuò)展的效能
所以workman不慢不是很正常么?
swoole 由于支持協(xié)程 并發(fā)性能要優(yōu)于workerman吧,單純的壓測應(yīng)該差不多,使用swoole可以用協(xié)程的方式寫代碼,workerman做不到,所以如果swoole用協(xié)程和workerman對比的話,workerman應(yīng)該不行
https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=db&l=zik073-1r
這個(gè)是國外權(quán)威壓測機(jī)構(gòu) www.techempower.com 第19輪壓測,帶數(shù)據(jù)庫業(yè)務(wù)的,swoole用了數(shù)據(jù)庫連接池+協(xié)程,workerman就是原始php mysql阻塞用法,結(jié)論仍然是二者性能差別不大,各項(xiàng)性能指標(biāo)均是workerman好些,尤其是plaintext,比swoole高35%左右。
如果有慢查詢,或者網(wǎng)絡(luò)io較多,swoole的協(xié)程應(yīng)該會(huì)更快。可以嘗試一下。如果光是打印helloworld 可能都差不多。
Swoole的部署折騰了我一個(gè)禮拜,終于能跑通了,再想想還要很多第三方類庫和擴(kuò)展要繼續(xù)折騰,還是算了。
Gatewayworker直接用XFTP工具把文件夾拖進(jìn)去,然后start就好了,省心多了。
對于我這種小白來說,不考慮什么集群,什么上千萬的并發(fā),什么容器,我選Gatewaywroker。