From f220cd249551472341779d6a9f98fae300e5b105 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sun, 26 Feb 2023 21:18:11 -0500 Subject: [PATCH] test: add benchmarks (#55) * test: add benchmarks * docs: add to readme --- .github/workflows/ci.yml | 16 ++++++++++++++ README.md | 1 + pyproject.toml | 2 +- tests/test_benchmarks.py | 48 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/test_benchmarks.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 98a598d..538a527 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -152,6 +152,22 @@ jobs: with: run: python -m pytest app-model --color=yes + benchmarks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + with: + python-version: "3.11" + + - name: install + run: python -m pip install -e .[test] + + - name: Run benchmarks + uses: CodSpeedHQ/action@v1 + with: + run: pytest --codspeed -v --color=yes + # SKIPPING CYTHON WHEELS UNTIL # https://github.com/cython/cython/issues/4888 # build: # name: Build wheels on ${{ matrix.os }} diff --git a/README.md b/README.md index b79c02b..45db9ed 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [![Python Version](https://img.shields.io/pypi/pyversions/in-n-out.svg?color=green)](https://python.org) [![CI](https://github.com/pyapp-kit/in-n-out/actions/workflows/ci.yml/badge.svg)](https://github.com/pyapp-kit/in-n-out/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/pyapp-kit/in-n-out/branch/main/graph/badge.svg)](https://app.codecov.io/gh/pyapp-kit/in-n-out) +[![Benchmarks](https://img.shields.io/badge/⏱-codspeed-%23FF7B53)](https://codspeed.io/pyapp-kit/in-n-out) Python dependency injection you can taste. diff --git a/pyproject.toml b/pyproject.toml index 7decfc4..181ed71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ dependencies = [] # extras # https://peps.python.org/pep-0621/#dependencies-optional-dependencies [project.optional-dependencies] -test = ["pytest>=6.0", "pytest-cov", "toolz"] +test = ["pytest>=6.0", "pytest-cov", "toolz", "pytest-codspeed"] dev = [ "black", "cruft", diff --git a/tests/test_benchmarks.py b/tests/test_benchmarks.py new file mode 100644 index 0000000..57fea54 --- /dev/null +++ b/tests/test_benchmarks.py @@ -0,0 +1,48 @@ +from __future__ import annotations + +import sys +from typing import Callable + +import pytest + +import in_n_out as ino + +if all(x not in {"--codspeed", "--benchmark", "tests/test_bench.py"} for x in sys.argv): + pytest.skip("use --benchmark to run benchmark", allow_module_level=True) + + +def some_func(x: int, y: str) -> tuple[int, str]: + return x, y + + +def returns_str(x: int, y: str) -> str: + return str(x) + y + + +def test_time_to_inject(benchmark: Callable) -> None: + benchmark(ino.inject, some_func) + + +def test_time_run_injected_no_injections(benchmark: Callable) -> None: + injected = ino.inject(some_func) + benchmark(injected, 1, "a") + + +def test_time_run_injected_2_injections(benchmark: Callable) -> None: + injected = ino.inject(some_func) + with ino.register(providers=[(lambda: 1, int), (lambda: "a", str)]): + benchmark(injected) + + +def test_time_run_process(benchmark: Callable) -> None: + injected = ino.inject_processors(returns_str) + with ino.register(processors=[(lambda x: print(x), str)]): + benchmark(injected, 1, "hi") + + +def test_time_run_inject_and_process(benchmark: Callable) -> None: + injected = ino.inject(returns_str, processors=True) + with ino.register( + providers=[(lambda: "a", str)], processors=[(lambda x: ..., str)] + ): + benchmark(injected, 1)