-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_test.py
48 lines (40 loc) · 1.5 KB
/
load_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from locust import HttpUser, task, between
import time
class QuickstartUser(HttpUser):
host = "http://localhost:8000"
wait_time = between(1, 30)
@task(1)
def run_code(self):
code = """class Solution:
def mergeAlternately(self, word1, word2):
merged = []
i, j = 0, 0
while i < len(word1) and j < len(word2):
merged.append(word1[i])
merged.append(word2[j])
i += 1
j += 1
# Append remaining characters from word1 if any
merged.extend(word1[i:])
# Append remaining characters from word2 if any
merged.extend(word2[j:])
return ''.join(merged)"""
response = self.client.post("/run_code", json={"problem_id": "1894", "code": code})
if response.status_code == 200:
job_id = response.json().get("job_id")
if job_id:
self.wait_for_completion(job_id)
def wait_for_completion(self, job_id):
max_attempts = 10
delay = 1
for attempt in range(max_attempts):
response = self.client.post("/check/status", json={"job_id": job_id})
if response.status_code == 200:
status = response.json().get("status")
if status == "done":
return
elif status == "error":
# Handle error case
break
delay *= 1.5 # Exponential backoff
time.sleep(delay)