|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Benchmarks for hub primitive operations. |
| 4 | +
|
| 5 | +Taken from https://github.com/gevent/gevent/blob/master/benchmarks/bench_hub.py |
| 6 | +Modified to remove perf and not need any command line arguments |
| 7 | +""" |
| 8 | +from __future__ import absolute_import |
| 9 | +from __future__ import division |
| 10 | +from __future__ import print_function |
| 11 | + |
| 12 | +# import perf |
| 13 | +# from perf import perf_counter |
| 14 | + |
| 15 | +import gevent |
| 16 | +from greenlet import greenlet |
| 17 | +from greenlet import getcurrent |
| 18 | + |
| 19 | + |
| 20 | +N = 1000 |
| 21 | + |
| 22 | +def bench_switch(): |
| 23 | + |
| 24 | + class Parent(type(gevent.get_hub())): |
| 25 | + def run(self): |
| 26 | + parent = self.parent |
| 27 | + for _ in range(N): |
| 28 | + parent.switch() |
| 29 | + |
| 30 | + def child(): |
| 31 | + parent = getcurrent().parent |
| 32 | + # Back to the hub, which in turn goes |
| 33 | + # back to the main greenlet |
| 34 | + for _ in range(N): |
| 35 | + parent.switch() |
| 36 | + |
| 37 | + hub = Parent(None, None) |
| 38 | + child_greenlet = greenlet(child, hub) |
| 39 | + for _ in range(N): |
| 40 | + child_greenlet.switch() |
| 41 | + |
| 42 | +def bench_wait_ready(): |
| 43 | + |
| 44 | + class Watcher(object): |
| 45 | + def start(self, cb, obj): |
| 46 | + # Immediately switch back to the waiter, mark as ready |
| 47 | + cb(obj) |
| 48 | + |
| 49 | + def stop(self): |
| 50 | + pass |
| 51 | + |
| 52 | + watcher = Watcher() |
| 53 | + hub = gevent.get_hub() |
| 54 | + |
| 55 | + for _ in range(1000): |
| 56 | + hub.wait(watcher) |
| 57 | + |
| 58 | +def bench_cancel_wait(): |
| 59 | + |
| 60 | + class Watcher(object): |
| 61 | + active = True |
| 62 | + callback = object() |
| 63 | + |
| 64 | + def close(self): |
| 65 | + pass |
| 66 | + |
| 67 | + watcher = Watcher() |
| 68 | + hub = gevent.get_hub() |
| 69 | + loop = hub.loop |
| 70 | + |
| 71 | + for _ in range(1000): |
| 72 | + # Schedule all the callbacks. |
| 73 | + hub.cancel_wait(watcher, None, True) |
| 74 | + |
| 75 | + # Run them! |
| 76 | + for cb in loop._callbacks: |
| 77 | + if cb.callback: |
| 78 | + cb.callback(*cb.args) |
| 79 | + cb.stop() # so the real loop won't do it |
| 80 | + |
| 81 | + # destroy the loop so we don't keep building these functions |
| 82 | + # up |
| 83 | + hub.destroy(True) |
| 84 | + |
| 85 | +def bench_wait_func_ready(): |
| 86 | + from gevent.hub import wait |
| 87 | + class ToWatch(object): |
| 88 | + def rawlink(self, cb): |
| 89 | + cb(self) |
| 90 | + |
| 91 | + watched_objects = [ToWatch() for _ in range(N)] |
| 92 | + |
| 93 | + t0 = perf_counter() |
| 94 | + |
| 95 | + wait(watched_objects) |
| 96 | + |
| 97 | + return perf_counter() - t0 |
| 98 | + |
| 99 | +def main(): |
| 100 | + |
| 101 | + runner = perf.Runner() |
| 102 | + |
| 103 | + runner.bench_func('multiple wait ready', |
| 104 | + bench_wait_func_ready, |
| 105 | + inner_loops=N) |
| 106 | + |
| 107 | + runner.bench_func('wait ready', |
| 108 | + bench_wait_ready, |
| 109 | + inner_loops=N) |
| 110 | + |
| 111 | + runner.bench_func('cancel wait', |
| 112 | + bench_cancel_wait, |
| 113 | + inner_loops=N) |
| 114 | + |
| 115 | + runner.bench_func('switch', |
| 116 | + bench_switch, |
| 117 | + inner_loops=N) |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + # main() |
| 121 | + for i in range(3000): |
| 122 | + bench_switch() |
0 commit comments