Skip to content

Commit c8b27bb

Browse files
committed
fix
1 parent ae76693 commit c8b27bb

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

examples/src/multithreading/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Dict # noqa pylint: disable=wrong-import-order
66

77
import aws_encryption_sdk
8+
import time
89

910

1011
def encrypt_and_decrypt_with_keyring(
@@ -44,3 +45,18 @@ def encrypt_and_decrypt_with_keyring(
4445
)
4546

4647
return decrypted_plaintext_data
48+
49+
50+
def run_encrypt_and_decrypt_with_keyring_for_duration_seconds(
51+
plaintext_data: bytes,
52+
keyring: IKeyring,
53+
client: aws_encryption_sdk.EncryptionSDKClient,
54+
duration: int = 2
55+
):
56+
"""Helper function to repeatedly run an encrypt and decrypt cycle for 'duration' seconds."""
57+
time_end = time.time() + duration
58+
59+
while time.time() < time_end:
60+
decrypted_plaintext_data = encrypt_and_decrypt_with_keyring(plaintext_data, keyring, client)
61+
assert decrypted_plaintext_data == plaintext_data, \
62+
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/test/multithreading/test_i_raw_aes_keyring_multithreaded_example.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,43 @@
88
import aws_encryption_sdk
99
from aws_encryption_sdk import CommitmentPolicy
1010

11-
from ...src.multithreading import encrypt_and_decrypt_with_keyring
11+
from ...src.multithreading import run_encrypt_and_decrypt_with_keyring_for_duration_seconds
1212
from ...src.multithreading.raw_aes_keyring import create_keyring
1313

14+
import time
15+
1416
pytestmark = [pytest.mark.examples]
1517

1618

17-
def test_encrypt_and_decrypt_with_keyring(n_threads=10):
18-
"""Test function for multi-threaded encrypt and decrypt using the Raw AES Keyring example."""
19+
def test_encrypt_and_decrypt_with_keyring_multithreaded_helper(n_threads=16, duration=60):
20+
"""Helper function for multi-threaded encrypt and decrypt using a keyring for fixed n_threads and duration."""
21+
print(n_threads, duration)
22+
start_time = time.time()
23+
print('start_time', start_time)
1924
keyring = create_keyring()
2025
plaintext_data = b"Hello World"
2126
client = aws_encryption_sdk.EncryptionSDKClient(
2227
commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
2328
)
2429

2530
with ThreadPoolExecutor(max_workers=n_threads) as executor:
26-
thread_futures = {executor.submit(encrypt_and_decrypt_with_keyring,
31+
thread_futures = {executor.submit(run_encrypt_and_decrypt_with_keyring_for_duration_seconds,
2732
plaintext_data=plaintext_data,
2833
keyring=keyring,
29-
client=client): i for i in range(n_threads)}
34+
client=client,
35+
duration=duration): i for i in range(n_threads)}
3036

3137
for future in as_completed(thread_futures):
32-
decrypted_plaintext_data = future.result()
33-
assert decrypted_plaintext_data == plaintext_data, \
34-
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
38+
future.result()
39+
end_time = time.time()
40+
print('end_time', end_time)
41+
print('duration', end_time - start_time)
42+
43+
44+
# def test_encrypt_and_decrypt_with_keyring_multithreaded(n_threads_list: list = [4, 16, 64], duration_list: list = [2, 10, 60]):
45+
# """Test function for multi-threaded encrypt and decrypt using a keyring for different n_threads and duration."""
46+
# print('hello', n_threads_list, duration_list)
47+
# for n in n_threads_list:
48+
# for d in duration_list:
49+
# print(n, d, time.time())
50+
# encrypt_and_decrypt_with_keyring_helper(n_threads=n, duration=d)

0 commit comments

Comments
 (0)