-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
141 lines (126 loc) · 5.53 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import json
import random
import time
import requests
bulk_test = 1
# 定义测试数据
sequences = {
# 1: {
# "name": "1a1x.A",
# "seq": "GSAGEDVGAPPDHLWVHQEGIYRDEYQRTWVAVVEEETSFLRARVQQIQVPLGDAARPSHLLTSQLPLMWQLYPEERYMDNNSRLWQIQHHLMVRGVQELLLKLLPDD",
# },
# 2: {
# "name": "1hh5.A",
# "seq": "MPKKIILICSPHIDDAASIFLAKGDPKINLLAVLTVVGGRSLDTNTKNALLVTDIFGIEGVPVAAGEEEPLVEGRKPKKDEPGEKGIGSIEYPPEFKNKLHGKHAVDLLIELILKYEPKTIILCPVGSLTNLATAIKEAPEIVERIKEIVFSGGGYTSGDATPVAEYTVYFDPEAAAIVFNTKLKVTMVGLDATAQALVTPEIKARIAAVGTRPAAFLLEVLEYYAKLKPAKKDEYGYLSDPLAVAYIIDPDVMTTRKAPASVDLDGEETVGTVVVDFEEPIPEECKTRVAVKVDYEKFWNMIVAALKRIGDPA",
# },
# 3: {
# "name": "1bkf.A",
# "seq": "GVQVETISPGDGRTFPKRGQTCVVHYTGMLEDGKKFDSSRDKNKPFKFMLGKQEVIRGWEEGVAQMSVGQRAKLTISPDYAYGATGVPGIIPPHATLVFDVELLKLE",
# },
# 4: {
# "name": "1ezs.A",
# "seq": "AESVQPLEKIAPYPQAEKGMKRQVIQLTPQEDESTLKVELLIGQTLEVDCNLHRLGGKLENKTLEGAAAAYYVFDKVSSPVSTRMACPDGKKEKKFVTAYLGDAGMLRYNSKLPIVVYTPDNVDVKYRVWKAEEKIDNAVVR",
# },
5: {
"seq": "MAEVIRSSAFWRSFPIFEEFDSETLCELSGIASYRKWSAGTVIFQRGDQGDYMIVVVSGRIKLSLFTPQGRELMLRQHEAGALFGEMALLDGQPRSADATAVTAAEGYVIGKKDFLALITQRPKTAEAVIRFLCAQLRDTTDRLETIALYDLNARVARFFLATLRQIHGSEMPQSANLRLTLSQTDIASILGASRPKVNRAILSLEESGAIKRADGIICCNVGRLLSIADPEEDLEHHHHHHHH",
"seq2": "MAEVIRSSAFWRSFPIFEEFDSETLCELSGIASYRKWSAGTVIFQRGDQGDYMIVVVSGRIKLSLFTPQGRELMLRQHEAGALFGEMALLDGQPRSADATAVTAAEGYVIGKKDFLALITQRPKTAEAVIRFLCAQLRDTTDRLETIALYDLNARVARFFLATLRQIHGSEMPQSANLRLTLSQTDIASILGASRPKVNRAILSLEESGAIKRADGIICCNVGRLLSIADPEEDLEHHHHHHHH",
},
}
# Assuming you have a function to generate test data
def generate_test_data():
choice = random.choice(list(sequences.keys()))
# Generate random sequence, name, and type
test_data = {
"seq": sequences[choice]["seq"],
"type": random.choice(
[
# "plddt",
# "tmscore",
"sc-tmscore",
# "pdb",
]
),
"seq2": sequences[choice].get("seq2", None), # Include seq2 if it exists
"name": sequences[choice].get("name", None), # Default name if not specified
}
# Include name if it exists in the sequences dictionary
if "name" in sequences[choice]:
test_data["name"] = sequences[choice]["name"]
return test_data
config_file = "./client.json"
with open(config_file, "r") as f:
config = json.load(f)
# Send POST requests with delay and exponential backoff
job_ids = {}
t_run = time.time()
for i in range(bulk_test):
print(f"Sending request {i+1}/{bulk_test}")
test_data = generate_test_data()
test_data_json = json.dumps(test_data)
retry_count = 0
sleep_base = 3
while retry_count < 5: # Maximum of 5 retries
try:
response = requests.post(
f"{config['server']}/predict/",
data=test_data_json,
headers={"Content-Type": "application/json"},
timeout=60, # Increase client-side timeout
)
if response.status_code == 200:
job_ids[response.json()["job_id"]] = -1
break
elif response.status_code == 429:
sleep_interval = sleep_base**retry_count
print(f"Job queue is full, retrying after delay... {sleep_interval}s")
time.sleep(sleep_interval) # Exponential backoff
elif response.status_code == 408:
sleep_interval = sleep_base**retry_count
print(f"Request timeout, retrying after delay... {sleep_interval}s")
time.sleep(sleep_interval) # Exponential backoff
elif response.status_code == 400:
print(f"Bad request: {response.text}")
break
elif response.status_code == 500:
print(f"Server error: {response.text}")
break
else:
print(f"Unexpected status code: {response.status_code}")
sleep_interval = sleep_base**retry_count
print(f"Request failed, retrying after delay... {sleep_interval}s")
time.sleep(sleep_interval) # Exponential backoff
retry_count += 1
except requests.exceptions.RequestException as e:
sleep_interval = sleep_base**retry_count
print(f"Request failed: {e}")
time.sleep(sleep_interval) # Exponential backoff
print(job_ids)
plddt_scores = {}
for id in job_ids:
print(f"Fetching result for job ID: {id}")
retry_count = 0
sleep_base = 3
while retry_count <= 5: # Maximum of 5 retries
try:
response = requests.get(
f"{config['server']}/result/{id}",
headers={"Content-Type": "application/json"},
timeout=60, # Increase client-side timeout
)
if response.status_code == 200:
job_ids[id] = response.json()
break
elif response.status_code == 202:
sleep_interval = sleep_base**retry_count
print(
f"Job is processing, be patient, retrying after delay... {sleep_interval}s"
)
time.sleep(sleep_interval) # Exponential backoff
retry_count += 1
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
time.sleep(2**retry_count) # Exponential backoff
t_run_done = time.time() - t_run
# Print response content
print(json.dumps(job_ids, indent=4))
print(f"Time taken: {t_run_done:.4f} seconds")