Skip to content

Commit 2b8c0bb

Browse files
authored
Add python script for Load Test (#57)
1 parent 5b3aee5 commit 2b8c0bb

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

performance-testing/uid2-operator/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# UID2 Operator Performance Testing Tool
22
The following instructions will work for any operator.
33

4-
## Steps
4+
## k6 Scripts
55
### Step 1 - Configure the K6 script
66
The script as checked in has a basic config that will run a check against all endpoints.
77

@@ -38,3 +38,29 @@ Set environment variables `CLIENT_KEY`, `CLIENT_SECRET`, `BASE_URL` and then use
3838
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
3939
```
4040
41+
## Python Locust Scripts
42+
### Step 1 - Configure Virtual Environment
43+
In order to run the python script, best practise is to create a virtual environment:
44+
```
45+
python3 -m venv venv
46+
source venv/bin/activate
47+
```
48+
49+
### Step 2 - Modify `uid2-client-python` to include eclapsed time
50+
See [this](https://github.com/IABTechLab/uid2-client-python/pull/55) as an example.
51+
52+
Once modified the `uid2-client-python` repo, run `python3 -m pip install -e /Users/katherine.chen/ttdsrc/uid2-client-python`
53+
54+
### Step 3 - Install Required Packages
55+
Once you've sourced into the venv, install required packages:
56+
```
57+
python3 -m pip install locust
58+
python3 -m pip install -e <path to uid2-client-python>
59+
```
60+
61+
### Step 4 - Run Load Test
62+
Run below command to start the load test:
63+
```
64+
venv/bin/python -m locust -f performance-testing/uid2-operator/locust-identity-map.py <base_url> <client_key> <client_secret>
65+
```
66+
Press enter again to navigate to localhost:8089, ane put down the desired arguments.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import json
2+
import time
3+
import sys
4+
from locust import User, task, between, events, TaskSet
5+
from locust.runners import MasterRunner
6+
from uid2_client import IdentityMapClient, IdentityMapInput
7+
8+
def _usage():
9+
print('Usage: venv/bin/python -m locust -f performance-testing/uid2-operator/locust-identity-map.py <base_url> <client_key> <client_secret>'
10+
, file=sys.stderr)
11+
sys.exit(1)
12+
13+
14+
if len(sys.argv) <= 4:
15+
_usage()
16+
17+
base_url = sys.argv[1]
18+
client_key = sys.argv[2]
19+
client_secret = sys.argv[3]
20+
email_count = 5000
21+
22+
class IdentityMapTasks(TaskSet):
23+
@task
24+
def identity_map_large_batch_task(self):
25+
try:
26+
response = self.user.identityMapClient.generate_identity_map(
27+
IdentityMapInput.from_emails([f"test{i}@example.com" for i in range(email_count)])
28+
)
29+
if response.is_success():
30+
self.user.environment.runner.stats.log_request("IdentityMap", "generate_identity_map", response.elapsed_time * 1000, len(response.response)) # Log successful request
31+
else:
32+
self.user.environment.runner.stats.log_error("IdentityMap", "generate_identity_map", f"Failed with status: {response.status}")
33+
except Exception as e:
34+
self.user.environment.runner.stats.log_error("IdentityMap", "generate_identity_map", f"Exception: {e}")
35+
print(f"Error in identity_map_large_batch_task: {e}")
36+
37+
class IdentityMapUser(User):
38+
wait_time = between(1, 2)
39+
40+
def __init__(self, *args, **kwargs):
41+
super().__init__(*args, **kwargs)
42+
self.identityMapClient = IdentityMapClient(
43+
base_url,
44+
client_key,
45+
client_secret)
46+
self.tasks = [IdentityMapTasks] # Assign TaskSet
47+
48+
# Handle summary data
49+
def on_locust_stop(environment, **kwargs):
50+
if isinstance(environment.runner, MasterRunner):
51+
summary = {
52+
"start_time": environment.runner.start_time,
53+
"end_time": time.time(),
54+
"duration": time.time() - environment.runner.start_time,
55+
"stats": environment.runner.stats.serialize_stats(),
56+
}
57+
with open("locust_summary.json", "w") as f:
58+
json.dump(summary, f, indent=4)
59+
print("Locust summary saved to locust_summary.json")
60+
61+
events.quitting.add_listener(on_locust_stop)

0 commit comments

Comments
 (0)