-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_client.py
30 lines (24 loc) · 1016 Bytes
/
benchmark_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
iimport time
import random
mport requests
from concurrent.futures import ThreadPoolExecutor
from sklearn.datasets import load_iris
CONCURRENCY = 20 # Number of threads (concurrent requests)
TOTAL_REQUESTS = 1000 # Total number of requests to send
client = bentoml.SyncHTTPClient("http://localhost:3000")
iris = load_iris()
data_samples = iris.data.tolist()
payloads = [random.choice(data_samples) for _ in range(TOTAL_REQUESTS)]
def send_request(index, data):
"""Send a single HTTP request and print the result."""
try:
start_time = time.time()
response = client.predict(np.array([data]))
duration = time.time() - start_time
except Exception as e:
print(f"Request {index}: Error -> {e}")
print(f"Sending {TOTAL_REQUESTS} requests to {client.url} with concurrency {CONCURRENCY}...")
with ThreadPoolExecutor(max_workers=CONCURRENCY) as executor:
for i, data in enumerate(payloads, start=1):
executor.submit(send_request, i, data)
print("Done.")