From 1a0150d7b8733d7fe071d2cc5d01563f6ebbd536 Mon Sep 17 00:00:00 2001 From: Viktor Date: Tue, 29 Oct 2024 08:29:10 +0000 Subject: [PATCH 1/2] Added stress and load test --- tests/load_stress_test.py | 82 +++++++++++++++++++++++++++++++ tests/test_jsons/load_stress.json | 16 ++++++ 2 files changed, 98 insertions(+) create mode 100644 tests/load_stress_test.py create mode 100644 tests/test_jsons/load_stress.json diff --git a/tests/load_stress_test.py b/tests/load_stress_test.py new file mode 100644 index 0000000..e27ce9f --- /dev/null +++ b/tests/load_stress_test.py @@ -0,0 +1,82 @@ +import pytest +import requests +import time +import json +import os +from concurrent.futures import ThreadPoolExecutor, as_completed + +base_url = "http://localhost:8080" +script_directory = os.path.dirname(os.path.abspath(__file__)) + +# Helper functions +def _post_request(url, payload={}): + """Send a POST HTTP request, test the status, jsonization and return the response in JSON.""" + resp = requests.post(f"{base_url}{url}", json=payload) + assert resp.status_code == 200 + assert resp.json() + return resp.json() + +def _get_request(url): + """Send a GET HTTP request, test the status, jsonization and return the response in JSON.""" + resp = requests.get(f"{base_url}{url}") + assert resp.status_code == 200 + assert resp.json() + return resp.json() + +def _wait_for_state(task_id, target_state, timeout=60): + """Wait for a task to reach the desired state (e.g., COMPLETE, CANCELED) or timeout.""" + for _ in range(timeout): + resp = _get_request(f"/v1/tasks/{task_id}") + state = resp.get("state") + if state == target_state: + return resp + time.sleep(1) + raise Exception(f"Task {task_id} did not reach state {target_state} within {timeout} seconds") + +def _open_json(filename): + """Open a JSON file from the test_jsons directory.""" + with open(f"{script_directory}/test_jsons/{filename}", 'r') as f: + data = f.read() + return json.loads(data) + + +# Test function to stress test by submitting multiple tasks using JSON files +def test_stress_multiple_task_submission(): + task_filename = "load_stress.json" # JSON file defining the task + task_payload = _open_json(task_filename) + + num_tasks = 10 # Number of tasks to submit + submitted_tasks = [] + + # Submit tasks concurrently using ThreadPoolExecutor + with ThreadPoolExecutor(max_workers=10) as executor: + futures = [executor.submit(_post_request, "/v1/tasks", task_payload) for _ in range(num_tasks)] + + # Collect the submitted task IDs + for future in as_completed(futures): + try: + response = future.result() + task_id = response.get("id") + assert task_id, "No task ID found in response" + submitted_tasks.append(task_id) + except Exception as e: + pytest.fail(f"Task submission failed: {e}") + + # Ensure all tasks were submitted successfully + assert len(submitted_tasks) == num_tasks, f"Only {len(submitted_tasks)} out of {num_tasks} tasks were submitted" + + # Check if all tasks eventually complete + completed_tasks = 0 + for task_id in submitted_tasks: + try: + _wait_for_state(task_id, "COMPLETE", timeout=60) # Wait for each task to complete + completed_tasks += 1 + except Exception as e: + print(f"Task {task_id} failed to complete: {e}") + + # Ensure that all tasks completed successfully + assert completed_tasks == num_tasks, f"Only {completed_tasks} out of {num_tasks} tasks completed" + +if __name__ == "__main__": + pytest.main() + diff --git a/tests/test_jsons/load_stress.json b/tests/test_jsons/load_stress.json new file mode 100644 index 0000000..e1950a4 --- /dev/null +++ b/tests/test_jsons/load_stress.json @@ -0,0 +1,16 @@ +{ + "resources": { + "cpu_cores": 1, + "preemptible": false, + "ram_gb": 1, + "disk_gb": 1, + "zones": ["us-west-1"] + }, + "executors":[ + { + "image":"ubuntu", + "command":[ + ] + } + ] +} From 1d096c0651ed738d561ce79149250cc51259cd07 Mon Sep 17 00:00:00 2001 From: Viktor Date: Thu, 14 Nov 2024 11:18:44 +0000 Subject: [PATCH 2/2] Updated load_stress_test.py --- tests/load_stress_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/load_stress_test.py b/tests/load_stress_test.py index e27ce9f..c52c8b8 100644 --- a/tests/load_stress_test.py +++ b/tests/load_stress_test.py @@ -23,7 +23,7 @@ def _get_request(url): assert resp.json() return resp.json() -def _wait_for_state(task_id, target_state, timeout=60): +def _wait_for_state(task_id, target_state, timeout=100): """Wait for a task to reach the desired state (e.g., COMPLETE, CANCELED) or timeout.""" for _ in range(timeout): resp = _get_request(f"/v1/tasks/{task_id}") @@ -45,7 +45,7 @@ def test_stress_multiple_task_submission(): task_filename = "load_stress.json" # JSON file defining the task task_payload = _open_json(task_filename) - num_tasks = 10 # Number of tasks to submit + num_tasks = 100 # Number of tasks to submit submitted_tasks = [] # Submit tasks concurrently using ThreadPoolExecutor @@ -69,7 +69,7 @@ def test_stress_multiple_task_submission(): completed_tasks = 0 for task_id in submitted_tasks: try: - _wait_for_state(task_id, "COMPLETE", timeout=60) # Wait for each task to complete + _wait_for_state(task_id, "COMPLETE", timeout=100) # Wait for each task to complete completed_tasks += 1 except Exception as e: print(f"Task {task_id} failed to complete: {e}")