Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. from concurrent.futures import ProcessPoolExecutor
  2. import os
  3. import time
  4.  
  5. import zmq
  6.  
  7. NMSGS = 1000000
  8. MSG_SIZE = 200
  9. URL = 'ipc://pyzmq-test.sock'
  10. # URL = 'tcp://127.0.0.1:5555'
  11.  
  12. def puller(n):
  13. ctx = zmq.Context()
  14. pull = ctx.socket(zmq.PULL)
  15. pull.bind(URL)
  16. tic = time.time()
  17. for i in range(n):
  18. pull.recv()
  19. toc = time.time()
  20. pull.close()
  21. ctx.term()
  22. return toc
  23.  
  24. def pusher(n):
  25. ctx = zmq.Context()
  26. push = ctx.socket(zmq.PUSH)
  27. msg = os.urandom(MSG_SIZE)
  28. push.connect(URL)
  29. tic = time.time()
  30. for i in range(n):
  31. push.send(msg)
  32. push.close()
  33. ctx.term()
  34. return tic
  35.  
  36. pool = ProcessPoolExecutor(1)
  37. t = 0
  38. n = 64
  39.  
  40. # keep running until a sample takes at least a few seconds
  41. print("%8s / %8s = %8s msgs/sec" % ('n msgs', 't (sec)', ''))
  42. while t < 4:
  43. f = pool.submit(puller, n)
  44. time.sleep(1)
  45. start = pusher(n)
  46. stop = f.result()
  47. t = stop - start
  48. msgs_sec = n / t
  49. print("%8i / %8.3g = %8i msgs/sec = %5i MB/sec" % (n, t, msgs_sec, 1e-6 * msgs_sec * MSG_SIZE))
  50. n *= 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement