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