-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_server.py
144 lines (121 loc) · 3.75 KB
/
test_server.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
142
143
144
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 22-7-2 上午1:12
# @Author : MaybeShewill-CV
# @Site : https://github.com/MaybeShewill-CV/MMAiServer
# @File : test_server.py
# @IDE: PyCharm
"""
test server
"""
import argparse
import os
import os.path as ops
import requests
import base64
import json
import hashlib
import time
import locust
from config_utils import parse_config_utils
CFG_MAP = parse_config_utils.cfg_map
def init_args():
"""
int args
"""
parser = argparse.ArgumentParser()
parser.add_argument('--server', type=str, help='model name')
parser.add_argument('--mode', type=str, help='test mode')
return parser.parse_args()
def single_test_mode(url, src_image_path, loop_times):
"""_summary_
Args:
url (_type_): _description_
src_image_path (_type_): _description_
loop_times (_type_): _description_
"""
assert ops.exists(src_image_path), '{:s} not exist'.format(src_image_path)
with open(src_image_path, 'rb') as f:
image_data = f.read()
base64_data = base64.b64encode(image_data)
task_id = src_image_path + str(time.time())
m2 = hashlib.md5()
m2.update(task_id.encode())
task_id = m2.hexdigest()
post_data = {
'img_data': base64_data.decode(),
'req_id': task_id,
}
for i in range(loop_times):
try:
resp = requests.post(url=url, data=json.dumps(post_data))
print(resp.text[:200])
except Exception as e:
print(e)
return
def locust_test_mode(url, src_image_path, u, r, t):
"""_summary_
Args:
url (_type_): _description_
src_image_path (_type_): _description_
u (_type_): _description_
r (_type_): _description_
t (_type_): _description_
"""
content = open('./server/locust_performance.py', 'r').readlines()
content[17] = 'URL = \'{:s}\'\n'.format(url)
content[18] = 'SRC_IMAGE_PATH = \'{:s}\'\n'.format(src_image_path)
open('./server/locust_performance.py', 'w').writelines(content)
command = 'locust -f ./server/locust_performance.py --host={:s} --headless -u {:d} -r {:d} -t {:s}'.format(url, u, r, t)
os.system(command=command)
return
def main_process():
"""
main func
"""
args = init_args()
server_name = args.server.lower()
if server_name not in CFG_MAP:
print('No valid configuration for model: {:s}'.format(server_name))
print('Supported servers are listed: ')
print(CFG_MAP.keys())
return
test_mode = args.mode
if test_mode not in ['single', 'locust']:
print('Only support \'single\' and \'locust\' mode')
return
cfg = CFG_MAP[server_name]
model_name = cfg.MODEL_NAME
url = cfg.URL
source_image_path = cfg.SOURCE_IMAGE_PATH
if test_mode == 'single':
loop_times = cfg.SINGLE.LOOP_TIMES
print('Start test server for model: {:s}, mode {:s}'.format(model_name, test_mode))
single_test_mode(
url=url,
src_image_path=source_image_path,
loop_times=loop_times
)
else:
u = cfg.LOCUST.U
r = cfg.LOCUST.R
t = cfg.LOCUST.T
print('Start test server for model: {:s}, mode {:s}'.format(model_name, test_mode))
locust_test_mode(
url=url,
src_image_path=source_image_path,
u=u,
r=r,
t=t
)
content = open('./server/locust_performance.py', 'r').readlines()
content[17] = 'URL = \'\'\n'
content[18] = 'SRC_IMAGE_PATH = \'\'\n'
open('./server/locust_performance.py', 'w').writelines(content)
print('Complete test')
return
if __name__ == '__main__':
"""
main func
"""
main_process()