mp_benchmarks.py 5.43 KB
Newer Older
1 2 3
#
# Simple benchmarks for the multiprocessing package
#
Benjamin Peterson's avatar
Benjamin Peterson committed
4 5 6
# Copyright (c) 2006-2008, R Oudkerk
# All rights reserved.
#
7

8 9 10 11 12
import time
import multiprocessing
import threading
import queue
import gc
13

14
_timer = time.perf_counter
15 16 17 18 19 20 21 22 23 24 25 26

delta = 1


#### TEST_QUEUESPEED

def queuespeed_func(q, c, iterations):
    a = '0' * 256
    c.acquire()
    c.notify()
    c.release()

27
    for i in range(iterations):
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        q.put(a)

    q.put('STOP')

def test_queuespeed(Process, q, c):
    elapsed = 0
    iterations = 1

    while elapsed < delta:
        iterations *= 2

        p = Process(target=queuespeed_func, args=(q, c, iterations))
        c.acquire()
        p.start()
        c.wait()
        c.release()

        result = None
        t = _timer()

        while result != 'STOP':
            result = q.get()

        elapsed = _timer() - t

        p.join()

55 56
    print(iterations, 'objects passed through the queue in', elapsed, 'seconds')
    print('average number/sec:', iterations/elapsed)
57 58 59 60 61 62 63 64 65 66


#### TEST_PIPESPEED

def pipe_func(c, cond, iterations):
    a = '0' * 256
    cond.acquire()
    cond.notify()
    cond.release()

67
    for i in range(iterations):
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        c.send(a)

    c.send('STOP')

def test_pipespeed():
    c, d = multiprocessing.Pipe()
    cond = multiprocessing.Condition()
    elapsed = 0
    iterations = 1

    while elapsed < delta:
        iterations *= 2

        p = multiprocessing.Process(target=pipe_func,
                                    args=(d, cond, iterations))
        cond.acquire()
        p.start()
        cond.wait()
        cond.release()

        result = None
        t = _timer()

        while result != 'STOP':
            result = c.recv()

        elapsed = _timer() - t
        p.join()

97 98
    print(iterations, 'objects passed through connection in',elapsed,'seconds')
    print('average number/sec:', iterations/elapsed)
99 100 101 102 103 104 105 106 107 108 109 110 111


#### TEST_SEQSPEED

def test_seqspeed(seq):
    elapsed = 0
    iterations = 1

    while elapsed < delta:
        iterations *= 2

        t = _timer()

112
        for i in range(iterations):
113 114
            a = seq[5]

115
        elapsed = _timer() - t
116

117 118
    print(iterations, 'iterations in', elapsed, 'seconds')
    print('average number/sec:', iterations/elapsed)
119 120 121 122 123 124 125 126 127 128 129 130 131


#### TEST_LOCK

def test_lockspeed(l):
    elapsed = 0
    iterations = 1

    while elapsed < delta:
        iterations *= 2

        t = _timer()

132
        for i in range(iterations):
133 134 135
            l.acquire()
            l.release()

136
        elapsed = _timer() - t
137

138 139
    print(iterations, 'iterations in', elapsed, 'seconds')
    print('average number/sec:', iterations/elapsed)
140 141 142 143 144 145 146 147


#### TEST_CONDITION

def conditionspeed_func(c, N):
    c.acquire()
    c.notify()

148
    for i in range(N):
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        c.wait()
        c.notify()

    c.release()

def test_conditionspeed(Process, c):
    elapsed = 0
    iterations = 1

    while elapsed < delta:
        iterations *= 2

        c.acquire()
        p = Process(target=conditionspeed_func, args=(c, iterations))
        p.start()

        c.wait()

        t = _timer()

169
        for i in range(iterations):
170 171 172
            c.notify()
            c.wait()

173
        elapsed = _timer() - t
174 175 176 177

        c.release()
        p.join()

178 179
    print(iterations * 2, 'waits in', elapsed, 'seconds')
    print('average number/sec:', iterations * 2 / elapsed)
180 181 182 183 184 185 186 187

####

def test():
    manager = multiprocessing.Manager()

    gc.disable()

188 189
    print('\n\t######## testing Queue.Queue\n')
    test_queuespeed(threading.Thread, queue.Queue(),
190
                    threading.Condition())
191
    print('\n\t######## testing multiprocessing.Queue\n')
192 193
    test_queuespeed(multiprocessing.Process, multiprocessing.Queue(),
                    multiprocessing.Condition())
194
    print('\n\t######## testing Queue managed by server process\n')
195 196
    test_queuespeed(multiprocessing.Process, manager.Queue(),
                    manager.Condition())
197
    print('\n\t######## testing multiprocessing.Pipe\n')
198 199
    test_pipespeed()

200
    print()
201

202 203 204 205 206 207 208 209
    print('\n\t######## testing list\n')
    test_seqspeed(list(range(10)))
    print('\n\t######## testing list managed by server process\n')
    test_seqspeed(manager.list(list(range(10))))
    print('\n\t######## testing Array("i", ..., lock=False)\n')
    test_seqspeed(multiprocessing.Array('i', list(range(10)), lock=False))
    print('\n\t######## testing Array("i", ..., lock=True)\n')
    test_seqspeed(multiprocessing.Array('i', list(range(10)), lock=True))
210

211
    print()
212

213
    print('\n\t######## testing threading.Lock\n')
214
    test_lockspeed(threading.Lock())
215
    print('\n\t######## testing threading.RLock\n')
216
    test_lockspeed(threading.RLock())
217
    print('\n\t######## testing multiprocessing.Lock\n')
218
    test_lockspeed(multiprocessing.Lock())
219
    print('\n\t######## testing multiprocessing.RLock\n')
220
    test_lockspeed(multiprocessing.RLock())
221
    print('\n\t######## testing lock managed by server process\n')
222
    test_lockspeed(manager.Lock())
223
    print('\n\t######## testing rlock managed by server process\n')
224 225
    test_lockspeed(manager.RLock())

226
    print()
227

228
    print('\n\t######## testing threading.Condition\n')
229
    test_conditionspeed(threading.Thread, threading.Condition())
230
    print('\n\t######## testing multiprocessing.Condition\n')
231
    test_conditionspeed(multiprocessing.Process, multiprocessing.Condition())
232
    print('\n\t######## testing condition managed by a server process\n')
233 234 235 236 237 238 239
    test_conditionspeed(multiprocessing.Process, manager.Condition())

    gc.enable()

if __name__ == '__main__':
    multiprocessing.freeze_support()
    test()