File: bench_hub.py

package info (click to toggle)
python-gevent 24.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,364 kB
  • sloc: python: 138,768; ansic: 87,807; sh: 12,548; makefile: 2,379; javascript: 108
file content (118 lines) | stat: -rw-r--r-- 2,556 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
# -*- coding: utf-8 -*-
"""
Benchmarks for hub primitive operations.

"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import pyperf as perf
from pyperf import perf_counter

import gevent
from greenlet import greenlet
from greenlet import getcurrent


N = 1000

def bench_switch():

    class Parent(type(gevent.get_hub())):
        def run(self):
            parent = self.parent
            for _ in range(N):
                parent.switch()

    def child():
        parent = getcurrent().parent
        # Back to the hub, which in turn goes
        # back to the main greenlet
        for _ in range(N):
            parent.switch()

    hub = Parent(None, None)
    child_greenlet = greenlet(child, hub)
    for _ in range(N):
        child_greenlet.switch()

def bench_wait_ready():

    class Watcher(object):
        def start(self, cb, obj):
            # Immediately switch back to the waiter, mark as ready
            cb(obj)

        def stop(self):
            pass

    watcher = Watcher()
    hub = gevent.get_hub()

    for _ in range(1000):
        hub.wait(watcher)

def bench_cancel_wait():

    class Watcher(object):
        active = True
        callback = object()

        def close(self):
            pass

    watcher = Watcher()
    hub = gevent.get_hub()
    loop = hub.loop

    for _ in range(1000):
        # Schedule all the callbacks.
        hub.cancel_wait(watcher, None, True)

    # Run them!
    for cb in loop._callbacks:
        if cb.callback:
            cb.callback(*cb.args)
            cb.stop() # so the real loop won't do it

    # destroy the loop so we don't keep building these functions
    # up
    hub.destroy(True)

def bench_wait_func_ready():
    from gevent.hub import wait
    class ToWatch(object):
        def rawlink(self, cb):
            cb(self)

    watched_objects = [ToWatch() for _ in range(N)]

    t0 = perf_counter()

    wait(watched_objects)

    return perf_counter() - t0

def main():

    runner = perf.Runner()

    runner.bench_func('multiple wait ready',
                      bench_wait_func_ready,
                      inner_loops=N)

    runner.bench_func('wait ready',
                      bench_wait_ready,
                      inner_loops=N)

    runner.bench_func('cancel wait',
                      bench_cancel_wait,
                      inner_loops=N)

    runner.bench_func('switch',
                      bench_switch,
                      inner_loops=N)

if __name__ == '__main__':
    main()