Skip to content

Commit

Permalink
Merge pull request #56 from keis/pytest-benchmark
Browse files Browse the repository at this point in the history
Add performance benchmarks using pytest-benchmark
  • Loading branch information
keis committed Jun 12, 2020
2 parents 13080e2 + 116d2e2 commit b1ee8e5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ script:
pip install mypy &&
mypy .
fi
- pytest --flake8 --cov=base58 .
- pytest --flake8 --cov=base58 --benchmark-disable .
after_success:
coveralls
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ incremental = False
[mypy-setuptools.*]
ignore_missing_imports = True

[mypy-pytest.*]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pytest-flake8
pytest-cov
PyHamcrest>=2.0.2
coveralls
pytest-benchmark
17 changes: 17 additions & 0 deletions test_base58.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
from itertools import product
from random import getrandbits
from hamcrest import assert_that, equal_to, calling, raises
from base58 import (
b58encode, b58decode, b58encode_check, b58decode_check, b58encode_int,
Expand Down Expand Up @@ -98,3 +100,18 @@ def test_alphabet_alias_exists_and_equals_bitcoin_alphabet():
def test_invalid_input():
data = 'xyz0' # 0 is not part of the bitcoin base58 alphabet
assert_that(calling(b58decode).with_args(data), raises(ValueError))


@pytest.mark.parametrize('length', [8, 32, 256, 1024])
def test_encode_random(benchmark, length) -> None:
data = getrandbits(length * 8).to_bytes(length, byteorder='big')
encoded = benchmark(lambda: b58encode(data))
assert_that(b58decode(encoded), equal_to(data))


@pytest.mark.parametrize('length', [8, 32, 256, 1024])
def test_decode_random(benchmark, length) -> None:
origdata = getrandbits(length * 8).to_bytes(length, byteorder='big')
encoded = b58encode(origdata)
data = benchmark(lambda: b58decode(encoded))
assert_that(data, equal_to(origdata))

0 comments on commit b1ee8e5

Please sign in to comment.