2 # Simple benchmarks for the multiprocessing package
5 import time
, sys
, multiprocessing
, threading
, Queue
, gc
7 if sys
.platform
== 'win32':
17 def queuespeed_func(q
, c
, iterations
):
23 for i
in xrange(iterations
):
28 def test_queuespeed(Process
, q
, c
):
32 while elapsed
< delta
:
35 p
= Process(target
=queuespeed_func
, args
=(q
, c
, iterations
))
44 while result
!= 'STOP':
47 elapsed
= _timer() - t
51 print iterations
, 'objects passed through the queue in', elapsed
, 'seconds'
52 print 'average number/sec:', iterations
/elapsed
57 def pipe_func(c
, cond
, iterations
):
63 for i
in xrange(iterations
):
69 c
, d
= multiprocessing
.Pipe()
70 cond
= multiprocessing
.Condition()
74 while elapsed
< delta
:
77 p
= multiprocessing
.Process(target
=pipe_func
,
78 args
=(d
, cond
, iterations
))
87 while result
!= 'STOP':
90 elapsed
= _timer() - t
93 print iterations
, 'objects passed through connection in',elapsed
,'seconds'
94 print 'average number/sec:', iterations
/elapsed
99 def test_seqspeed(seq
):
103 while elapsed
< delta
:
108 for i
in xrange(iterations
):
113 print iterations
, 'iterations in', elapsed
, 'seconds'
114 print 'average number/sec:', iterations
/elapsed
119 def test_lockspeed(l
):
123 while elapsed
< delta
:
128 for i
in xrange(iterations
):
134 print iterations
, 'iterations in', elapsed
, 'seconds'
135 print 'average number/sec:', iterations
/elapsed
140 def conditionspeed_func(c
, N
):
150 def test_conditionspeed(Process
, c
):
154 while elapsed
< delta
:
158 p
= Process(target
=conditionspeed_func
, args
=(c
, iterations
))
165 for i
in xrange(iterations
):
174 print iterations
* 2, 'waits in', elapsed
, 'seconds'
175 print 'average number/sec:', iterations
* 2 / elapsed
180 manager
= multiprocessing
.Manager()
184 print '\n\t######## testing Queue.Queue\n'
185 test_queuespeed(threading
.Thread
, Queue
.Queue(),
186 threading
.Condition())
187 print '\n\t######## testing multiprocessing.Queue\n'
188 test_queuespeed(multiprocessing
.Process
, multiprocessing
.Queue(),
189 multiprocessing
.Condition())
190 print '\n\t######## testing Queue managed by server process\n'
191 test_queuespeed(multiprocessing
.Process
, manager
.Queue(),
193 print '\n\t######## testing multiprocessing.Pipe\n'
198 print '\n\t######## testing list\n'
199 test_seqspeed(range(10))
200 print '\n\t######## testing list managed by server process\n'
201 test_seqspeed(manager
.list(range(10)))
202 print '\n\t######## testing Array("i", ..., lock=False)\n'
203 test_seqspeed(multiprocessing
.Array('i', range(10), lock
=False))
204 print '\n\t######## testing Array("i", ..., lock=True)\n'
205 test_seqspeed(multiprocessing
.Array('i', range(10), lock
=True))
209 print '\n\t######## testing threading.Lock\n'
210 test_lockspeed(threading
.Lock())
211 print '\n\t######## testing threading.RLock\n'
212 test_lockspeed(threading
.RLock())
213 print '\n\t######## testing multiprocessing.Lock\n'
214 test_lockspeed(multiprocessing
.Lock())
215 print '\n\t######## testing multiprocessing.RLock\n'
216 test_lockspeed(multiprocessing
.RLock())
217 print '\n\t######## testing lock managed by server process\n'
218 test_lockspeed(manager
.Lock())
219 print '\n\t######## testing rlock managed by server process\n'
220 test_lockspeed(manager
.RLock())
224 print '\n\t######## testing threading.Condition\n'
225 test_conditionspeed(threading
.Thread
, threading
.Condition())
226 print '\n\t######## testing multiprocessing.Condition\n'
227 test_conditionspeed(multiprocessing
.Process
, multiprocessing
.Condition())
228 print '\n\t######## testing condition managed by a server process\n'
229 test_conditionspeed(multiprocessing
.Process
, manager
.Condition())
233 if __name__
== '__main__':
234 multiprocessing
.freeze_support()