80 lines
3.4 KiB
Python
Executable File
80 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Lasttest gegen eine TESTINSTANZ: Antwortzeiten üblicher Seiten (nur Lesen).
|
|
|
|
Ziel aus dem Pflichtenheft: unter 200 ms für übliche Seiten. Das Skript ruft
|
|
Seiten gleichzeitig ab und meldet je Seite Median, 95. Perzentil und Maximum.
|
|
Es braucht nur Python 3, keine Pakete, und sendet ausschließlich GET-Anfragen.
|
|
|
|
python3 scripts/loadtest.py https://test.umfrage.example.de \\
|
|
--path / --path /login --path /f/<token> --users 20 --requests 50
|
|
|
|
Mit --cookie "surveybox_session=…" (bzw. __Host-surveybox_session=…) lassen
|
|
sich auch Seiten hinter der Anmeldung messen, z. B. /forms. Den Wert aus den
|
|
Entwicklerwerkzeugen des Browsers kopieren und danach abmelden.
|
|
|
|
Hinweis: Die Anmeldung begrenzt Fehlversuche je IP, Seiten zum Lesen nicht.
|
|
Bitte nur gegen eigene Testinstanzen laufen lassen.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import statistics
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
|
def fetch(url: str, cookie: str | None) -> tuple[float, int]:
|
|
request = urllib.request.Request(url, headers={ # noqa: S310 (Adresse vom Aufrufer)
|
|
"User-Agent": "surveybox-loadtest", "Accept-Language": "de",
|
|
**({"Cookie": cookie} if cookie else {})})
|
|
start = time.perf_counter()
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=30) as resp: # noqa: S310
|
|
resp.read()
|
|
status = resp.status
|
|
except urllib.error.HTTPError as exc:
|
|
status = exc.code
|
|
return (time.perf_counter() - start) * 1000, status
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__,
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
parser.add_argument("base", help="Adresse der Testinstanz, z. B. https://test.example.de")
|
|
parser.add_argument("--path", action="append", default=[],
|
|
help="Seite (mehrfach möglich); Standard: / und /login")
|
|
parser.add_argument("--users", type=int, default=10, help="gleichzeitige Abrufe")
|
|
parser.add_argument("--requests", type=int, default=50, help="Abrufe je Seite")
|
|
parser.add_argument("--limit-ms", type=float, default=200.0,
|
|
help="Grenze für das 95. Perzentil (Standard 200)")
|
|
parser.add_argument("--cookie", default=None, help="Cookie-Kopf für angemeldete Seiten")
|
|
args = parser.parse_args()
|
|
paths = args.path or ["/", "/login"]
|
|
base = args.base.rstrip("/")
|
|
failed = False
|
|
print(f"{'Seite':40} {'Median':>8} {'95 %':>8} {'Max':>8} Fehler")
|
|
for path in paths:
|
|
url = base + path
|
|
fetch(url, args.cookie) # aufwärmen
|
|
with ThreadPoolExecutor(max_workers=args.users) as pool:
|
|
results = list(pool.map(fetch, [url] * args.requests, [args.cookie] * args.requests))
|
|
times = sorted(t for t, _ in results)
|
|
errors = sum(1 for _, status in results if status >= 400)
|
|
p95 = times[max(int(len(times) * 0.95) - 1, 0)]
|
|
too_slow = p95 > args.limit_ms
|
|
failed = failed or too_slow or errors > 0
|
|
mark = " ZU LANGSAM" if too_slow else ""
|
|
print(f"{path[:40]:40} {statistics.median(times):7.0f} {p95:7.0f} {times[-1]:7.0f}"
|
|
f" {errors}{mark}")
|
|
print(f"\nGrenze 95. Perzentil: {args.limit_ms:.0f} ms bei {args.users} gleichzeitigen Abrufen")
|
|
return 1 if failed else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|