|
2 | 2 | Benchmarking and performance tests.
|
3 | 3 | """
|
4 | 4 | import pytest
|
5 |
| -from pluggy import HookspecMarker, HookimplMarker |
| 5 | +from pluggy import HookspecMarker, HookimplMarker, PluginManager |
6 | 6 | from pluggy._hooks import HookImpl
|
7 | 7 | from pluggy._callers import _multicall
|
8 | 8 |
|
@@ -43,3 +43,60 @@ def setup():
|
43 | 43 | return (hook_name, hook_impls, caller_kwargs, firstresult), {}
|
44 | 44 |
|
45 | 45 | 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