Skip to content

Commit 8fbf59b

Browse files
committed
refactor benchmark tests
1 parent b3912a7 commit 8fbf59b

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

tests/test_benchmark.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
""" This module contains benchmark tests intended to guide development of a performant codebase. """
2+
import nacl
3+
import pysodium
24
import pytest
35

4-
from paseto.protocol.version2 import Version2
56
from paseto.crypto import primitives
7+
from paseto.protocol.version2 import Version2
68

79
KEY = b"0" * 32
810
MESSAGE = b"foo"
@@ -12,18 +14,17 @@
1214
@pytest.mark.benchmark(group="encrypt")
1315
def test_encrypt_one(benchmark):
1416
"""Benchmark only encryption."""
15-
from nacl.bindings import crypto_aead_xchacha20poly1305_ietf_encrypt as encrypt
16-
primitives.encrypt = encrypt
17+
primitives.encrypt = nacl.bindings.crypto_aead_xchacha20poly1305_ietf_encrypt
1718

1819
token = benchmark(Version2.encrypt, MESSAGE, KEY, FOOTER)
1920
plain_text = Version2.decrypt(token, KEY, FOOTER)
2021
assert plain_text == MESSAGE
2122

23+
2224
@pytest.mark.benchmark(group="encrypt")
2325
def test_encrypt_two(benchmark):
2426
"""Benchmark only encryption."""
25-
from pysodium import crypto_aead_xchacha20poly1305_ietf_encrypt as encrypt
26-
primitives.encrypt = encrypt
27+
primitives.encrypt = pysodium.crypto_aead_xchacha20poly1305_ietf_encrypt
2728

2829
token = benchmark(Version2.encrypt, MESSAGE, KEY, FOOTER)
2930
plain_text = Version2.decrypt(token, KEY, FOOTER)
@@ -33,8 +34,7 @@ def test_encrypt_two(benchmark):
3334
@pytest.mark.benchmark(group="decrypt")
3435
def test_decrypt_one(benchmark):
3536
"""Benchmark only decryption."""
36-
from nacl.bindings import crypto_aead_xchacha20poly1305_ietf_decrypt as decrypt
37-
primitives.decrypt = decrypt
37+
primitives.decrypt = nacl.bindings.crypto_aead_xchacha20poly1305_ietf_decrypt
3838

3939
token = Version2.encrypt(MESSAGE, KEY, FOOTER)
4040
plain_text = benchmark(Version2.decrypt, token, KEY, FOOTER)
@@ -44,20 +44,18 @@ def test_decrypt_one(benchmark):
4444
@pytest.mark.benchmark(group="decrypt")
4545
def test_decrypt_two(benchmark):
4646
"""Benchmark only decryption."""
47-
from pysodium import crypto_aead_xchacha20poly1305_ietf_decrypt as decrypt
48-
primitives.decrypt = decrypt
47+
primitives.decrypt = pysodium.crypto_aead_xchacha20poly1305_ietf_decrypt
4948

5049
token = Version2.encrypt(MESSAGE, KEY, FOOTER)
5150
plain_text = benchmark(Version2.decrypt, token, KEY, FOOTER)
5251
assert plain_text == MESSAGE
5352

53+
5454
@pytest.mark.benchmark(group="encrypt_and_decrypt")
5555
def test_encrypt_and_decrypt_one(benchmark):
5656
"""Benchmark encryption and decryption run together."""
57-
from nacl.bindings import crypto_aead_xchacha20poly1305_ietf_encrypt as encrypt
58-
primitives.encrypt = encrypt
59-
from nacl.bindings import crypto_aead_xchacha20poly1305_ietf_decrypt as decrypt
60-
primitives.decrypt = decrypt
57+
primitives.encrypt = nacl.bindings.crypto_aead_xchacha20poly1305_ietf_encrypt
58+
primitives.decrypt = nacl.bindings.crypto_aead_xchacha20poly1305_ietf_decrypt
6159

6260
def encrypt_and_decrypt():
6361
token = Version2.encrypt(MESSAGE, KEY, FOOTER)
@@ -66,13 +64,12 @@ def encrypt_and_decrypt():
6664
plain_text = benchmark(encrypt_and_decrypt)
6765
assert plain_text == MESSAGE
6866

67+
6968
@pytest.mark.benchmark(group="encrypt_and_decrypt")
7069
def test_encrypt_and_decrypt_two(benchmark):
7170
"""Benchmark encryption and decryption run together."""
72-
from pysodium import crypto_aead_xchacha20poly1305_ietf_decrypt as decrypt
73-
primitives.decrypt = decrypt
74-
from pysodium import crypto_aead_xchacha20poly1305_ietf_encrypt as encrypt
75-
primitives.encrypt = encrypt
71+
primitives.decrypt = pysodium.crypto_aead_xchacha20poly1305_ietf_decrypt
72+
primitives.encrypt = pysodium.crypto_aead_xchacha20poly1305_ietf_encrypt
7673

7774
def encrypt_and_decrypt():
7875
token = Version2.encrypt(MESSAGE, KEY, FOOTER)

0 commit comments

Comments
 (0)