def callback(self, msg): if self.timer.nr == 1: self.condition.acquire() self.timer.watch = zmq.Stopwatch() self.timer.watch.start() self.timer.nr += 1 elif self.timer.nr == self.message_count: self.timer.elapsed = self.timer.watch.stop() self.condition.notifyAll() self.condition.release() else: self.timer.nr += 1 return msg
def latency(url, count, size, poll, copy): """Perform a latency test""" ctx = zmq.Context() s = ctx.socket(zmq.REQ) s.setsockopt(zmq.LINGER, -1) s.connect(url) if poll: p = zmq.Poller() p.register(s) msg = b' ' * size watch = zmq.Stopwatch() block = zmq.NOBLOCK if poll else 0 time.sleep(1) watch.start() for i in range(0, count): if poll: res = p.poll() assert (res[0][1] & zmq.POLLOUT) s.send(msg, block, copy=copy) if poll: res = p.poll() assert (res[0][1] & zmq.POLLIN) msg = s.recv(block, copy=copy) assert len(msg) == size elapsed = watch.stop() s.send(b'done') latency = elapsed / (count * 2.) print("message size : %8i [B]" % (size, )) print("roundtrip count: %8i [msgs]" % (count, )) print("mean latency : %12.3f [µs]" % (latency, )) print("test time : %12.3f [s]" % (elapsed * 1e-6, ))
def throughput(url, count, size, poll, copy): """recv a bunch of messages on a PULL socket Should be started before `pusher` """ ctx = zmq.Context() s = ctx.socket(zmq.PULL) # Add your socket options here. # For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. if poll: p = zmq.Poller() p.register(s) s.bind(url) watch = zmq.Stopwatch() block = zmq.NOBLOCK if poll else 0 # Wait for the other side to connect. msg = s.recv() assert len(msg) == size watch.start() for i in range(count - 1): if poll: res = p.poll() msg = s.recv(block, copy=copy) elapsed = watch.stop() if elapsed == 0: elapsed = 1 throughput = (1e6 * float(count)) / float(elapsed) megabits = float(throughput * size * 8) / 1e6 print("message size : %8i [B]" % (size, )) print("message count : %8i [msgs]" % (count, )) print("mean throughput: %8.0f [msg/s]" % (throughput, )) print("mean throughput: %12.3f [Mb/s]" % (megabits, )) print("test time : %12.3f [s]" % (elapsed * 1e-6, ))
except (ValueError, OverflowError), e: print 'message-size and message-count must be integers' sys.exit(1) ctx = zmq.Context() s = ctx.socket(zmq.REQ) print connect_to s.connect(connect_to) if use_poll: p = zmq.Poller() p.register(s) msg = ' ' * message_size clock = zmq.Stopwatch() clock.start() start = 0 # start = time.clock() for i in range(0, roundtrip_count): if use_poll: res = p.poll() assert (res[0][1] & zmq.POLLOUT) s.send(msg, zmq.NOBLOCK if use_poll else 0, copy=use_copy) if use_poll: res = p.poll() assert (res[0][1] & zmq.POLLIN) msg = s.recv(zmq.NOBLOCK if use_poll else 0, copy=use_copy) assert len(msg) == message_size
def main (argv): use_poll = '-p' in argv use_copy = '-c' in argv if use_copy: argv.remove('-c') if use_poll: argv.remove('-p') if len (argv) != 4: print ('usage: local_thr [-c use-copy] [-p use-poll] <bind-to> <message-size> <message-count>') sys.exit(1) try: bind_to = argv[1] message_size = int(argv[2]) message_count = int(argv[3]) except (ValueError, OverflowError): print ('message-size and message-count must be integers') sys.exit(1) ctx = zmq.Context() s = ctx.socket(zmq.SUB) # Add your socket options here. # For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. # remove the b for Python2.5: s.setsockopt(zmq.SUBSCRIBE , b'') if use_poll: p = zmq.Poller() p.register(s) s.bind(bind_to) watch = zmq.Stopwatch() block = zmq.NOBLOCK if use_poll else 0 # Wait for the other side to connect. msg = s.recv() assert len (msg) == message_size watch.start() for i in range (message_count-1): if use_poll: res = p.poll() assert(res[0][1] & zmq.POLLIN) msg = s.recv(block, copy=use_copy) elapsed = watch.stop() if elapsed == 0: elapsed = 1 throughput = (1e6 * float(message_count)) / float(elapsed) megabits = float(throughput * message_size * 8) / 1e6 print ("message size: %i [B]" % (message_size, )) print ("message count: %i" % (message_count, )) print ("mean throughput: %.0f [msg/s]" % (throughput, )) print ("mean throughput: %.3f [Mb/s]" % (megabits, )) s.close() ctx.term() return throughput