2023-12-13 09:01:31 +00:00
import requests
import time
2023-12-25 03:32:09 +00:00
import random
2023-12-13 09:01:31 +00:00
import argparse
import multiprocessing
import tqdm
import traceback
2023-12-25 03:32:09 +00:00
from requests_toolbelt import MultipartEncoderMonitor
import requests
2023-12-13 09:01:31 +00:00
parser = argparse . ArgumentParser ( )
parser . add_argument ( " --host " , dest = " host " , default = " https://sbt.idp.sdsrv.ai " , required = False )
parser . add_argument ( " -u " , " --username " , help = " Username to connect to server " , required = True )
parser . add_argument ( " -p " , " --password " , help = " Password to connect to server " , required = True )
parser . add_argument ( " --num_requests " , type = int , help = " Number of requests " , required = False , default = 100 )
parser . add_argument ( " --num_workers " , type = int , help = " Number of workers " , required = False , default = 3 )
parser . add_argument ( " --checking_interval " , type = float , help = " Interval result checking time " , required = False , default = 0.5 )
args = parser . parse_args ( )
PROCESSING_TIMEOUT = 60
# =================================================================
# GET THE TOKEN
response = requests . post ( f ' { args . host } /api/ctel/login/ ' , json = {
' username ' : args . username ,
' password ' : args . password
} )
try :
token = response . json ( ) [ ' token ' ]
except :
print ( " Failed to login " )
print ( response . content )
2023-12-25 03:32:09 +00:00
2023-12-13 09:01:31 +00:00
# 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 ) :
2023-12-25 03:32:09 +00:00
_ , 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 ' ) ) ,
]
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 ' ) ) ,
]
2023-12-13 09:01:31 +00:00
num_files = len ( files )
2023-12-25 03:32:09 +00:00
files . append ( ( ' processType ' , ' 12 ' ) )
2023-12-13 09:01:31 +00:00
# =================================================================
# UPLOAD THE FILE
start_time = time . time ( )
2023-12-25 03:32:09 +00:00
end_of_upload_time = 0
def my_callback ( monitor ) :
nonlocal end_of_upload_time
if monitor . bytes_read == monitor . len :
end_of_upload_time = time . time ( )
m = MultipartEncoderMonitor . from_fields (
fields = files ,
callback = my_callback
)
2023-12-13 09:01:31 +00:00
try :
response = requests . post ( f ' { args . host } /api/ctel/images/process_sync/ ' , headers = {
' Authorization ' : token ,
2023-12-25 03:32:09 +00:00
' Content-Type ' : m . content_type
} , data = m , timeout = 300 )
2023-12-13 09:01:31 +00:00
except requests . exceptions . Timeout :
print ( " Timeout occurred while uploading " )
return {
" success " : False ,
" status " : " timeout " ,
" upload_time " : 0 ,
" process_time " : 0 ,
" num_files " : 0 ,
}
except Exception as e :
print ( e )
traceback . print_exc ( )
print ( " Unknown exception occurred while uploading " )
return {
" success " : False ,
" status " : " unknown error " ,
" upload_time " : 0 ,
" process_time " : 0 ,
" num_files " : 0 ,
}
end_time = time . time ( )
2023-12-25 03:32:09 +00:00
upload_time = end_of_upload_time - start_time
2023-12-13 09:01:31 +00:00
# =================================================================
try :
data = response . json ( )
2023-12-25 03:32:09 +00:00
if len ( data [ " files " ] ) != num_files :
return {
" success " : False ,
" status " : " missing_file " ,
" upload_time " : 0 ,
" process_time " : 0 ,
" num_files " : 0 ,
}
2023-12-13 09:01:31 +00:00
data . pop ( " files " , None )
except :
print ( response . content )
return {
" success " : False ,
" status " : " timeout " ,
" upload_time " : 0 ,
" process_time " : 0 ,
" num_files " : 0 ,
}
return {
" success " : True ,
" status " : 200 ,
" upload_time " : upload_time ,
2023-12-25 03:32:09 +00:00
" process_time " : time . time ( ) - start_time - upload_time ,
2023-12-13 09:01:31 +00:00
" num_files " : num_files ,
}
def gen_input ( num_input ) :
for _ in range ( num_input ) :
2023-12-25 03:32:09 +00:00
yield ( None , token )
2023-12-13 09:01:31 +00:00
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 ) :
results . append ( result )
print ( " ## TEST REPORT ################################# " )
print ( " Number of requests: {} " . format ( args . num_requests ) )
print ( " Number of concurrent requests: {} " . format ( args . num_workers ) )
print ( " -------------------------------------- " )
print ( " SUCCESS RATE " )
counter = { }
for result in results :
counter [ result [ " status " ] ] = counter . get ( result [ " status " ] , 0 ) + 1
total_requests = sum ( counter . values ( ) )
print ( " Success rate: {} " . format ( counter . get ( 200 , 0 ) / total_requests if total_requests > 0 else - 1 ) )
print ( " Statuses: " , counter )
print ( " -------------------------------------- " )
print ( " TIME BY REQUEST " )
uploading_time = [ x [ " upload_time " ] for x in results if x [ " success " ] ]
if len ( uploading_time ) == 0 :
print ( " No valid uploading time " )
print ( " Check the results! " )
processing_time = [ x [ " process_time " ] for x in results if x [ " success " ] ]
2023-12-25 03:32:09 +00:00
print ( " Uploading time (Avg / Min / Max): {:.3f} s {:.3f} s {:.3f} s " . format ( sum ( uploading_time ) / len ( uploading_time ) , min ( uploading_time ) , max ( uploading_time ) ) )
print ( " Processing time (Avg / Min / Max): {:.3f} s {:.3f} s {:.3f} s " . format ( sum ( processing_time ) / len ( processing_time ) , min ( processing_time ) , max ( processing_time ) ) )
2023-12-13 09:01:31 +00:00
print ( " -------------------------------------- " )
print ( " TIME BY IMAGE " )
uploading_time = [ x [ " upload_time " ] for x in results if x [ " success " ] ]
processing_time = [ x [ " process_time " ] for x in results if x [ " success " ] ]
num_images = sum ( x [ " num_files " ] for x in results if x [ " success " ] )
print ( " Total images: " , num_images )
2023-12-15 05:43:19 +00:00
print ( " Uploading + Processing time: {:.3f} s " . format ( sum ( processing_time ) / num_images ) )
2023-12-13 09:01:31 +00:00
print ( " -------------------------------------- " )
2023-12-25 03:32:09 +00:00