Commit 596773a1 authored by Jason Madden's avatar Jason Madden

Add benchmark for greenlet tracers. [skip ci]

Current numbers on 3.7b3:

no tracer: Mean +- std dev: 414 ns +- 10 ns
trivial tracer: Mean +- std dev: 792 ns +- 16 ns
monitor tracer: Mean +- std dev: 1.62 us +- 0.12 us
max switch tracer: Mean +- std dev: 3.06 us +- 0.12 us
hub switch tracer: Mean +- std dev: 2.16 us +- 0.04 us
parent 58754ba8
# -*- 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 _monitor as monitor
N = 1000
@contextlib.contextmanager
def tracer(func):
greenlet.settrace(func)
try:
yield
finally:
greenlet.settrace(None)
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
with tracer(trivial):
return _run(loops)
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()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment