|
| 1 | +""" |
| 2 | +Benchmarking and performance tests. |
| 3 | +""" |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pluggy import _MultiCall, HookImpl |
| 7 | +from pluggy import HookspecMarker, HookimplMarker |
| 8 | + |
| 9 | + |
| 10 | +hookspec = HookspecMarker("example") |
| 11 | +hookimpl = HookimplMarker("example") |
| 12 | + |
| 13 | + |
| 14 | +def MC(methods, kwargs, firstresult=False): |
| 15 | + hookfuncs = [] |
| 16 | + for method in methods: |
| 17 | + f = HookImpl(None, "<temp>", method, method.example_impl) |
| 18 | + hookfuncs.append(f) |
| 19 | + return _MultiCall(hookfuncs, kwargs, {"firstresult": firstresult}) |
| 20 | + |
| 21 | + |
| 22 | +@hookimpl(hookwrapper=True) |
| 23 | +def m1(arg1, arg2, arg3): |
| 24 | + yield |
| 25 | + |
| 26 | + |
| 27 | +@hookimpl |
| 28 | +def m2(arg1, arg2, arg3): |
| 29 | + return arg1, arg2, arg3 |
| 30 | + |
| 31 | + |
| 32 | +@hookimpl(hookwrapper=True) |
| 33 | +def w1(arg1, arg2, arg3): |
| 34 | + yield |
| 35 | + |
| 36 | + |
| 37 | +@hookimpl(hookwrapper=True) |
| 38 | +def w2(arg1, arg2, arg3): |
| 39 | + yield |
| 40 | + |
| 41 | + |
| 42 | +def inner_exec(methods): |
| 43 | + return MC(methods, {'arg1': 1, 'arg2': 2, 'arg3': 3}).execute() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.benchmark |
| 47 | +def test_hookimpls_speed(benchmark): |
| 48 | + benchmark(inner_exec, [m1, m2]) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.benchmark |
| 52 | +def test_hookwrappers_speed(benchmark): |
| 53 | + benchmark(inner_exec, [w1, w2]) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.benchmark |
| 57 | +def test_impls_and_wrappers_speed(benchmark): |
| 58 | + benchmark(inner_exec, [m1, m2, w1, w2]) |
0 commit comments