Skip to content

Commit 3b57dbb

Browse files
committed
Add python script
1 parent 37f0a9f commit 3b57dbb

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
21+
# base_url = "http://135.237.2.132"
22+
# client_secret = "AL1igbD8V6c9YjrmkapVL0vdtwvI7aP4wAWbCoqYEMw="
23+
# client_key = "UID2-C-I-146-h/Ju95.+SM3BKYyEIkWcGPK2OOVXCzoGezunDCKgeXYs="
24+
email_count = 5000
25+
26+
class IdentityMapTasks(TaskSet):
27+
@task
28+
def identity_map_large_batch_task(self):
29+
try:
30+
response = self.user.identityMapClient.generate_identity_map(
31+
IdentityMapInput.from_emails([f"test{i}@example.com" for i in range(email_count)])
32+
)
33+
if response.is_success():
34+
self.user.environment.runner.stats.log_request("IdentityMap", "generate_identity_map", response.elapsed_time * 1000, len(response.response)) # Log successful request
35+
else:
36+
self.user.environment.runner.stats.log_error("IdentityMap", "generate_identity_map", f"Failed with status: {response.status}")
37+
except Exception as e:
38+
self.user.environment.runner.stats.log_error("IdentityMap", "generate_identity_map", f"Exception: {e}")
39+
print(f"Error in identity_map_large_batch_task: {e}")
40+
41+
class IdentityMapUser(User):
42+
wait_time = between(1, 2)
43+
44+
def __init__(self, *args, **kwargs):
45+
super().__init__(*args, **kwargs)
46+
self.identityMapClient = IdentityMapClient(
47+
base_url,
48+
client_key,
49+
client_secret)
50+
self.tasks = [IdentityMapTasks] # Assign TaskSet
51+
52+
# Handle summary data
53+
def on_locust_stop(environment, **kwargs):
54+
if isinstance(environment.runner, MasterRunner):
55+
summary = {
56+
"start_time": environment.runner.start_time,
57+
"end_time": time.time(),
58+
"duration": time.time() - environment.runner.start_time,
59+
"stats": environment.runner.stats.serialize_stats(),
60+
}
61+
with open("locust_summary.json", "w") as f:
62+
json.dump(summary, f, indent=4)
63+
print("Locust summary saved to locust_summary.json")
64+
65+
events.quitting.add_listener(on_locust_stop)

0 commit comments

Comments
 (0)