benchmarks.py 5.5 KB

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