Update speedtest script

This commit is contained in:
Viet Anh Nguyen 2023-12-25 10:07:58 +07:00
parent 5c9a51ccde
commit ac4eb8dde1

View File

@ -1,11 +1,11 @@
import requests
import time
import random
import argparse
import multiprocessing
import tqdm
import random
import traceback
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
from requests_toolbelt import MultipartEncoderMonitor
import requests
@ -32,19 +32,31 @@ try:
except:
print("Failed to login")
print(response.content)
# After the login, store the token in the memory (RAM) or DB
# Re-login to issue a new token after 6 days.
# =================================================================
def process_file(data):
files, token = data
files = (
# 'invoice_file': ('invoice.jpg', open("test_samples/sbt/invoice.jpg", "rb"), 'application/octet-stream'),
_, token = data
files = []
if random.random() < 0.2:
files = [
('invoice_file', ("invoice.jpg", open("test_samples/sbt/invoice.jpg", "rb"), 'application/octet-stream')),
('imei_files', ("imei1.jpg", open("test_samples/sbt/imei1.jpg", "rb"), 'application/octet-stream')),
('imei_files', ("imei1.jpg", open("test_samples/sbt/imei2.jpg", "rb"), 'application/octet-stream')),
]
elif random.random() < 0.6:
files = [
('imei_files', ("imei1.jpg", open("test_samples/sbt/imei1.jpg", "rb"), 'application/octet-stream')),
('processType', '12'),
)
num_files = len(files) - 1
]
else:
files = [
('imei_files', ("imei1.jpg", open("test_samples/sbt/imei1.jpg", "rb"), 'application/octet-stream')),
('imei_files', ("imei1.jpg", open("test_samples/sbt/imei2.jpg", "rb"), 'application/octet-stream')),
]
num_files = len(files)
files.append(('processType', '12'))
# =================================================================
# UPLOAD THE FILE
start_time = time.time()
@ -88,8 +100,15 @@ def process_file(data):
try:
data = response.json()
if len(data["files"]) != num_files:
return {
"success": False,
"status": "missing_file",
"upload_time": 0,
"process_time": 0,
"num_files": 0,
}
data.pop("files", None)
print(data)
except:
print(response.content)
return {
@ -108,37 +127,9 @@ def process_file(data):
}
# invoice_files = [
# ('invoice_file', ('invoice.pdf', open("test_samples/20220303025923NHNE_20220222_Starhub_Order_Confirmation_by_Email.pdf", "rb").read())),
# ]
invoice_files = [
('invoice_file', ('invoice.jpg', open("test_samples/sbt/invoice.jpg", "rb").read())),
]
imei_files = [
('imei_files', ("test_samples/sbt/imei1.jpg", open("test_samples/sbt/imei1.jpg", "rb").read(), 'application/octet-stream')),
('imei_files', ("test_samples/sbt/imei2.jpg", open("test_samples/sbt/imei1.jpg", "rb").read(), 'application/octet-stream')),
# ('imei_files', ("test_samples/sbt/imei3.jpg", open("test_samples/sbt/imei3.jpg", "rb").read())),
# ('imei_files', ("test_samples/sbt/imei4.jpeg", open("test_samples/sbt/imei4.jpeg", "rb").read())),
# ('imei_files', ("test_samples/sbt/imei5.jpg", open("test_samples/sbt/imei5.jpg", "rb").read())),
]
def get_imei_files():
num_files = random.randint(1, len(imei_files) + 1)
files = imei_files[:num_files]
# print("Num of imei files:", len(files))
return files
# def get_files():
# return imei_files
# return invoice_files + get_imei_files()
def get_files():
return {
'invoice_file': ('invoice.jpg', open("test_samples/sbt/invoice.jpg", "rb").read()),
'imei_files': ("test_samples/sbt/imei1.jpg", open("test_samples/sbt/imei1.jpg", "rb").read(), 'application/octet-stream'),
'processType': 12,
}
def gen_input(num_input):
for _ in range(num_input):
yield (get_files(), token)
yield (None, token)
pool = multiprocessing.Pool(processes=args.num_workers)
results = []
for result in tqdm.tqdm(pool.imap_unordered(process_file, gen_input(num_input=args.num_requests)), total=args.num_requests):
@ -147,7 +138,6 @@ for result in tqdm.tqdm(pool.imap_unordered(process_file, gen_input(num_input=ar
print("## TEST REPORT #################################")
print("Number of requests: {}".format(args.num_requests))
print("Number of concurrent requests: {}".format(args.num_workers))
print("Number of files: 1 invoice, 1-5 imei files (random)")
print("--------------------------------------")
print("SUCCESS RATE")
counter = {}