1 | n/a | # |
---|
2 | n/a | # Simple benchmarks for the multiprocessing package |
---|
3 | n/a | # |
---|
4 | n/a | # Copyright (c) 2006-2008, R Oudkerk |
---|
5 | n/a | # All rights reserved. |
---|
6 | n/a | # |
---|
7 | n/a | |
---|
8 | n/a | import time |
---|
9 | n/a | import multiprocessing |
---|
10 | n/a | import threading |
---|
11 | n/a | import queue |
---|
12 | n/a | import gc |
---|
13 | n/a | |
---|
14 | n/a | _timer = time.perf_counter |
---|
15 | n/a | |
---|
16 | n/a | delta = 1 |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | #### TEST_QUEUESPEED |
---|
20 | n/a | |
---|
21 | n/a | def queuespeed_func(q, c, iterations): |
---|
22 | n/a | a = '0' * 256 |
---|
23 | n/a | c.acquire() |
---|
24 | n/a | c.notify() |
---|
25 | n/a | c.release() |
---|
26 | n/a | |
---|
27 | n/a | for i in range(iterations): |
---|
28 | n/a | q.put(a) |
---|
29 | n/a | |
---|
30 | n/a | q.put('STOP') |
---|
31 | n/a | |
---|
32 | n/a | def test_queuespeed(Process, q, c): |
---|
33 | n/a | elapsed = 0 |
---|
34 | n/a | iterations = 1 |
---|
35 | n/a | |
---|
36 | n/a | while elapsed < delta: |
---|
37 | n/a | iterations *= 2 |
---|
38 | n/a | |
---|
39 | n/a | p = Process(target=queuespeed_func, args=(q, c, iterations)) |
---|
40 | n/a | c.acquire() |
---|
41 | n/a | p.start() |
---|
42 | n/a | c.wait() |
---|
43 | n/a | c.release() |
---|
44 | n/a | |
---|
45 | n/a | result = None |
---|
46 | n/a | t = _timer() |
---|
47 | n/a | |
---|
48 | n/a | while result != 'STOP': |
---|
49 | n/a | result = q.get() |
---|
50 | n/a | |
---|
51 | n/a | elapsed = _timer() - t |
---|
52 | n/a | |
---|
53 | n/a | p.join() |
---|
54 | n/a | |
---|
55 | n/a | print(iterations, 'objects passed through the queue in', elapsed, 'seconds') |
---|
56 | n/a | print('average number/sec:', iterations/elapsed) |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | #### TEST_PIPESPEED |
---|
60 | n/a | |
---|
61 | n/a | def pipe_func(c, cond, iterations): |
---|
62 | n/a | a = '0' * 256 |
---|
63 | n/a | cond.acquire() |
---|
64 | n/a | cond.notify() |
---|
65 | n/a | cond.release() |
---|
66 | n/a | |
---|
67 | n/a | for i in range(iterations): |
---|
68 | n/a | c.send(a) |
---|
69 | n/a | |
---|
70 | n/a | c.send('STOP') |
---|
71 | n/a | |
---|
72 | n/a | def test_pipespeed(): |
---|
73 | n/a | c, d = multiprocessing.Pipe() |
---|
74 | n/a | cond = multiprocessing.Condition() |
---|
75 | n/a | elapsed = 0 |
---|
76 | n/a | iterations = 1 |
---|
77 | n/a | |
---|
78 | n/a | while elapsed < delta: |
---|
79 | n/a | iterations *= 2 |
---|
80 | n/a | |
---|
81 | n/a | p = multiprocessing.Process(target=pipe_func, |
---|
82 | n/a | args=(d, cond, iterations)) |
---|
83 | n/a | cond.acquire() |
---|
84 | n/a | p.start() |
---|
85 | n/a | cond.wait() |
---|
86 | n/a | cond.release() |
---|
87 | n/a | |
---|
88 | n/a | result = None |
---|
89 | n/a | t = _timer() |
---|
90 | n/a | |
---|
91 | n/a | while result != 'STOP': |
---|
92 | n/a | result = c.recv() |
---|
93 | n/a | |
---|
94 | n/a | elapsed = _timer() - t |
---|
95 | n/a | p.join() |
---|
96 | n/a | |
---|
97 | n/a | print(iterations, 'objects passed through connection in',elapsed,'seconds') |
---|
98 | n/a | print('average number/sec:', iterations/elapsed) |
---|
99 | n/a | |
---|
100 | n/a | |
---|
101 | n/a | #### TEST_SEQSPEED |
---|
102 | n/a | |
---|
103 | n/a | def test_seqspeed(seq): |
---|
104 | n/a | elapsed = 0 |
---|
105 | n/a | iterations = 1 |
---|
106 | n/a | |
---|
107 | n/a | while elapsed < delta: |
---|
108 | n/a | iterations *= 2 |
---|
109 | n/a | |
---|
110 | n/a | t = _timer() |
---|
111 | n/a | |
---|
112 | n/a | for i in range(iterations): |
---|
113 | n/a | a = seq[5] |
---|
114 | n/a | |
---|
115 | n/a | elapsed = _timer() - t |
---|
116 | n/a | |
---|
117 | n/a | print(iterations, 'iterations in', elapsed, 'seconds') |
---|
118 | n/a | print('average number/sec:', iterations/elapsed) |
---|
119 | n/a | |
---|
120 | n/a | |
---|
121 | n/a | #### TEST_LOCK |
---|
122 | n/a | |
---|
123 | n/a | def test_lockspeed(l): |
---|
124 | n/a | elapsed = 0 |
---|
125 | n/a | iterations = 1 |
---|
126 | n/a | |
---|
127 | n/a | while elapsed < delta: |
---|
128 | n/a | iterations *= 2 |
---|
129 | n/a | |
---|
130 | n/a | t = _timer() |
---|
131 | n/a | |
---|
132 | n/a | for i in range(iterations): |
---|
133 | n/a | l.acquire() |
---|
134 | n/a | l.release() |
---|
135 | n/a | |
---|
136 | n/a | elapsed = _timer() - t |
---|
137 | n/a | |
---|
138 | n/a | print(iterations, 'iterations in', elapsed, 'seconds') |
---|
139 | n/a | print('average number/sec:', iterations/elapsed) |
---|
140 | n/a | |
---|
141 | n/a | |
---|
142 | n/a | #### TEST_CONDITION |
---|
143 | n/a | |
---|
144 | n/a | def conditionspeed_func(c, N): |
---|
145 | n/a | c.acquire() |
---|
146 | n/a | c.notify() |
---|
147 | n/a | |
---|
148 | n/a | for i in range(N): |
---|
149 | n/a | c.wait() |
---|
150 | n/a | c.notify() |
---|
151 | n/a | |
---|
152 | n/a | c.release() |
---|
153 | n/a | |
---|
154 | n/a | def test_conditionspeed(Process, c): |
---|
155 | n/a | elapsed = 0 |
---|
156 | n/a | iterations = 1 |
---|
157 | n/a | |
---|
158 | n/a | while elapsed < delta: |
---|
159 | n/a | iterations *= 2 |
---|
160 | n/a | |
---|
161 | n/a | c.acquire() |
---|
162 | n/a | p = Process(target=conditionspeed_func, args=(c, iterations)) |
---|
163 | n/a | p.start() |
---|
164 | n/a | |
---|
165 | n/a | c.wait() |
---|
166 | n/a | |
---|
167 | n/a | t = _timer() |
---|
168 | n/a | |
---|
169 | n/a | for i in range(iterations): |
---|
170 | n/a | c.notify() |
---|
171 | n/a | c.wait() |
---|
172 | n/a | |
---|
173 | n/a | elapsed = _timer() - t |
---|
174 | n/a | |
---|
175 | n/a | c.release() |
---|
176 | n/a | p.join() |
---|
177 | n/a | |
---|
178 | n/a | print(iterations * 2, 'waits in', elapsed, 'seconds') |
---|
179 | n/a | print('average number/sec:', iterations * 2 / elapsed) |
---|
180 | n/a | |
---|
181 | n/a | #### |
---|
182 | n/a | |
---|
183 | n/a | def test(): |
---|
184 | n/a | manager = multiprocessing.Manager() |
---|
185 | n/a | |
---|
186 | n/a | gc.disable() |
---|
187 | n/a | |
---|
188 | n/a | print('\n\t######## testing Queue.Queue\n') |
---|
189 | n/a | test_queuespeed(threading.Thread, queue.Queue(), |
---|
190 | n/a | threading.Condition()) |
---|
191 | n/a | print('\n\t######## testing multiprocessing.Queue\n') |
---|
192 | n/a | test_queuespeed(multiprocessing.Process, multiprocessing.Queue(), |
---|
193 | n/a | multiprocessing.Condition()) |
---|
194 | n/a | print('\n\t######## testing Queue managed by server process\n') |
---|
195 | n/a | test_queuespeed(multiprocessing.Process, manager.Queue(), |
---|
196 | n/a | manager.Condition()) |
---|
197 | n/a | print('\n\t######## testing multiprocessing.Pipe\n') |
---|
198 | n/a | test_pipespeed() |
---|
199 | n/a | |
---|
200 | n/a | print() |
---|
201 | n/a | |
---|
202 | n/a | print('\n\t######## testing list\n') |
---|
203 | n/a | test_seqspeed(list(range(10))) |
---|
204 | n/a | print('\n\t######## testing list managed by server process\n') |
---|
205 | n/a | test_seqspeed(manager.list(list(range(10)))) |
---|
206 | n/a | print('\n\t######## testing Array("i", ..., lock=False)\n') |
---|
207 | n/a | test_seqspeed(multiprocessing.Array('i', list(range(10)), lock=False)) |
---|
208 | n/a | print('\n\t######## testing Array("i", ..., lock=True)\n') |
---|
209 | n/a | test_seqspeed(multiprocessing.Array('i', list(range(10)), lock=True)) |
---|
210 | n/a | |
---|
211 | n/a | print() |
---|
212 | n/a | |
---|
213 | n/a | print('\n\t######## testing threading.Lock\n') |
---|
214 | n/a | test_lockspeed(threading.Lock()) |
---|
215 | n/a | print('\n\t######## testing threading.RLock\n') |
---|
216 | n/a | test_lockspeed(threading.RLock()) |
---|
217 | n/a | print('\n\t######## testing multiprocessing.Lock\n') |
---|
218 | n/a | test_lockspeed(multiprocessing.Lock()) |
---|
219 | n/a | print('\n\t######## testing multiprocessing.RLock\n') |
---|
220 | n/a | test_lockspeed(multiprocessing.RLock()) |
---|
221 | n/a | print('\n\t######## testing lock managed by server process\n') |
---|
222 | n/a | test_lockspeed(manager.Lock()) |
---|
223 | n/a | print('\n\t######## testing rlock managed by server process\n') |
---|
224 | n/a | test_lockspeed(manager.RLock()) |
---|
225 | n/a | |
---|
226 | n/a | print() |
---|
227 | n/a | |
---|
228 | n/a | print('\n\t######## testing threading.Condition\n') |
---|
229 | n/a | test_conditionspeed(threading.Thread, threading.Condition()) |
---|
230 | n/a | print('\n\t######## testing multiprocessing.Condition\n') |
---|
231 | n/a | test_conditionspeed(multiprocessing.Process, multiprocessing.Condition()) |
---|
232 | n/a | print('\n\t######## testing condition managed by a server process\n') |
---|
233 | n/a | test_conditionspeed(multiprocessing.Process, manager.Condition()) |
---|
234 | n/a | |
---|
235 | n/a | gc.enable() |
---|
236 | n/a | |
---|
237 | n/a | if __name__ == '__main__': |
---|
238 | n/a | multiprocessing.freeze_support() |
---|
239 | n/a | test() |
---|