File: bench_spawn.py

package info (click to toggle)
python-gevent 0.13.6-1%2Bnmu3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,324 kB
  • sloc: python: 13,296; makefile: 95; ansic: 37
file content (169 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55
56
57
58
59
60
61
62
63
64
65
66
67
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""Benchmarking spawn() performance.
"""
import sys
import os
import random
from time import time


def init():
    global N, counter
    N = 10000
    counter = 0

init()


def incr(sleep, **kwargs):
    global counter
    counter += 1
    sleep(0)


def noop(p):
    pass


def test(spawn, sleep, kwargs):
    start = time()
    for _ in xrange(N):
        spawn(incr, sleep, **kwargs)
    delta = time() - start
    print 'spawning: %.1f microseconds per greenlet' % (delta * 1000000.0 / N)
    assert counter == 0, counter
    start = time()
    sleep(0)
    delta = time() - start
    assert counter == N, (counter, N)
    print 'sleep(0): %.1f microseconds per greenlet' % (delta * 1000000.0 / N)


def bench_none(options):
    kwargs = options.kwargs
    start = time()
    for _ in xrange(N):
        incr(noop, **kwargs)
    delta = time() - start
    assert counter == N, (counter, N)
    print '%.2f microseconds' % (delta * 1000000.0 / N)


def bench_gevent(options):
    import gevent
    print 'using gevent from %s' % gevent.__file__
    from gevent import spawn, sleep
    test(spawn, sleep, options.kwargs)


def bench_geventraw(options):
    import gevent
    print 'using gevent from %s' % gevent.__file__
    from gevent import sleep, spawn_raw
    test(spawn_raw, sleep, options.kwargs)


def bench_geventpool(options):
    import gevent
    print 'using gevent from %s' % gevent.__file__
    from gevent import sleep
    from gevent.pool import Pool
    p = Pool()
    test(p.spawn, sleep, options.kwargs)
    start = time()
    p.join()
    delta = time() - start
    print 'joining: %.1f microseconds per greenlet' % (delta * 1000000.0 / N)


def bench_eventlet(options):
    try:
        import eventlet
    except ImportError:
        if options.ignore_import_errors:
            return
        raise
    print 'using eventlet from %s' % eventlet.__file__
    from eventlet.api import spawn, sleep, use_hub
    if options.eventlet_hub is not None:
        use_hub(options.eventlet_hub)
    test(spawn, sleep, options.kwargs)


def bench_eventlet1(options):
    try:
        import eventlet
    except ImportError:
        if options.ignore_import_errors:
            return
        raise
    print 'using eventlet from %s' % eventlet.__file__
    from eventlet.proc import spawn_greenlet as spawn
    from eventlet.api import sleep, use_hub
    if options.eventlet_hub:
        use_hub(options.eventlet_hub)
    if options.with_kwargs:
        print 'eventlet.proc.spawn_greenlet does support kwargs'
        return
    test(spawn, sleep, options.kwargs)


def bench_all(options):
    import time
    error = 0
    names = all()
    random.shuffle(names)
    for func in names:
        cmd = '%s %s %s --ignore-import-errors' % (sys.executable, __file__, func)
        print cmd
        sys.stdout.flush()
        time.sleep(0.01)
        if os.system(cmd):
            error = 1
            print '%s failed' % cmd
        print
    for func in names:
        cmd = '%s %s --with-kwargs %s --ignore-import-errors' % (sys.executable, __file__, func)
        print cmd
        sys.stdout.flush()
        if os.system(cmd):
            error = 1
            print '%s failed' % cmd
        print
    if error:
        sys.exit(1)


def all():
    result = [x for x in globals() if x.startswith('bench_') and x != 'bench_all']
    result.sort(key=lambda x: globals()[x].func_code.co_firstlineno)
    result = [x.replace('bench_', '') for x in result]
    return result


def all_functions():
    return [globals()['bench_%s' % x] for x in all()]


if __name__ == '__main__':
    USAGE = 'USAGE: python %s [--with-kwargs] [--eventlet-hub HUB] %s' % (sys.argv[0], '|'.join(all()))
    if not sys.argv[1:]:
        sys.exit(USAGE)
    import optparse
    parser = optparse.OptionParser()
    parser.add_option('--with-kwargs', default=False, action='store_true')
    parser.add_option('--eventlet-hub')
    parser.add_option('--ignore-import-errors', action='store_true')
    options, args = parser.parse_args()
    if options.with_kwargs:
        options.kwargs = {'foo': 1, 'bar': 'hello'}
    else:
        options.kwargs = {}
    if len(args) != 1:
        sys.exit(USAGE)
    if args[0] == 'all':
        bench_all(options)
    else:
        if args[0] not in all():
            sys.exit(USAGE)
        function = globals()['bench_' + args[0]]
        function(options)