|
import os |
|
import time |
|
import random |
|
import requests |
|
import logging |
|
from concurrent.futures import ThreadPoolExecutor, as_completed |
|
from tqdm import tqdm |
|
|
|
|
|
|
|
|
|
URL = "http://localhost:7860/classify" |
|
NUM_REQUESTS = 4000 |
|
MAX_WORKERS = os.cpu_count() |
|
TIMEOUT = 20 |
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
|
|
|
SAMPLE_POPULATION = """Limes have higher contents of sugars and acids than lemons do.[1] Lime juice may be squeezed from fresh limes, or purchased in bottles in both unsweetened and sweetened varieties. Lime juice is used to make limeade, and as an ingredient (typically as sour mix) in many cocktails. |
|
|
|
Lime pickles are an integral part of Indian cuisine, especially in South India. In Kerala, the Onam Sadhya usually includes either lemon pickle or lime pickle. Other Indian preparations of limes include sweetened lime pickle, salted pickle, and lime chutney. |
|
|
|
In cooking, lime is valued both for the acidity of its juice and the floral aroma of its zest. It is a common ingredient in authentic Mexican, Vietnamese and Thai dishes. Lime soup is a traditional dish from the Mexican state of Yucatan. It is also used for its pickling properties in ceviche. Some guacamole recipes call for lime juice. |
|
|
|
The use of dried limes (called black lime or limoo) as a flavouring is typical of Persian cuisine, Iraqi cuisine, as well as in Eastern Arabian cuisine baharat (a spice mixture that is also called kabsa or kebsa). |
|
|
|
Key lime gives the character flavouring to the American dessert known as Key lime pie. In Australia, desert lime is used for making marmalade. |
|
|
|
Lime is an ingredient in several highball cocktails, often based on gin, such as gin and tonic, the gimlet and the Rickey. Freshly squeezed lime juice is also considered a key ingredient in margaritas, although sometimes lemon juice is substituted. It is also found in many rum cocktails such as the daiquiri, and other tropical drinks. |
|
|
|
Lime extracts and lime essential oils are frequently used in perfumes, cleaning products, and aromatherapy.""".split() |
|
|
|
|
|
|
|
|
|
|
|
def build_payload() -> dict: |
|
sentence = " ".join( |
|
random.choices(SAMPLE_POPULATION, k=random.randint(20, len(SAMPLE_POPULATION))) |
|
) |
|
return {"sentence": sentence} |
|
|
|
|
|
|
|
|
|
|
|
def send_request() -> int | str: |
|
try: |
|
response = requests.post( |
|
URL, |
|
json=build_payload(), |
|
headers={"Content-Type": "application/json"}, |
|
timeout=TIMEOUT, |
|
) |
|
return response.status_code |
|
except requests.RequestException as e: |
|
return f"Error: {e}" |
|
|
|
|
|
|
|
|
|
|
|
def test_endpoint(): |
|
print(f"Sending {NUM_REQUESTS} requests to {URL} with {MAX_WORKERS} workers") |
|
start_time = time.time() |
|
|
|
successful = 0 |
|
failed = 0 |
|
status_distribution = {} |
|
|
|
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: |
|
futures = [executor.submit(send_request) for _ in range(NUM_REQUESTS)] |
|
for future in tqdm( |
|
as_completed(futures), total=NUM_REQUESTS, desc="Processing" |
|
): |
|
result = future.result() |
|
if isinstance(result, int): |
|
status_distribution[result] = status_distribution.get(result, 0) + 1 |
|
if 200 <= result < 300: |
|
successful += 1 |
|
else: |
|
failed += 1 |
|
else: |
|
failed += 1 |
|
logging.warning(result) |
|
|
|
duration = time.time() - start_time |
|
print("\n--- Test Summary ---") |
|
print(f"Elapsed Time : {duration:.2f} seconds") |
|
print(f"Total Requests Sent : {NUM_REQUESTS}") |
|
print(f"Successful Requests : {successful}") |
|
print(f"Failed Requests : {failed}") |
|
print(f"Status Code Summary : {status_distribution}") |
|
|
|
|
|
if __name__ == "__main__": |
|
test_endpoint() |
|
|