10

我正在涉足进程间通信;目的是让工作进程执行一些计算并将结果传递回控制进程。我安装了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 实验的大致相同。

这些数字是预期的吗?这里的限制因素是什么?

4

1 回答 1

7

我认为有两件事正在发生。首先,你有setInterval ( -> socket.send 'helo world' ), 1. 这每毫秒发送一个请求,因此您将被限制为每秒 1000 个请求。

此外,请求/响应模型是同步的。请求进入套接字并阻塞,直到给出响应。有了响应,下一个请求就被处理和阻塞了。

我将请求者脚本从socket = zmq.socket 'req'tosocket = zmq.socket 'push'setInterval ( -> socket.send 'helo world' ), 0to更改为socket.send 'helo world' while true. 然后我将响应脚本更改socket = zmq.socket 'rep'socket = zmq.socket 'pull'并得到了这个输出

$ coffee responder.coffee
Responder bound to port 5555
received 282 messages per second
received 333357 messages per second
received 249988 messages per second
received 333331 messages per second
received 250003 messages per second
received 333331 messages per second
received 333331 messages per second
received 333331 messages per second
...

请求者没有得到输出,因为它阻塞了 while 循环,但它确实证明了 ØMQ 可以获得更好的性能。

于 2013-05-06T23:06:44.003 回答