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