Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add micro benchmarks for hook calling playing with a the number of callers, wrappers, doing evil nesting #324

Merged
merged 1 commit into from Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.cfg
Expand Up @@ -46,6 +46,7 @@ dev =
tox
testing =
pytest
pytest-benchmark

[devpi:upload]
formats=sdist.tgz,bdist_wheel
59 changes: 58 additions & 1 deletion testing/benchmark.py
Expand Up @@ -2,7 +2,7 @@
Benchmarking and performance tests.
"""
import pytest
from pluggy import HookspecMarker, HookimplMarker
from pluggy import HookspecMarker, HookimplMarker, PluginManager
from pluggy._hooks import HookImpl
from pluggy._callers import _multicall

Expand Down Expand Up @@ -43,3 +43,60 @@ def setup():
return (hook_name, hook_impls, caller_kwargs, firstresult), {}

benchmark.pedantic(_multicall, setup=setup)


@pytest.mark.parametrize(
("plugins, wrappers, nesting"),
[
(1, 1, 0),
(1, 1, 1),
(1, 1, 5),
(1, 5, 1),
(1, 5, 5),
(5, 1, 1),
(5, 1, 5),
(5, 5, 1),
(5, 5, 5),
(20, 20, 0),
(100, 100, 0),
],
)
def test_call_hook(benchmark, plugins, wrappers, nesting):
pm = PluginManager("example")

class HookSpec:
@hookspec
def fun(self, hooks, nesting: int):
yield

class Plugin:
def __init__(self, num):
self.num = num

def __repr__(self):
return f"<Plugin {self.num}>"

@hookimpl
def fun(self, hooks, nesting: int):
if nesting:
hooks.fun(hooks=hooks, nesting=nesting - 1)

class PluginWrap:
def __init__(self, num):
self.num = num

def __repr__(self):
return f"<PluginWrap {self.num}>"

@hookimpl(hookwrapper=True)
def fun(self):
yield

pm.add_hookspecs(HookSpec)

for i in range(plugins):
pm.register(Plugin(i), name=f"plug_{i}")
for i in range(wrappers):
pm.register(PluginWrap(i), name=f"wrap_plug_{i}")

benchmark(pm.hook.fun, hooks=pm.hook, nesting=nesting)