8000 add micro benchmarks for hook calling playing with a the number of ca… · pytest-dev/pluggy@1424ab0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1424ab0

Browse files
add micro benchmarks for hook calling playing with a the number of callers, wrappers, doing evil nesting
1 parent 5e51864 commit 1424ab0

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dev =
4646
tox
4747
testing =
4848
pytest
49+
pytest-benchmark
4950

5051
[devpi:upload]
5152
formats=sdist.tgz,bdist_wheel

testing/benchmark.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Benchmarking and performance tests.
33
"""
44
import pytest
5-
from pluggy import HookspecMarker, HookimplMarker
5+
from pluggy import HookspecMarker, HookimplMarker, PluginManager
66
from pluggy._hooks import HookImpl
77
from pluggy._callers import _multicall
88

@@ -43,3 +43,60 @@ def setup():
4343
return (hook_name, hook_impls, caller_kwargs, firstresult), {}
4444

4545
benchmark.pedantic(_multicall, setup=setup)
46+
47+
48+
@pytest.mark.parametrize(
49+
("plugins, wrappers, nesting"),
50+
[
51+
(1, 1, 0),
52+
(1, 1, 1),
53+
(1, 1, 5),
54+
(1, 5, 1),
55+
(1, 5, 5),
56+
(5, 1, 1),
57+
(5, 1, 5),
58+
(5, 5, 1),
59+
(5, 5, 5),
60+
(20, 20, 0),
61+
(100, 100, 0),
62+
],
63+
)
64+
def test_call_hook(benchmark, plugins, wrappers, nesting):
65+
pm = PluginManager("example")
66+
67+
class HookSpec:
68+
@hookspec
69+
def fun(self, hooks, nesting: int):
70+
yield
71+
72+
class Plugin:
73+
def __init__(self, num):
74+
self.num = num
75+
76+
def __repr__(self):
77+
return f"<Plugin {self.num}>"
78+
79+
@hookimpl
80+
def fun(self, hooks, nesting: int):
81+
if nesting:
82+
hooks.fun(hooks=hooks, nesting=nesting - 1)
83+
84+
class PluginWrap:
85+
def __init__(self, num):
86+
self.num = num
87+
88+
def __repr__(self):
89+
return f"<PluginWrap {self.num}>"
90+
91+
@hookimpl(hookwrapper=True)
92+
def fun(self):
93+
yield
94+
95+
pm.add_hookspecs(HookSpec)
96+
97+
for i in range(plugins):
98+
pm.register(Plugin(i), name=f"plug_{i}")
99+
for i in range(wrappers):
100+
pm.register(PluginWrap(i), name=f"wrap_plug_{i}")
101+
102+
benchmark(pm.hook.fun, hooks=pm.hook, nesting=nesting)

0 commit comments

Comments
 (0)
0