1111# Config
1212# -----------------------------
1313api_url = "http://52.13.56.115:8000/predict"
14- concurrent_users = 100
15- total_requests = 5000
1614
1715LOG_DIR = "logs"
1816os .makedirs (LOG_DIR , exist_ok = True )
6967# -----------------------------
7068# Async request
7169# -----------------------------
72- async def send_request (session , text ):
70+ async def send_request (session , text , quantize ):
7371 start = time .perf_counter ()
7472 timestamp = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S.%f" )[:- 3 ]
73+ payload = {"text" : text ,'quantize' :quantize }
7574
7675 try :
77- async with session .get (api_url , params = { "text" : text } ) as response :
76+ async with session .post (api_url , json = payload ) as response :
7877 result = await response .json ()
7978 latency = (time .perf_counter () - start ) * 1000 # ms
8079
8180 # log only to file
8281 logger .info (
83- f"[{ timestamp } ] Text: { text [:30 ]:<30} | Result: { result .get ('sentiment' , 'N/A' ):<8} | Latency: { latency :.2f} ms"
82+ f"[{ timestamp } ] Text: { text [:30 ]:<30} | Result: { result .get ('sentiment' , 'N/A' ):<8} | Q: { result . get ( 'quantized' ) } | Latency: { latency :.2f} ms"
8483 )
8584 except Exception as e :
8685 logger .error (f"Error: { e } " )
@@ -89,13 +88,13 @@ async def send_request(session, text):
8988# -----------------------------
9089# Main function with progress bar
9190# -----------------------------
92- async def main ():
91+ async def main (concurent_users , total_requests , quantize ):
9392 async with aiohttp .ClientSession () as session :
9493 tasks = []
9594 with tqdm (total = total_requests , desc = "Simulating load" , ncols = 80 ) as pbar :
9695 for i in range (total_requests ):
9796 text = random .choice (sample_texts )
98- task = asyncio .create_task (send_request (session , text ))
97+ task = asyncio .create_task (send_request (session , text , quantize ))
9998 tasks .append (task )
10099
101100 if len (tasks ) >= concurrent_users :
@@ -109,4 +108,15 @@ async def main():
109108
110109
111110if __name__ == "__main__" :
112- asyncio .run (main ())
111+ import argparse
112+ parser = argparse .ArgumentParser (description = "Load Simulator for Sentiment API" )
113+ parser .add_argument ('--concurrent' , type = int , default = 1 , help = 'Number of concurrent users' )
114+ parser .add_argument ('--requests' , type = int , default = 1 , help = 'Total number of requests to send' )
115+ parser .add_argument ('--quantize' ,type = str ,default = "true" , help = "Whether to use quantized model or fp32 model" )
116+ args = parser .parse_args ()
117+ concurrent_users = args .concurrent
118+ total_requests = args .requests
119+ quantize = args .quantize == "true"
120+
121+ asyncio .run (main (concurrent_users , total_requests ,quantize ))
122+
0 commit comments