Skip to content

Commit

Permalink
feat: add introspection benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Jun 7, 2023
1 parent 260fe53 commit 84cc814
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,19 @@ jobs:
run: pip install pytest-benchmark~=4.0.0 py
- name: Run tests
run: pytest -vs

benchmarks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12
uses: actions/setup-python@v2
with:
python-version: "3.11"
- name: Install local version of pytest-codspeed
run: pip install .

- name: Run benchmarks
uses: CodSpeedHQ/action@main
with:
run: pytest tests/benchmarks/ --codspeed
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ use_parentheses = true
force_grid_wrap = 0
float_to_top = true

[tool.pytest.ini_options]
addopts = "--ignore=tests/benchmarks"

[tool.coverage.run]
branch = true
[tool.coverage.report]
Expand Down
53 changes: 53 additions & 0 deletions tests/benchmarks/test_bench_fibo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
def recursive_fibonacci(n: int) -> int:
if n in [0, 1]:
return n
return recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2)


def recursive_cached_fibonacci(n: int) -> int:
cache = {0: 0, 1: 1}

def fibo(n) -> int:
if n in cache:
return cache[n]
cache[n] = fibo(n - 1) + fibo(n - 2)
return cache[n]

return fibo(n)


def iterative_fibonacci(n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a


def test_iterative_fibo_10(benchmark):
@benchmark
def _():
iterative_fibonacci(10)


def test_recursive_fibo_10(benchmark):
@benchmark
def _():
recursive_fibonacci(10)


def test_recursive_fibo_20(benchmark):
@benchmark
def _():
recursive_fibonacci(20)


def test_recursive_cached_fibo_10(benchmark):
@benchmark
def _():
recursive_cached_fibonacci(10)


def test_recursive_cached_fibo_100(benchmark):
@benchmark
def _():
recursive_cached_fibonacci(100)

0 comments on commit 84cc814

Please # to comment.