You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.5 KiB

Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r67348 | benjamin.peterson | 2008-11-22 20:09:41 -0600 (Sat, 22 Nov 2008) | 1 line raise a better error ........ r67355 | georg.brandl | 2008-11-23 13:17:25 -0600 (Sun, 23 Nov 2008) | 2 lines #4392: fix parameter name. ........ r67359 | georg.brandl | 2008-11-23 15:57:30 -0600 (Sun, 23 Nov 2008) | 2 lines #4399: fix typo. ........ r67362 | gregory.p.smith | 2008-11-23 18:41:43 -0600 (Sun, 23 Nov 2008) | 2 lines Document PY_SSIZE_T_CLEAN for PyArg_ParseTuple. ........ r67364 | benjamin.peterson | 2008-11-23 19:16:29 -0600 (Sun, 23 Nov 2008) | 2 lines replace reference to debugger-hooks ........ r67365 | benjamin.peterson | 2008-11-23 22:09:03 -0600 (Sun, 23 Nov 2008) | 1 line #4396 make the parser module correctly validate the with syntax ........ r67367 | georg.brandl | 2008-11-24 10:16:07 -0600 (Mon, 24 Nov 2008) | 2 lines Fix typo. ........ r67368 | georg.brandl | 2008-11-24 13:56:47 -0600 (Mon, 24 Nov 2008) | 2 lines #4404: make clear what "path" is. ........ r67398 | benjamin.peterson | 2008-11-26 11:39:17 -0600 (Wed, 26 Nov 2008) | 1 line fix typo in sqlite3 docs ........ r67423 | jesse.noller | 2008-11-28 12:59:35 -0600 (Fri, 28 Nov 2008) | 2 lines issue4238: bsd support for cpu_count ........ r67424 | christian.heimes | 2008-11-28 13:33:33 -0600 (Fri, 28 Nov 2008) | 1 line Retain copyright of processing examples. This was requested by a Debian maintainer during packaging of the multiprocessing package for 2.4/2.5 ........ r67432 | benjamin.peterson | 2008-11-28 17:18:46 -0600 (Fri, 28 Nov 2008) | 1 line SVN format 9 is the same it seems ........ r67440 | jeremy.hylton | 2008-11-28 17:42:59 -0600 (Fri, 28 Nov 2008) | 4 lines Move definition int sval into branch of ifdef where it is used. Otherwise, you get a warning about an undefined variable. ........ r67441 | jeremy.hylton | 2008-11-28 18:09:16 -0600 (Fri, 28 Nov 2008) | 2 lines Reflow long lines. ........ r67444 | amaury.forgeotdarc | 2008-11-28 20:03:32 -0600 (Fri, 28 Nov 2008) | 2 lines Fix a small typo in docstring ........ r67445 | benjamin.peterson | 2008-11-29 21:07:33 -0600 (Sat, 29 Nov 2008) | 1 line StringIO.close() stops you from using the buffer, too ........ r67454 | benjamin.peterson | 2008-11-30 08:43:23 -0600 (Sun, 30 Nov 2008) | 1 line note the version that works ........ r67455 | martin.v.loewis | 2008-11-30 13:28:27 -0600 (Sun, 30 Nov 2008) | 1 line Issue #4365: Add crtassem.h constants to the msvcrt module. ........ r67457 | christian.heimes | 2008-11-30 15:16:28 -0600 (Sun, 30 Nov 2008) | 1 line w# requires Py_ssize_t ........ r67458 | benjamin.peterson | 2008-11-30 15:46:16 -0600 (Sun, 30 Nov 2008) | 1 line fix pyspecific extensions that were broken by Sphinx's grand renaming ........
18 years ago
  1. #
  2. # Simple benchmarks for the multiprocessing package
  3. #
  4. # Copyright (c) 2006-2008, R Oudkerk
  5. # All rights reserved.
  6. #
  7. import time
  8. import sys
  9. import multiprocessing
  10. import threading
  11. import queue
  12. import gc
  13. if sys.platform == 'win32':
  14. _timer = time.clock
  15. else:
  16. _timer = time.time
  17. delta = 1
  18. #### TEST_QUEUESPEED
  19. def queuespeed_func(q, c, iterations):
  20. a = '0' * 256
  21. c.acquire()
  22. c.notify()
  23. c.release()
  24. for i in range(iterations):
  25. q.put(a)
  26. q.put('STOP')
  27. def test_queuespeed(Process, q, c):
  28. elapsed = 0
  29. iterations = 1
  30. while elapsed < delta:
  31. iterations *= 2
  32. p = Process(target=queuespeed_func, args=(q, c, iterations))
  33. c.acquire()
  34. p.start()
  35. c.wait()
  36. c.release()
  37. result = None
  38. t = _timer()
  39. while result != 'STOP':
  40. result = q.get()
  41. elapsed = _timer() - t
  42. p.join()
  43. print(iterations, 'objects passed through the queue in', elapsed, 'seconds')
  44. print('average number/sec:', iterations/elapsed)
  45. #### TEST_PIPESPEED
  46. def pipe_func(c, cond, iterations):
  47. a = '0' * 256
  48. cond.acquire()
  49. cond.notify()
  50. cond.release()
  51. for i in range(iterations):
  52. c.send(a)
  53. c.send('STOP')
  54. def test_pipespeed():
  55. c, d = multiprocessing.Pipe()
  56. cond = multiprocessing.Condition()
  57. elapsed = 0
  58. iterations = 1
  59. while elapsed < delta:
  60. iterations *= 2
  61. p = multiprocessing.Process(target=pipe_func,
  62. args=(d, cond, iterations))
  63. cond.acquire()
  64. p.start()
  65. cond.wait()
  66. cond.release()
  67. result = None
  68. t = _timer()
  69. while result != 'STOP':
  70. result = c.recv()
  71. elapsed = _timer() - t
  72. p.join()
  73. print(iterations, 'objects passed through connection in',elapsed,'seconds')
  74. print('average number/sec:', iterations/elapsed)
  75. #### TEST_SEQSPEED
  76. def test_seqspeed(seq):
  77. elapsed = 0
  78. iterations = 1
  79. while elapsed < delta:
  80. iterations *= 2
  81. t = _timer()
  82. for i in range(iterations):
  83. a = seq[5]
  84. elapsed = _timer() - t
  85. print(iterations, 'iterations in', elapsed, 'seconds')
  86. print('average number/sec:', iterations/elapsed)
  87. #### TEST_LOCK
  88. def test_lockspeed(l):
  89. elapsed = 0
  90. iterations = 1
  91. while elapsed < delta:
  92. iterations *= 2
  93. t = _timer()
  94. for i in range(iterations):
  95. l.acquire()
  96. l.release()
  97. elapsed = _timer() - t
  98. print(iterations, 'iterations in', elapsed, 'seconds')
  99. print('average number/sec:', iterations/elapsed)
  100. #### TEST_CONDITION
  101. def conditionspeed_func(c, N):
  102. c.acquire()
  103. c.notify()
  104. for i in range(N):
  105. c.wait()
  106. c.notify()
  107. c.release()
  108. def test_conditionspeed(Process, c):
  109. elapsed = 0
  110. iterations = 1
  111. while elapsed < delta:
  112. iterations *= 2
  113. c.acquire()
  114. p = Process(target=conditionspeed_func, args=(c, iterations))
  115. p.start()
  116. c.wait()
  117. t = _timer()
  118. for i in range(iterations):
  119. c.notify()
  120. c.wait()
  121. elapsed = _timer() - t
  122. c.release()
  123. p.join()
  124. print(iterations * 2, 'waits in', elapsed, 'seconds')
  125. print('average number/sec:', iterations * 2 / elapsed)
  126. ####
  127. def test():
  128. manager = multiprocessing.Manager()
  129. gc.disable()
  130. print('\n\t######## testing Queue.Queue\n')
  131. test_queuespeed(threading.Thread, queue.Queue(),
  132. threading.Condition())
  133. print('\n\t######## testing multiprocessing.Queue\n')
  134. test_queuespeed(multiprocessing.Process, multiprocessing.Queue(),
  135. multiprocessing.Condition())
  136. print('\n\t######## testing Queue managed by server process\n')
  137. test_queuespeed(multiprocessing.Process, manager.Queue(),
  138. manager.Condition())
  139. print('\n\t######## testing multiprocessing.Pipe\n')
  140. test_pipespeed()
  141. print()
  142. print('\n\t######## testing list\n')
  143. test_seqspeed(list(range(10)))
  144. print('\n\t######## testing list managed by server process\n')
  145. test_seqspeed(manager.list(list(range(10))))
  146. print('\n\t######## testing Array("i", ..., lock=False)\n')
  147. test_seqspeed(multiprocessing.Array('i', list(range(10)), lock=False))
  148. print('\n\t######## testing Array("i", ..., lock=True)\n')
  149. test_seqspeed(multiprocessing.Array('i', list(range(10)), lock=True))
  150. print()
  151. print('\n\t######## testing threading.Lock\n')
  152. test_lockspeed(threading.Lock())
  153. print('\n\t######## testing threading.RLock\n')
  154. test_lockspeed(threading.RLock())
  155. print('\n\t######## testing multiprocessing.Lock\n')
  156. test_lockspeed(multiprocessing.Lock())
  157. print('\n\t######## testing multiprocessing.RLock\n')
  158. test_lockspeed(multiprocessing.RLock())
  159. print('\n\t######## testing lock managed by server process\n')
  160. test_lockspeed(manager.Lock())
  161. print('\n\t######## testing rlock managed by server process\n')
  162. test_lockspeed(manager.RLock())
  163. print()
  164. print('\n\t######## testing threading.Condition\n')
  165. test_conditionspeed(threading.Thread, threading.Condition())
  166. print('\n\t######## testing multiprocessing.Condition\n')
  167. test_conditionspeed(multiprocessing.Process, multiprocessing.Condition())
  168. print('\n\t######## testing condition managed by a server process\n')
  169. test_conditionspeed(multiprocessing.Process, manager.Condition())
  170. gc.enable()
  171. if __name__ == '__main__':
  172. multiprocessing.freeze_support()
  173. test()