save code
This commit is contained in:
+91
-62
@@ -5,77 +5,79 @@ import base64
|
||||
import time
|
||||
import random
|
||||
|
||||
url = 'http://mq.aidigifi.meidaojia.com/api/swapHair/v1'
|
||||
# url = 'https://692524911165445-http-8801.northwest1.gpugeek.com:8443/api/swapHair/v1'
|
||||
# url = 'http://mq.aidigifi.meidaojia.com/api/swapHair/v1'
|
||||
url = 'https://693570664882181-http-8801.northwest1.gpugeek.com:8443/api/swapHair/v1'
|
||||
# url = 'https://692502023221253-http-8801.northwest1.gpugeek.com:8443/api/swapHair/v1'
|
||||
# url = 'https://692493464571909-http-8801.northwest1.gpugeek.com:8443/api/swapHair/v1'
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
data = {
|
||||
"hair_id": "1907651680352395265",
|
||||
"task_id": "1907651680352395265",
|
||||
"user_img_path": "https://cdn.meidaojia.com/ZoeFiles/user2_1_%E5%89%AF%E6%9C%AC.JPG",
|
||||
# "user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_8.JPG",
|
||||
"is_hr": "false",
|
||||
"output_format": "base64"
|
||||
}
|
||||
|
||||
more_data = []
|
||||
more_data.append(data)
|
||||
|
||||
data1 = {
|
||||
"hair_id": "1907651680352395265",
|
||||
"task_id": "2345623451234",
|
||||
# "hair_id": "1907651680352395265",
|
||||
"hair_id": "1907651525024735234",
|
||||
"task_id": "345564352345",
|
||||
#"user_img_path": "https://cdn.meidaojia.com/ZoeFiles/user2_1_%E5%89%AF%E6%9C%AC.JPG",
|
||||
# "user_img_path": "http://git.xiangsilian.com/imgs/test.jpg",
|
||||
"user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_8.JPG",
|
||||
"is_hr": "false",
|
||||
"output_format": "base64"
|
||||
}
|
||||
more_data.append(data1)
|
||||
|
||||
data2 = {
|
||||
"hair_id": "1907651680352395265",
|
||||
"task_id": "3145645324",
|
||||
"user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_7.JPG",
|
||||
"is_hr": "false",
|
||||
"output_format": "base64"
|
||||
}
|
||||
more_data.append(data2)
|
||||
# more_data = []
|
||||
# more_data.append(data)
|
||||
|
||||
data3 = {
|
||||
"hair_id": "1907651680352395265",
|
||||
"task_id": "456412345",
|
||||
"user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_6.JPG",
|
||||
"is_hr": "false",
|
||||
"output_format": "base64"
|
||||
}
|
||||
more_data.append(data3)
|
||||
# data1 = {
|
||||
# "hair_id": "1907651680352395265",
|
||||
# "task_id": "2345623451234",
|
||||
# "user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_8.JPG",
|
||||
# "is_hr": "false",
|
||||
# "output_format": "base64"
|
||||
# }
|
||||
# more_data.append(data1)
|
||||
|
||||
data4 = {
|
||||
"hair_id": "1907651680352395265",
|
||||
"task_id": "45623445",
|
||||
"user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_5.JPG",
|
||||
"is_hr": "false",
|
||||
"output_format": "base64"
|
||||
}
|
||||
more_data.append(data4)
|
||||
# data2 = {
|
||||
# "hair_id": "1907651680352395265",
|
||||
# "task_id": "3145645324",
|
||||
# "user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_7.JPG",
|
||||
# "is_hr": "false",
|
||||
# "output_format": "base64"
|
||||
# }
|
||||
# more_data.append(data2)
|
||||
|
||||
data5 = {
|
||||
"hair_id": "1907651680352395265",
|
||||
"task_id": "2345867435",
|
||||
"user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_4.JPG",
|
||||
"is_hr": "false",
|
||||
"output_format": "base64"
|
||||
}
|
||||
more_data.append(data5)
|
||||
# data3 = {
|
||||
# "hair_id": "1907651680352395265",
|
||||
# "task_id": "456412345",
|
||||
# "user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_6.JPG",
|
||||
# "is_hr": "false",
|
||||
# "output_format": "base64"
|
||||
# }
|
||||
# more_data.append(data3)
|
||||
|
||||
# data4 = {
|
||||
# "hair_id": "1907651680352395265",
|
||||
# "task_id": "45623445",
|
||||
# "user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_5.JPG",
|
||||
# "is_hr": "false",
|
||||
# "output_format": "base64"
|
||||
# }
|
||||
# more_data.append(data4)
|
||||
|
||||
# data5 = {
|
||||
# "hair_id": "1907651680352395265",
|
||||
# "task_id": "2345867435",
|
||||
# "user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_4.JPG",
|
||||
# "is_hr": "false",
|
||||
# "output_format": "base64"
|
||||
# }
|
||||
# more_data.append(data5)
|
||||
|
||||
|
||||
data6 = {
|
||||
"hair_id": "1907651680352395265",
|
||||
"task_id": "86747535",
|
||||
"user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_3.JPG",
|
||||
"is_hr": "false",
|
||||
"output_format": "base64"
|
||||
}
|
||||
more_data.append(data6)
|
||||
# data6 = {
|
||||
# "hair_id": "1907651680352395265",
|
||||
# "task_id": "86747535",
|
||||
# "user_img_path": "https://cdn.meidaojia.com/ZoeFiles/testuser_3.JPG",
|
||||
# "is_hr": "false",
|
||||
# "output_format": "base64"
|
||||
# }
|
||||
# more_data.append(data6)
|
||||
|
||||
def image_to_base64(file_path, mime_type=None):
|
||||
"""
|
||||
@@ -108,7 +110,7 @@ def image_to_base64(file_path, mime_type=None):
|
||||
# 组合成Data URI格式
|
||||
return f"data:{mime_type};base64,{encoded_string}"
|
||||
|
||||
img64_str = image_to_base64("aaa.jpg")
|
||||
img64_str = image_to_base64("testuser_8.JPG")
|
||||
data['user_img_path'] = img64_str
|
||||
|
||||
# 存储所有请求的响应
|
||||
@@ -125,9 +127,9 @@ def send_request(start_time):
|
||||
sleep_time = start_time #random.uniform(0, 2) # 生成 [0, 2) 之间的随机浮点数
|
||||
time.sleep(sleep_time)
|
||||
global send_index
|
||||
send_data = more_data[send_index]
|
||||
send_data = data #more_data[send_index]
|
||||
# send_index = send_index + 1
|
||||
send_data['task_id'] = str(int(random.uniform(0, 10000000)))
|
||||
# send_data['task_id'] = str(int(random.uniform(0, 10000000)))
|
||||
response = requests.post(url, headers=headers, json=send_data)
|
||||
with lock: # 加锁,防止多线程同时修改 responses
|
||||
responses.append(response)
|
||||
@@ -137,7 +139,7 @@ def send_request(start_time):
|
||||
|
||||
# 创建并启动多个线程
|
||||
threads = []
|
||||
num_requests = 10 # 并发请求数量
|
||||
num_requests = 1 # 并发请求数量
|
||||
|
||||
time_start = time.time()
|
||||
|
||||
@@ -159,6 +161,33 @@ total_time = (end_time - time_start)
|
||||
qps = num_requests/total_time
|
||||
print(f"num_requests:{num_requests} total_time:{total_time} qps:{qps}")
|
||||
|
||||
|
||||
|
||||
import base64
|
||||
import os
|
||||
|
||||
def save_base64_image(base64_str, filename):
|
||||
"""
|
||||
将 Base64 编码的图片字符串保存为本地 JPG 文件。
|
||||
|
||||
:param base64_str: Base64 编码的图片字符串(可能包含 data:image/jpeg;base64, 前缀)
|
||||
:param filename: 保存的文件名(例如 'image.jpg')
|
||||
"""
|
||||
# 如果包含 data URL 前缀,去掉它
|
||||
if base64_str.startswith("data:image"):
|
||||
base64_str = base64_str.split(",", 1)[1]
|
||||
|
||||
try:
|
||||
image_data = base64.b64decode(base64_str)
|
||||
with open(filename, "wb") as f:
|
||||
f.write(image_data)
|
||||
print(f"图片已保存为 {filename}")
|
||||
except Exception as e:
|
||||
print(f"保存图片失败: {e}")
|
||||
|
||||
# 打印所有响应
|
||||
for i, resp in enumerate(responses, 1):
|
||||
print(f"请求 {i} 结果: {resp.status_code, resp.text[:128]}")
|
||||
print(f"请求 {i} 结果: {resp.status_code, resp.text[:128]}")
|
||||
json_obj = json.loads(resp.text)
|
||||
data_string = json_obj['data']
|
||||
save_base64_image(data_string, f"response_{i}.jpg")
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 190 KiB |
Reference in New Issue
Block a user