benchmark.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """
  2. Benchmarking and performance tests.
  3. """
  4. import pytest
  5. from pluggy import HookspecMarker, HookimplMarker
  6. from pluggy.hooks import HookImpl
  7. from pluggy.callers import _multicall, _legacymulticall
  8. hookspec = HookspecMarker("example")
  9. hookimpl = HookimplMarker("example")
  10. def MC(methods, kwargs, callertype, firstresult=False):
  11. hookfuncs = []
  12. for method in methods:
  13. f = HookImpl(None, "<temp>", method, method.example_impl)
  14. hookfuncs.append(f)
  15. return callertype(hookfuncs, kwargs, firstresult=firstresult)
  16. @hookimpl
  17. def hook(arg1, arg2, arg3):
  18. return arg1, arg2, arg3
  19. @hookimpl(hookwrapper=True)
  20. def wrapper(arg1, arg2, arg3):
  21. yield
  22. @pytest.fixture(params=[10, 100], ids="hooks={}".format)
  23. def hooks(request):
  24. return [hook for i in range(request.param)]
  25. @pytest.fixture(params=[10, 100], ids="wrappers={}".format)
  26. def wrappers(request):
  27. return [wrapper for i in range(request.param)]
  28. @pytest.fixture(params=[_multicall, _legacymulticall], ids=lambda item: item.__name__)
  29. def callertype(request):
  30. return request.param
  31. def inner_exec(methods, callertype):
  32. return MC(methods, {"arg1": 1, "arg2": 2, "arg3": 3}, callertype)
  33. def test_hook_and_wrappers_speed(benchmark, hooks, wrappers, callertype):
  34. benchmark(inner_exec, hooks + wrappers, callertype)