Created
January 26, 2022 10:16
-
-
Save florin-talarian/959f1092ae2ce608b193330a8b612390 to your computer and use it in GitHub Desktop.
Lock Service API Perf Testing With Locust
This file contains 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
from uuid import uuid4 | |
from random import randint, choice | |
from locust import HttpUser, task, between | |
UUIDS = [str(uuid4()) for _ in range(1000)] | |
class LockServiceLoadTesting(HttpUser): | |
def on_start(self): | |
self.client.headers = {"X-LockService-User": "49ac1e09-7267-4c2c-a4bd-6fac615ccce0"} | |
@task | |
def acquire_non_unique_lock(self): | |
with self.client.get(f"/{choice(UUIDS)}?waitTimeMs=0&ttlMs=5000", name='only_acquire_lock') as response: | |
try: | |
assert len(response.content) == 32 | |
except AssertionError: | |
assert response.content == b"", "resp cont is " + str(response.content) | |
@task | |
def acquire_unique_lock(self): | |
with self.client.get(f"/{str(uuid4())}?waitTimeMs=0&ttlMs=5000", name='only_acquire_lock') as response: | |
try: | |
assert len(response.content) == 32 | |
except AssertionError: | |
assert response.content == b"", "resp cont is " + str(response.content) | |
@task | |
def release_lock(self): | |
with self.client.delete(f"/{str(uuid4())}?permitId=123456", name='only_release_lock') as response: | |
assert response.content == b'false' | |
@task | |
def acquire_and_release_non_unique_lock(self): | |
acquired_lock_id = choice(UUIDS) | |
with self.client.get(f"/{acquired_lock_id}?waitTimeMs=300&ttlMs=5000", name='acquire_lock') as response: | |
try: | |
assert len(response.content) == 32 | |
if randint(0, 1) == 1: | |
permitId = response.content.decode("utf-8") | |
with self.client.delete(f"/{acquired_lock_id}?permitId={permitId}", name='release_lock') as response2: | |
assert response2.content == b'true' | |
except Exception: | |
assert response.content == b"", "resp cont is " + str(response.content) | |
@task | |
def acquire_and_release_unique_lock(self): | |
acquired_lock_id = str(uuid4()) | |
with self.client.get(f"/{acquired_lock_id}?waitTimeMs=0&ttlMs=5000", name='acquire_lock') as response: | |
try: | |
assert len(response.content) == 32 | |
permitId = response.content.decode("utf-8") | |
with self.client.delete(f"/{acquired_lock_id}?permitId={permitId}", name='release_lock') as response2: | |
assert response2.content == b'true' | |
except Exception: | |
assert response.content == b"", "resp cont is " + str(response.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment