File: bench_tracer.py

package info (click to toggle)
python-gevent 20.9.0-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 21,000 kB
  • sloc: python: 158,152; ansic: 72,855; sh: 5,235; makefile: 1,574; javascript: 108; awk: 18
file content (120 lines) | stat: -rw-r--r-- 2,310 bytes parent folder | download | duplicates (4)
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
# -*- coding: utf-8 -*-
"""
Benchmarks for gevent.queue

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

import contextlib

import perf

import greenlet
import gevent
from gevent import _tracer as monitor

N = 1000

@contextlib.contextmanager
def tracer(cls, *args):
    inst = cls(*args)
    try:
        yield
    finally:
        inst.kill()

def _run(loops):

    duration = 0

    for _ in range(loops):

        g1 = None
        def switch():
            parent = gevent.getcurrent().parent
            for _ in range(N):
                parent.switch()

        g1 = gevent.Greenlet(switch)
        g1.parent = gevent.getcurrent()

        t1 = perf.perf_counter()
        for _ in range(N):
            g1.switch()

        t2 = perf.perf_counter()

        duration += t2 - t1
    return duration


def bench_no_trace(loops):
    return _run(loops)

def bench_trivial_tracer(loops):

    def trivial(_event, _args):
        return

    greenlet.settrace(trivial)
    try:
        return _run(loops)
    finally:
        greenlet.settrace(None)


def bench_monitor_tracer(loops):
    with tracer(monitor.GreenletTracer):
        return _run(loops)


def bench_hub_switch_tracer(loops):
    # use current as the hub, since tracer fires
    # when we switch into that greenlet
    with tracer(monitor.HubSwitchTracer, gevent.getcurrent(), 1):
        return _run(loops)

def bench_max_switch_tracer(loops):
    # use object() as the hub, since tracer fires
    # when switch into something that's *not* the hub
    with tracer(monitor.MaxSwitchTracer, object, 1):
        return _run(loops)

def main():
    runner = perf.Runner()

    runner.bench_time_func(
        "no tracer",
        bench_no_trace,
        inner_loops=N
    )

    runner.bench_time_func(
        "trivial tracer",
        bench_trivial_tracer,
        inner_loops=N
    )

    runner.bench_time_func(
        "monitor tracer",
        bench_monitor_tracer,
        inner_loops=N
    )

    runner.bench_time_func(
        "max switch tracer",
        bench_max_switch_tracer,
        inner_loops=N
    )

    runner.bench_time_func(
        "hub switch tracer",
        bench_hub_switch_tracer,
        inner_loops=N
    )


if __name__ == '__main__':
    main()