Skip to content

Benchmarks #123

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
merged 5 commits into from
Feb 11, 2021
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
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ History

1.3.0 (UNRELEASED)
------------------
* ``cattrs`` now has a benchmark suite to help make and keep cattrs the fastest it can be. The instructions on using it can be found under the `Benchmarking <https://cattrs.readthedocs.io/en/latest/benchmarking.html>` section in the docs.
(`#123 <https://github.com/Tinche/cattrs/pull/123>`_)


1.2.0 (2021-01-31)
------------------
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: clean clean-test clean-pyc clean-build docs help
.PHONY: clean clean-test clean-pyc clean-build docs help bench bench-cmp
.DEFAULT_GOAL := help
define BROWSER_PYSCRIPT
import os, webbrowser, sys
Expand Down Expand Up @@ -51,7 +51,7 @@ lint: ## check style with flake8
flake8 src/cattr tests

test: ## run tests quickly with the default Python
pytest -x --ff
pytest -x --ff tests


test-all: ## run tests on every Python version with tox
Expand Down Expand Up @@ -87,3 +87,9 @@ dist: clean ## builds source and wheel package

install: clean ## install the package to the active Python's site-packages
python setup.py install

bench-cmp:
pytest bench --benchmark-compare

bench:
pytest bench --benchmark-save base
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,15 @@ characteristic_.

``cattrs`` is tested with Hypothesis_, by David R. MacIver.

``cattrs`` is benchmarked using perf_, by Victor Stinner.
``cattrs`` is benchmarked using perf_ and pytest-benchmark_.

This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.

.. _attrs: https://github.com/hynek/attrs
.. _characteristic: https://github.com/hynek/characteristic
.. _Hypothesis: http://hypothesis.readthedocs.io/en/latest/
.. _perf: https://github.com/haypo/perf
.. _pytest-benchmark: https://pytest-benchmark.readthedocs.io/en/latest/index.html
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage

Empty file added bench/__init__.py
Empty file.
189 changes: 0 additions & 189 deletions bench/bench.py

This file was deleted.

97 changes: 97 additions & 0 deletions bench/test_attrs_collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from enum import IntEnum
from typing import List

import attr
import pytest

from cattr import Converter, GenConverter, UnstructureStrategy


@pytest.mark.parametrize(
"converter_cls",
[Converter, GenConverter],
)
@pytest.mark.parametrize(
"unstructure_strat",
[UnstructureStrategy.AS_DICT, UnstructureStrategy.AS_TUPLE],
)
def test_unstructure_attrs_lists(benchmark, converter_cls, unstructure_strat):
"""
Benchmark a large (30 attributes) attrs class containing lists of
primitives.
"""

class E(IntEnum):
ONE = 1
TWO = 2

@attr.define
class C:
a: List[int]
b: List[float]
c: List[str]
d: List[bytes]
e: List[E]
f: List[int]
g: List[float]
h: List[str]
i: List[bytes]
j: List[E]
k: List[int]
l: List[float]
m: List[str]
n: List[bytes]
o: List[E]
p: List[int]
q: List[float]
r: List[str]
s: List[bytes]
t: List[E]
u: List[int]
v: List[float]
w: List[str]
x: List[bytes]
y: List[E]
z: List[int]
aa: List[float]
ab: List[str]
ac: List[bytes]
ad: List[E]

c = converter_cls(unstruct_strat=unstructure_strat)

benchmark(
c.unstructure,
C(
[1] * 3,
[1.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[E.ONE] * 3,
[2] * 3,
[2.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[E.TWO] * 3,
[3] * 3,
[3.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[E.ONE] * 3,
[4] * 3,
[4.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[E.TWO] * 3,
[5] * 3,
[5.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[E.ONE] * 3,
[6] * 3,
[6.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[E.TWO] * 3,
),
)
Loading