Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# ruff: noqa: D100, D103

from functools import reduce
from typing import Any, Callable

from pytest_benchmark.fixture import BenchmarkFixture
from stateless import (
Ability,
Depend,
Handler,
Need,
Success,
handle,
need,
run,
supply,
)
from stateless.errors import UnhandledAbilityError
from typing_extensions import Never


def never_handler(_: Ability[Any]) -> Never:
raise UnhandledAbilityError()


dummy_handler: Handler[Never] = handle(never_handler) # type: ignore


def create_effect_chain(chain_length: int, n_yields: int) -> Callable[[], Success[str]]:
def base() -> Depend[Need[str], str]:
for i in range(n_yields):
yield from need(str)
return "done"

def wrap_test_handler(
f: Callable[[], Depend[Need[str], str]],
) -> Callable[[], Depend[Need[str], str]]:
return dummy_handler(f)

g = reduce(lambda acc, _: wrap_test_handler(acc), range(chain_length - 1), base)

return supply("")(g)


def test_effect_chain(benchmark: BenchmarkFixture) -> None:
"""Benchmark a long chain of functions using functools.reduce."""

effect = create_effect_chain(500, 0)()
benchmark(run, effect)


def test_handler_chain(benchmark: BenchmarkFixture) -> None:
effect = create_effect_chain(chain_length=500, n_yields=500)()

benchmark(run, effect)
68 changes: 51 additions & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ cloudpickle = "^3.0.0"
mypy = "^1.6.1"
ipdb = "^0.13.13"
ipython = "^8.17.2"
pytest = "^7.4.3"
pytest = "^8"
pyright = "^1.1.336"
pre-commit = "^3.5.0"
ruff = "^0.14.2"
coverage = "^7.3.2"
toml = "^0.10.2"
pytest-benchmark = "^5.2.1"


[tool.mypy]
Expand Down
Loading