Skip to content

Commit

Permalink
test: add benchmarks (#55)
Browse files Browse the repository at this point in the history
* test: add benchmarks

* docs: add to readme
  • Loading branch information
tlambert03 authored Feb 27, 2023
1 parent 9e07bb1 commit f220cd2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions tests/test_benchmarks.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit f220cd2

Please sign in to comment.