顯示廣告
隱藏 ✕
※ 本文為 dinos 轉寄自 ptt.cc 更新時間: 2013-06-03 18:11:07
看板 PHP
作者 rickysu (Ricky)
標題 Re: [請益] 兩個 socket server 之間的溝通?
時間 Mon Jun  3 17:29:17 2013


要 keep 大量 connection,以及快速 response ,
使用 socket 函數幾乎是不可行的方式。
傳統的 socket select,底層還是透過 polling 方式去監視 socket 狀態。
一旦連線數多的時候,效能就直直落。

目前比較有效率的作法是透過 linux 本身的 epoll 或是 freebsd 的 kqueue,
當然也有善心人士封裝成 libevent 可供使用。

php本身也有善心人士將 libevent 打包成 extension。

http://pecl.php.net/package/libevent

http://pecl.php.net/package/event

pecl libevent 的 bug 還頗多,倒是 pecl event 實作的還挺不錯的。

效能更是嚇嚇叫,前陣子實際測試的結果,node.js幾乎是被打假的。

以下是一個簡單的 http server
<?php
$base = new EventBase();
$http = new EventHttp($base);
$http->bind('127.0.0.1', 8080);
$http->setDefaultCallback(function($req) {
    $buffer = $req->getOutputBuffer();
    $buffer->add("hello world");
    $req->sendReply(200,"ok", $buffer);
});

$base->dispatch();

實際用 ab -c 1000 -n 1000 http://localhost:8080/
pecl event    Requests per second:    17247.92  (mean)

使用 nodejs
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("hello world");
}).listen(8080);

nodejs        Requests per second:    3711.91  (mean)

硬體 Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz

就參考看看吧

另外 ZMQ 也是一個效能挺不錯的 lib,如果要同時對多個節點廣播時,效能也是
嚇死人。


--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.130.136.115

--
※ 看板: dinos 文章推薦值: 0 目前人氣: 0 累積人氣: 336 
guest
x)推文 r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇