使用cluster實(shí)現(xiàn)多線程

1: 新創(chuàng)建一個(gè)master.js,代碼如下:

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

  if(cluster.isMaster){
    console.log('[master] ' + "start master...");

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

    cluster.on('listening', function(worker, address){
      console.log('[master] ' + 'listening: worker' + worker.id + ',pid:' +       worker.process.pid + ', Address:' + address.address + ":" + address.port);
    });
  }else if(cluster.isWorker){
    console.log('[worker] ' + "start worker ..." + cluster.worker.id);
    require('./app.js');
  }

通過(guò)上面的代碼 可以有多個(gè)線程同時(shí)開(kāi)啟 訪問(wèn)app.js,結(jié)果如圖:

屏幕快照 2016-12-19 下午9.52.00.png

2: 進(jìn)行壓力測(cè)試的工具 選用的是siege

  • 安裝siege

    brew install siege
    
  • 使用siege測(cè)試

    siege -c 100 -r 2 http://localhost:4000
    
  • siege各部分參數(shù)簡(jiǎn)單說(shuō)明

    Transactions: 30000 hits //完成30000次處理
    Availability: 100.00 % //100.00 % 成功率
    Elapsed time: 68.59 secs //總共使用時(shí)間
    Data transferred: 817.76 MB //共數(shù)據(jù)傳輸 817.76 
    MBResponse time: 0.04 secs //響應(yīng)時(shí)間,顯示網(wǎng)絡(luò)連接的速度                     
    Transaction rate: 437.38 trans/sec //平均每秒完成 437.38 次處
    Throughput: 11.92 MB/sec //平均每秒傳送數(shù)據(jù)
    Concurrency: 17.53 //實(shí)際最高并發(fā)連接數(shù)
    Successful transactions: 30000 //成功處理次數(shù)
    Failed transactions: 0 //失敗處理次數(shù)
    Longest transaction: 3.12 //每次傳輸所花最長(zhǎng)時(shí)間
    Shortest transaction: 0.00 //每次傳輸所花最短時(shí)間
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容