1. 程式人生 > >workerman和nodejs自測效能對比

workerman和nodejs自測效能對比

    根據目前的專案要求,需要找一個合適的框架做api/rpc服務,以前做web專案選型的ci框架,經測試發現使用web框架做api/rpc並不實際;原因是,框架中呼叫了很多的model,其實在api/RPC中並不真正需要,載入的model一多而且很影響效能;

   本來做php開發的,想從php中選型一個好的socket框架的程式來實現。後來查詢資料發現php中確實存在這種框架:swoole,workman ,兩個框架都進行了下載和測試,發現還是workerman 文件,案例,測試工具和程式碼規範度等都比較齊全,而且效能比較優越,有圖表分析功能,載入框架能跑到1w以上的併發每秒。

  後有同事建議採用nodejs 說nodejs效能高,開始對nodejs測試了下做了下對比,剛開始使用nodejs單程序跑helloword,能跑到5000每秒,後覺得效能應該不止這樣後代碼進行調整成使用多程序方式,併發提升了一杯 ,這裡要說明下,我採用的測試伺服器為雙核伺服器

nodejs 測試程式碼:

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    console.log("master start...");

    // Fork workers.
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('listening',function(worker,address){
        console.log('listening: worker ' + worker.process.pid +', Address: '+address.address+":"+address.port);
    });

    cluster.on('exit', function(worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died');
    });
} else {
    http.createServer(function(req, res) {
        res.writeHead(200);
        res.end("hello world\n");
    }).listen(0);

}
~
~
~

 以下是測試結果:
[[email protected] ~]# webbench -c 1000 -t 30 http://xxx.xx.xx.xxx:44354/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://xxx.xx.xx.xxx:44354/
1000 clients, running 30 sec.

Speed=666260 pages/min, 966068 bytes/sec.
Requests: 333130 susceed, 0 failed.

值得注意的是: 伺服器的計算能力和cpu的核心有關,我提升程序的個數,其實沒有達到什麼作用,併發一直保持 66w 每分鐘左右

---------------

workerman 我同樣進行壓測:

[[email protected] ~]# webbench -c 1000 -t 30 http://xxx.xx.xx.xxx:2015/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://xxx.xx.xx.xxx:2015/
1000 clients, running 30 sec.
Speed=675500 pages/min, 1640655 bytes/sec.
Requests: 337750 susceed, 0 failed.
可以看出,在效能差不多,在直接丟擲helloword的情況下。當然以上測是是對框架進行測試並沒有實際對程式程式碼效能進行測試。

不過可以看出一點,如果你在使用php開發workerman框架,是一個不錯的選擇,當然nodejs也不差,畢竟很多大型企業都開始使用。