-
-
Notifications
You must be signed in to change notification settings - Fork 145
Add CodSpeed performance benchmarks and CI integration #2426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+290
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e56f2e8
Add CodSpeed performance benchmarks and CI workflow
codspeedbot 64a2819
Add do-notation benchmarks and loosen pytest-codspeed constraint
codspeedbot 7f1109f
Remove walltime mode from CodSpeed workflow
codspeedbot b7412dd
fix linter issues
thepabloaguilar 7258423
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| name: CodSpeed | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| pull_request: | ||
| # `workflow_dispatch` allows CodSpeed to trigger backtest | ||
| # performance analysis in order to generate initial data. | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| id-token: write # for OpenID Connect authentication with CodSpeed | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| benchmarks: | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| include: | ||
| - mode: instrumentation | ||
| runner: ubuntu-latest | ||
| - mode: memory | ||
| runner: ubuntu-latest | ||
|
|
||
| name: Run benchmarks (${{ matrix.mode }}) | ||
| runs-on: ${{ matrix.runner }} | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v6.0.3 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: '3.12' | ||
|
|
||
| - name: Install poetry | ||
| run: | | ||
| curl -sSL "https://install.python-poetry.org" | python | ||
|
|
||
| # Adding `poetry` to `$PATH`: | ||
| echo "$HOME/.local/bin" >> $GITHUB_PATH | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| poetry config virtualenvs.in-project true | ||
| poetry install --all-extras | ||
|
|
||
| - name: Run benchmarks | ||
| uses: CodSpeedHQ/action@v4.17.6 | ||
| with: | ||
| mode: ${{ matrix.mode }} | ||
| run: poetry run pytest benchmarks/ --codspeed -p no:cov -o addopts="" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| """Performance benchmarks for the core ``returns`` containers. | ||
|
|
||
| These benchmarks exercise the hot paths of the most commonly used | ||
| containers (``Result``, ``Maybe``, ``IO``) together with the pipeline | ||
| and iterable helpers. They are measured by CodSpeed in CI. | ||
| """ | ||
|
|
||
| from returns.io import IO | ||
| from returns.iterables import Fold | ||
| from returns.maybe import Maybe, Nothing, Some | ||
| from returns.pipeline import flow | ||
| from returns.pointfree import bind, map_ | ||
| from returns.result import Failure, Result, Success, safe | ||
|
|
||
|
|
||
| def _increment(value: int) -> int: | ||
| return value + 1 | ||
|
|
||
|
|
||
| def _as_success(value: int) -> Result[int, str]: | ||
| return Success(value + 1) | ||
|
|
||
|
|
||
| def _as_some(value: int) -> Maybe[int]: | ||
| return Some(value + 1) | ||
|
|
||
|
|
||
| def test_result_map_chain(benchmark) -> None: | ||
|
thepabloaguilar marked this conversation as resolved.
|
||
| """A long chain of ``.map`` calls over a ``Result``.""" | ||
|
|
||
| def run() -> Result[int, str]: | ||
| container: Result[int, str] = Success(0) | ||
| for _ in range(100): | ||
| container = container.map(_increment) | ||
| return container | ||
|
|
||
| assert benchmark(run) == Success(100) | ||
|
|
||
|
|
||
| def test_result_bind_chain(benchmark) -> None: | ||
| """A long chain of ``.bind`` calls over a ``Result``.""" | ||
|
|
||
| def run() -> Result[int, str]: | ||
| container: Result[int, str] = Success(0) | ||
| for _ in range(100): | ||
| container = container.bind(_as_success) | ||
| return container | ||
|
|
||
| assert benchmark(run) == Success(100) | ||
|
|
||
|
|
||
| def test_result_do_notation(benchmark) -> None: | ||
| """Compose ``Result`` values through ``.do`` notation.""" | ||
|
|
||
| def run() -> Result[int, str]: | ||
| return Result.do( | ||
| first + second for first in Success(1) for second in Success(2) | ||
| ) | ||
|
|
||
| assert benchmark(run) == Success(3) | ||
|
|
||
|
|
||
| def test_maybe_do_notation(benchmark) -> None: | ||
| """Compose ``Maybe`` values through ``.do`` notation.""" | ||
|
|
||
| def run() -> Maybe[int]: | ||
| return Maybe.do( | ||
| first + second for first in Some(1) for second in Some(2) | ||
| ) | ||
|
|
||
| assert benchmark(run) == Some(3) | ||
|
|
||
|
|
||
| def test_result_failure_lash(benchmark) -> None: | ||
| """Recover from a failure using ``.lash`` and ``.value_or``.""" | ||
| value = 42 | ||
|
|
||
| def run() -> int: | ||
| container: Result[int, str] = Failure('boom') | ||
| return container.lash(lambda _: Success(value)).value_or(0) | ||
|
|
||
| assert benchmark(run) == value | ||
|
|
||
|
|
||
| def test_safe_decorator(benchmark) -> None: | ||
| """The ``@safe`` decorator wrapping a raising function.""" | ||
|
|
||
| @safe | ||
| def _divide(numerator: int, denominator: int) -> float: | ||
| return numerator / denominator | ||
|
|
||
| def run() -> Result[float, Exception]: | ||
| return _divide(10, 0) | ||
|
|
||
| result = benchmark(run) | ||
| assert isinstance(result, Failure) | ||
|
|
||
|
|
||
| def test_maybe_map_chain(benchmark) -> None: | ||
| """A long chain of ``.map`` calls over a ``Maybe``.""" | ||
|
|
||
| def run() -> Maybe[int]: | ||
| container: Maybe[int] = Some(0) | ||
| for _ in range(100): | ||
| container = container.map(_increment) | ||
| return container | ||
|
|
||
| assert benchmark(run) == Some(100) | ||
|
|
||
|
|
||
| def test_maybe_bind_nothing(benchmark) -> None: | ||
| """Short-circuiting a ``Maybe`` chain through ``Nothing``.""" | ||
|
|
||
| def run() -> int: | ||
| container: Maybe[int] = Some(1) | ||
| container = container.bind(lambda _: Nothing) | ||
| return container.bind(_as_some).value_or(-1) | ||
|
|
||
| assert benchmark(run) == -1 | ||
|
|
||
|
|
||
| def test_io_map_chain(benchmark) -> None: | ||
| """A long chain of ``.map`` calls over an ``IO`` container.""" | ||
|
|
||
| def run() -> IO[int]: | ||
| container = IO(0) | ||
| for _ in range(100): | ||
| container = container.map(_increment) | ||
| return container | ||
|
|
||
| assert benchmark(run) == IO(100) | ||
|
|
||
|
|
||
| def test_flow_pipeline(benchmark) -> None: | ||
| """Compose containers through ``flow`` with point-free helpers.""" | ||
|
|
||
| def run() -> Result[int, str]: | ||
| return flow( | ||
| Success(1), | ||
| map_(_increment), | ||
| bind(_as_success), | ||
| map_(_increment), | ||
| ) | ||
|
|
||
| assert benchmark(run) == Success(4) | ||
|
|
||
|
|
||
| def test_fold_collect_results(benchmark) -> None: | ||
| """Fold an iterable of ``Result`` values into a single container.""" | ||
| items = [Success(index) for index in range(100)] | ||
|
|
||
| def run() -> Result[tuple[int, ...], str]: | ||
| return Fold.collect(items, Success(())) | ||
|
|
||
| assert benchmark(run) == Success(tuple(range(100))) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.