diff --git a/performance-testing/uid2-operator/README.md b/performance-testing/uid2-operator/README.md index 89ba6bd..27dcbd6 100644 --- a/performance-testing/uid2-operator/README.md +++ b/performance-testing/uid2-operator/README.md @@ -1,7 +1,7 @@ # UID2 Operator Performance Testing Tool The following instructions will work for any operator. -## Steps +## k6 Scripts ### Step 1 - Configure the K6 script The script as checked in has a basic config that will run a check against all endpoints. @@ -38,3 +38,29 @@ Set environment variables `CLIENT_KEY`, `CLIENT_SECRET`, `BASE_URL` and then use k6 run k6-uid2-operator.js -e CLIENT_KEY=$CLIENT_KEY -e CLIENT_SECRET=$CLIENT_SECRET -e BASE_URL=$BASE_URL -e REFRESH_TOKEN=$REFRESH_TOKEN ``` +## Python Locust Scripts +### Step 1 - Configure Virtual Environment +In order to run the python script, best practise is to create a virtual environment: +``` +python3 -m venv venv +source venv/bin/activate +``` + +### Step 2 - Modify `uid2-client-python` to include eclapsed time +See [this](https://github.com/IABTechLab/uid2-client-python/pull/55) as an example. + +Once modified the `uid2-client-python` repo, run `python3 -m pip install -e /Users/katherine.chen/ttdsrc/uid2-client-python` + +### Step 3 - Install Required Packages +Once you've sourced into the venv, install required packages: +``` +python3 -m pip install locust +python3 -m pip install -e +``` + +### Step 4 - Run Load Test +Run below command to start the load test: +``` +venv/bin/python -m locust -f performance-testing/uid2-operator/locust-identity-map.py +``` +Press enter again to navigate to localhost:8089, ane put down the desired arguments. \ No newline at end of file diff --git a/performance-testing/uid2-operator/locust-identity-map.py b/performance-testing/uid2-operator/locust-identity-map.py new file mode 100644 index 0000000..148d6cd --- /dev/null +++ b/performance-testing/uid2-operator/locust-identity-map.py @@ -0,0 +1,61 @@ +import json +import time +import sys +from locust import User, task, between, events, TaskSet +from locust.runners import MasterRunner +from uid2_client import IdentityMapClient, IdentityMapInput + +def _usage(): + print('Usage: venv/bin/python -m locust -f performance-testing/uid2-operator/locust-identity-map.py ' + , file=sys.stderr) + sys.exit(1) + + +if len(sys.argv) <= 4: + _usage() + +base_url = sys.argv[1] +client_key = sys.argv[2] +client_secret = sys.argv[3] +email_count = 5000 + +class IdentityMapTasks(TaskSet): + @task + def identity_map_large_batch_task(self): + try: + response = self.user.identityMapClient.generate_identity_map( + IdentityMapInput.from_emails([f"test{i}@example.com" for i in range(email_count)]) + ) + if response.is_success(): + self.user.environment.runner.stats.log_request("IdentityMap", "generate_identity_map", response.elapsed_time * 1000, len(response.response)) # Log successful request + else: + self.user.environment.runner.stats.log_error("IdentityMap", "generate_identity_map", f"Failed with status: {response.status}") + except Exception as e: + self.user.environment.runner.stats.log_error("IdentityMap", "generate_identity_map", f"Exception: {e}") + print(f"Error in identity_map_large_batch_task: {e}") + +class IdentityMapUser(User): + wait_time = between(1, 2) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.identityMapClient = IdentityMapClient( + base_url, + client_key, + client_secret) + self.tasks = [IdentityMapTasks] # Assign TaskSet + +# Handle summary data +def on_locust_stop(environment, **kwargs): + if isinstance(environment.runner, MasterRunner): + summary = { + "start_time": environment.runner.start_time, + "end_time": time.time(), + "duration": time.time() - environment.runner.start_time, + "stats": environment.runner.stats.serialize_stats(), + } + with open("locust_summary.json", "w") as f: + json.dump(summary, f, indent=4) + print("Locust summary saved to locust_summary.json") + +events.quitting.add_listener(on_locust_stop) \ No newline at end of file