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 requests
import time import time
import random
import argparse import argparse
import multiprocessing import multiprocessing
import tqdm import tqdm
import random
import traceback import traceback
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor from requests_toolbelt import MultipartEncoderMonitor
import requests import requests
@ -32,19 +32,31 @@ try:
except: except:
print("Failed to login") print("Failed to login")
print(response.content) print(response.content)
# After the login, store the token in the memory (RAM) or DB # After the login, store the token in the memory (RAM) or DB
# Re-login to issue a new token after 6 days. # Re-login to issue a new token after 6 days.
# ================================================================= # =================================================================
def process_file(data): def process_file(data):
files, token = data _, token = data
files = ( files = []
# 'invoice_file': ('invoice.jpg', open("test_samples/sbt/invoice.jpg", "rb"), 'application/octet-stream'), if random.random() < 0.2:
('imei_files', ("imei1.jpg", open("test_samples/sbt/imei1.jpg", "rb"), 'application/octet-stream')), files = [
('imei_files', ("imei1.jpg", open("test_samples/sbt/imei1.jpg", "rb"), 'application/octet-stream')), ('invoice_file', ("invoice.jpg", open("test_samples/sbt/invoice.jpg", "rb"), 'application/octet-stream')),
('processType', '12'), ('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) - 1 ]
elif random.random() < 0.6:
files = [
('imei_files', ("imei1.jpg", open("test_samples/sbt/imei1.jpg", "rb"), 'application/octet-stream')),
]
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 # UPLOAD THE FILE
start_time = time.time() start_time = time.time()
@ -88,8 +100,15 @@ def process_file(data):
try: try:
data = response.json() 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) data.pop("files", None)
print(data)
except: except:
print(response.content) print(response.content)
return { 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): def gen_input(num_input):
for _ in range(num_input): for _ in range(num_input):
yield (get_files(), token) yield (None, token)
pool = multiprocessing.Pool(processes=args.num_workers) pool = multiprocessing.Pool(processes=args.num_workers)
results = [] results = []
for result in tqdm.tqdm(pool.imap_unordered(process_file, gen_input(num_input=args.num_requests)), total=args.num_requests): 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("## TEST REPORT #################################")
print("Number of requests: {}".format(args.num_requests)) print("Number of requests: {}".format(args.num_requests))
print("Number of concurrent requests: {}".format(args.num_workers)) print("Number of concurrent requests: {}".format(args.num_workers))
print("Number of files: 1 invoice, 1-5 imei files (random)")
print("--------------------------------------") print("--------------------------------------")
print("SUCCESS RATE") print("SUCCESS RATE")
counter = {} counter = {}