我正在涉足进程间通信;目的是让工作进程执行一些计算并将结果传递回控制进程。我安装了zeromq.node并在 coffeescript 中设置了一个简单的请求者和响应者。
请求者:
# requester.coffee
zmq = require 'zmq'
context = new zmq.Context()
socket = zmq.socket 'req'
socket.bind 'tcp://127.0.0.1:5555', ( error ) =>
throw error if error?
console.log 'Requesting writer bound to port 5555'
setInterval ( -> socket.send 'helo world' ), 1
response_count = 0
t0 = new Date() / 1000
socket.on 'message', ( message ) ->
response_count += 1
# x = message.toString 'utf-8'
if response_count % 1000 is 0
t1 = new Date() / 1000
console.log "received #{ parseInt response_count / ( t1 - t0 ) + 0.5 } messages per second"
response_count = 0
t0 = new Date() / 1000
响应者:
# responder.coffee
zmq = require 'zmq'
context = new zmq.Context()
socket = zmq.socket 'rep'
socket.connect 'tcp://127.0.0.1:5555'
console.log 'Responder bound to port 5555'
process.stdin.resume()
request_count = 0
t0 = new Date() / 1000
socket.on 'message', ( message ) ->
request_count += 1
# message = message.toString 'utf-8'
# console.log message
socket.send 'helo back'
if request_count % 1000 is 0
t1 = new Date() / 1000
console.log "received #{ parseInt request_count / ( t1 - t0 ) + 0.5 } messages per second"
request_count = 0
t0 = new Date() / 1000
现在,当我在我的 ubuntu(11.10、8GB、Intel Duo Core 3GHz、NodeJS 0.8.6)机器上的单独终端窗口中运行它们时,我得到以下输出:
received 135 messages per second
received 6369 messages per second
received 6849 messages per second
received 6944 messages per second
received 7042 messages per second
received 7143 messages per second
received 5952 messages per second
received 2967 messages per second
received 914 messages per second
received 912 messages per second
received 928 messages per second
received 919 messages per second
received 947 messages per second
received 906 messages per second
received 918 messages per second
received 929 messages per second
received 916 messages per second
received 917 messages per second
received 916 messages per second
received 928 messages per second
其中(1)看起来有点像几秒钟后传输通道中存在某种饱和;(2) 感觉不够快。根据这个基准,我应该在每秒数十万而不是数千条消息中,这一点得到了这个讨论的证实(“ZeroMQ:接收 10,000 条消息需要大约 15 毫秒”)。
我还尝试使用用 python 3 编写的响应器并得到完全相同的数字。更重要的是,我编写了一对替代脚本,主进程将生成一个子进程并通过标准输出/标准输入与它进行通信;我每秒获得大约 750 条消息(当我增加消息长度时,我看不到太大的差异),这与 zeromq 实验的大致相同。
这些数字是预期的吗?这里的限制因素是什么?