backend endpoints test script added
This commit is contained in:
@@ -240,3 +240,10 @@ Within a single point you have to do the scripts from top to bottom.
|
||||
* Path: inse-internal-external-scripts\extern\generic-project-crawler\project-crawler-v4-all\BMBF_bundprojects
|
||||
* Crawl & Upload: gpc.data.py
|
||||
|
||||
### INSE test:
|
||||
|
||||
#### INSE endpoints test:
|
||||
* Path: inse-internal-external-scripts\inse_test
|
||||
* Create stats file: test_endpoints.py
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,172 @@
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import time
|
||||
import sys
|
||||
import json
|
||||
from urllib.parse import urlencode
|
||||
|
||||
# Configure your endpoints here:
|
||||
#JS_API = "https://inse.nes.uni-due.de/backend/api/v1"
|
||||
JS_API = "http://localhost:3000/api/v1"
|
||||
#FLASK_API = "http://localhost:5000/api/v1"
|
||||
FLASK_API = "https://inse.uni-due.de/api/v1/flask"
|
||||
with open('all_endpoints.json', 'r') as f:
|
||||
ALL_ENDPOINTS = json.load(f)
|
||||
|
||||
HEADERS = {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
ENDPOINTS_MAPPING={'person': 1,
|
||||
'keyword': 1,
|
||||
'group': 1,
|
||||
'nlp': 1,
|
||||
'startups': 1,
|
||||
'news': 1,
|
||||
'projects': 1,
|
||||
'hierarchy': 1,
|
||||
'forecasting': 1,
|
||||
'exploration': 1,
|
||||
'stats': 1
|
||||
}
|
||||
async def make_request(session, conf, results):
|
||||
original_url = conf["url"]
|
||||
method = conf.get("method", "GET").upper()
|
||||
category = conf.get("category", "")
|
||||
sub_category = conf.get("sub_category", "")
|
||||
sub_sub_category = conf.get("sub_sub_category", "")
|
||||
params = conf.get("params", {})
|
||||
json_data = conf.get("json")
|
||||
|
||||
# Build the actual request URL
|
||||
request_url = original_url
|
||||
if params:
|
||||
request_url = f"{request_url}?{urlencode(params, doseq=True)}"
|
||||
|
||||
start = time.perf_counter()
|
||||
|
||||
try:
|
||||
async with session.request(method, request_url, headers=HEADERS, json=json_data) as resp:
|
||||
duration = (time.perf_counter() - start) * 1000
|
||||
# Store all identifying information
|
||||
results.append({
|
||||
"url": original_url,
|
||||
"path": original_url[len(JS_API):],
|
||||
"method": method,
|
||||
"params": params,
|
||||
"status": resp.status,
|
||||
"duration": duration,
|
||||
"category": category,
|
||||
"sub_category": sub_category,
|
||||
"sub_sub_category": sub_sub_category,
|
||||
})
|
||||
except Exception as e:
|
||||
duration = (time.perf_counter() - start) * 1000
|
||||
results.append({
|
||||
"url": original_url,
|
||||
"method": method,
|
||||
"params": params,
|
||||
"status": "ERROR",
|
||||
"duration": duration,
|
||||
"category": category,
|
||||
"sub_category": sub_category,
|
||||
"sub_sub_category": sub_sub_category,
|
||||
"error": str(e)
|
||||
})
|
||||
|
||||
async def run_stress_test():
|
||||
results = []
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
total_requests = sum(conf.get("repeat", 1) for conf in ENDPOINTS)
|
||||
print(f"Running {total_requests} total requests sequentially...")
|
||||
|
||||
for conf in ENDPOINTS:
|
||||
for _ in range(conf.get("repeat", 1)):
|
||||
await make_request(session, conf, results)
|
||||
|
||||
# ===== COUNT SUCCESS / FAILURE =====
|
||||
success_calls = 0
|
||||
failed_calls = 0
|
||||
|
||||
for r in results:
|
||||
if r["status"] == "ERROR":
|
||||
failed_calls += 1
|
||||
elif isinstance(r["status"], int) and 200 <= r["status"] < 400:
|
||||
success_calls += 1
|
||||
else:
|
||||
failed_calls += 1
|
||||
|
||||
print("\n===== CALL SUMMARY =====")
|
||||
print(f"Total calls : {len(results)}")
|
||||
print(f"Success calls : {success_calls}")
|
||||
print(f"Failed calls : {failed_calls}")
|
||||
|
||||
# ===== AGGREGATION LOGIC =====
|
||||
endpoint_map = {}
|
||||
|
||||
for result in results:
|
||||
key = (
|
||||
result["url"],
|
||||
result["method"],
|
||||
result["category"],
|
||||
result["sub_category"],
|
||||
result["sub_sub_category"],
|
||||
)
|
||||
|
||||
endpoint_map.setdefault(key, []).append(result)
|
||||
|
||||
unique_endpoints = []
|
||||
|
||||
for (url, method, category, sub_category, sub_sub_category), calls in endpoint_map.items():
|
||||
response_times = [c["duration"] for c in calls if isinstance(c["status"], int)]
|
||||
|
||||
success_count = sum(
|
||||
1 for c in calls
|
||||
if isinstance(c["status"], int) and 200 <= c["status"] < 400
|
||||
)
|
||||
|
||||
failure_count = len(calls) - success_count
|
||||
|
||||
unique_endpoints.append({
|
||||
"url": url,
|
||||
"path": url[len(JS_API):],
|
||||
"method": method,
|
||||
"response_times": [round(t, 2) for t in response_times],
|
||||
"avg_response_time": round(sum(response_times) / len(response_times), 2)
|
||||
if response_times else None,
|
||||
"count": len(calls),
|
||||
"success_count": success_count,
|
||||
"failure_count": failure_count,
|
||||
"success_rate": round(success_count / len(calls), 3),
|
||||
"category": category,
|
||||
"sub_category": sub_category,
|
||||
"sub_sub_category": sub_sub_category
|
||||
})
|
||||
|
||||
# for endpoint in unique_endpoints:
|
||||
# print(f"\n{endpoint['method']} {endpoint['url']}")
|
||||
# print(f" Requests: {endpoint['count']}")
|
||||
# print(f" Avg response: {endpoint['avg_response_time']}ms")
|
||||
# print(f" Response times: {endpoint['response_times']}")
|
||||
|
||||
return unique_endpoints
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
all_results = []
|
||||
if len(sys.argv) != 2:
|
||||
sys.exit("No path given. Give path to all_endpoints.json")
|
||||
print("Starting stress tests...")
|
||||
for key, endpoints in ALL_ENDPOINTS.items():
|
||||
if (ENDPOINTS_MAPPING.get(key, 0) == 1):
|
||||
print(f"Running stress test for category: {key}")
|
||||
|
||||
ENDPOINTS = endpoints # set endpoints for this run
|
||||
result_batch = asyncio.run(run_stress_test())
|
||||
|
||||
all_results.extend(result_batch)
|
||||
|
||||
with open("endpoints_tested.json", "w") as json_file:
|
||||
json.dump(all_results, json_file, indent=4)
|
||||
Reference in New Issue
Block a user