/Doc/includes/mp_benchmarks.py

http://unladen-swallow.googlecode.com/ · Python · 238 lines · 156 code · 70 blank · 12 comment · 16 complexity · 35219bfbf3399586a18bf9eb59b03f8b MD5 · raw file

  1. #
  2. # Simple benchmarks for the multiprocessing package
  3. #
  4. # Copyright (c) 2006-2008, R Oudkerk
  5. # All rights reserved.
  6. #
  7. import time, sys, multiprocessing, threading, Queue, gc
  8. if sys.platform == 'win32':
  9. _timer = time.clock
  10. else:
  11. _timer = time.time
  12. delta = 1
  13. #### TEST_QUEUESPEED
  14. def queuespeed_func(q, c, iterations):
  15. a = '0' * 256
  16. c.acquire()
  17. c.notify()
  18. c.release()
  19. for i in xrange(iterations):
  20. q.put(a)
  21. q.put('STOP')
  22. def test_queuespeed(Process, q, c):
  23. elapsed = 0
  24. iterations = 1
  25. while elapsed < delta:
  26. iterations *= 2
  27. p = Process(target=queuespeed_func, args=(q, c, iterations))
  28. c.acquire()
  29. p.start()
  30. c.wait()
  31. c.release()
  32. result = None
  33. t = _timer()
  34. while result != 'STOP':
  35. result = q.get()
  36. elapsed = _timer() - t
  37. p.join()
  38. print iterations, 'objects passed through the queue in', elapsed, 'seconds'
  39. print 'average number/sec:', iterations/elapsed
  40. #### TEST_PIPESPEED
  41. def pipe_func(c, cond, iterations):
  42. a = '0' * 256
  43. cond.acquire()
  44. cond.notify()
  45. cond.release()
  46. for i in xrange(iterations):
  47. c.send(a)
  48. c.send('STOP')
  49. def test_pipespeed():
  50. c, d = multiprocessing.Pipe()
  51. cond = multiprocessing.Condition()
  52. elapsed = 0
  53. iterations = 1
  54. while elapsed < delta:
  55. iterations *= 2
  56. p = multiprocessing.Process(target=pipe_func,
  57. args=(d, cond, iterations))
  58. cond.acquire()
  59. p.start()
  60. cond.wait()
  61. cond.release()
  62. result = None
  63. t = _timer()
  64. while result != 'STOP':
  65. result = c.recv()
  66. elapsed = _timer() - t
  67. p.join()
  68. print iterations, 'objects passed through connection in',elapsed,'seconds'
  69. print 'average number/sec:', iterations/elapsed
  70. #### TEST_SEQSPEED
  71. def test_seqspeed(seq):
  72. elapsed = 0
  73. iterations = 1
  74. while elapsed < delta:
  75. iterations *= 2
  76. t = _timer()
  77. for i in xrange(iterations):
  78. a = seq[5]
  79. elapsed = _timer()-t
  80. print iterations, 'iterations in', elapsed, 'seconds'
  81. print 'average number/sec:', iterations/elapsed
  82. #### TEST_LOCK
  83. def test_lockspeed(l):
  84. elapsed = 0
  85. iterations = 1
  86. while elapsed < delta:
  87. iterations *= 2
  88. t = _timer()
  89. for i in xrange(iterations):
  90. l.acquire()
  91. l.release()
  92. elapsed = _timer()-t
  93. print iterations, 'iterations in', elapsed, 'seconds'
  94. print 'average number/sec:', iterations/elapsed
  95. #### TEST_CONDITION
  96. def conditionspeed_func(c, N):
  97. c.acquire()
  98. c.notify()
  99. for i in xrange(N):
  100. c.wait()
  101. c.notify()
  102. c.release()
  103. def test_conditionspeed(Process, c):
  104. elapsed = 0
  105. iterations = 1
  106. while elapsed < delta:
  107. iterations *= 2
  108. c.acquire()
  109. p = Process(target=conditionspeed_func, args=(c, iterations))
  110. p.start()
  111. c.wait()
  112. t = _timer()
  113. for i in xrange(iterations):
  114. c.notify()
  115. c.wait()
  116. elapsed = _timer()-t
  117. c.release()
  118. p.join()
  119. print iterations * 2, 'waits in', elapsed, 'seconds'
  120. print 'average number/sec:', iterations * 2 / elapsed
  121. ####
  122. def test():
  123. manager = multiprocessing.Manager()
  124. gc.disable()
  125. print '\n\t######## testing Queue.Queue\n'
  126. test_queuespeed(threading.Thread, Queue.Queue(),
  127. threading.Condition())
  128. print '\n\t######## testing multiprocessing.Queue\n'
  129. test_queuespeed(multiprocessing.Process, multiprocessing.Queue(),
  130. multiprocessing.Condition())
  131. print '\n\t######## testing Queue managed by server process\n'
  132. test_queuespeed(multiprocessing.Process, manager.Queue(),
  133. manager.Condition())
  134. print '\n\t######## testing multiprocessing.Pipe\n'
  135. test_pipespeed()
  136. print
  137. print '\n\t######## testing list\n'
  138. test_seqspeed(range(10))
  139. print '\n\t######## testing list managed by server process\n'
  140. test_seqspeed(manager.list(range(10)))
  141. print '\n\t######## testing Array("i", ..., lock=False)\n'
  142. test_seqspeed(multiprocessing.Array('i', range(10), lock=False))
  143. print '\n\t######## testing Array("i", ..., lock=True)\n'
  144. test_seqspeed(multiprocessing.Array('i', range(10), lock=True))
  145. print
  146. print '\n\t######## testing threading.Lock\n'
  147. test_lockspeed(threading.Lock())
  148. print '\n\t######## testing threading.RLock\n'
  149. test_lockspeed(threading.RLock())
  150. print '\n\t######## testing multiprocessing.Lock\n'
  151. test_lockspeed(multiprocessing.Lock())
  152. print '\n\t######## testing multiprocessing.RLock\n'
  153. test_lockspeed(multiprocessing.RLock())
  154. print '\n\t######## testing lock managed by server process\n'
  155. test_lockspeed(manager.Lock())
  156. print '\n\t######## testing rlock managed by server process\n'
  157. test_lockspeed(manager.RLock())
  158. print
  159. print '\n\t######## testing threading.Condition\n'
  160. test_conditionspeed(threading.Thread, threading.Condition())
  161. print '\n\t######## testing multiprocessing.Condition\n'
  162. test_conditionspeed(multiprocessing.Process, multiprocessing.Condition())
  163. print '\n\t######## testing condition managed by a server process\n'
  164. test_conditionspeed(multiprocessing.Process, manager.Condition())
  165. gc.enable()
  166. if __name__ == '__main__':
  167. multiprocessing.freeze_support()
  168. test()