|
| 1 | +""" |
| 2 | +Benchmarking and performance tests. |
| 3 | +""" |
| 4 | +import pytest |
| 5 | +from pluggy import _MultiCall, HookImpl, HookspecMarker, HookimplMarker |
| 6 | + |
| 7 | +hookspec = HookspecMarker("example") |
| 8 | +hookimpl = HookimplMarker("example") |
| 9 | + |
| 10 | + |
| 11 | +def MC(methods, kwargs, firstresult=False): |
| 12 | + hookfuncs = [] |
| 13 | + for method in methods: |
| 14 | + f = HookImpl(None, "<temp>", method, method.example_impl) |
| 15 | + hookfuncs.append(f) |
| 16 | + return _MultiCall(hookfuncs, kwargs, {"firstresult": firstresult}) |
| 17 | + |
| 18 | + |
| 19 | +@hookimpl |
| 20 | +def hook(arg1, arg2, arg3): |
| 21 | + return arg1, arg2, arg3 |
| 22 | + |
| 23 | + |
| 24 | +@hookimpl(hookwrapper=True) |
| 25 | +def wrapper(arg1, arg2, arg3): |
| 26 | + yield |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture( |
| 30 | + params=[0, 1, 10, 100], |
| 31 | + ids="hooks={}".format, |
| 32 | +) |
| 33 | +def hooks(request): |
| 34 | + return [hook for i in range(request.param)] |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture( |
| 38 | + params=[0, 1, 10, 100], |
| 39 | + ids="wrappers={}".format, |
| 40 | +) |
| 41 | +def wrappers(request): |
| 42 | + return [wrapper for i in range(request.param)] |
| 43 | + |
| 44 | + |
| 45 | +def inner_exec(methods): |
| 46 | + return MC(methods, {'arg1': 1, 'arg2': 2, 'arg3': 3}).execute() |
| 47 | + |
| 48 | + |
| 49 | +def test_hook_and_wrappers_speed(benchmark, hooks, wrappers): |
| 50 | + benchmark(inner_exec, hooks + wrappers) |
0 commit comments