tox.ini: add runuwsgi and bench targets

This commit is contained in:
Benjamin Dauvergne 2023-04-13 21:39:22 +02:00
parent 0cc9bcdd4f
commit 3195d4c3a3
6 changed files with 202 additions and 0 deletions

23
fixtures/config.json Normal file
View File

@ -0,0 +1,23 @@
[
{
"fields" : {
"value" : "5c55f859e66e2f6dee41a55c6ebd8b04f9dd88f4b22dd451b2663797edff7fc3"
},
"model" : "data.config",
"pk" : "REQUEST_FROM_ANTS_AUTH_TOKEN"
},
{
"fields" : {
"value" : "GRmDWrsZFwYGPTHUzUS5"
},
"model" : "data.config",
"pk" : "REQUEST_TO_ANTS_AUTH_TOKEN"
},
{
"fields" : {
"value" : "https://ppd.api.rendezvouspasseport.ants.gouv.fr/api/"
},
"model" : "data.config",
"pk" : "REQUEST_TO_ANTS_BASE_URL"
}
]

51
scripts/bench.py Normal file
View File

@ -0,0 +1,51 @@
import asyncio
import sys
import time
import aiohttp
requests = 0
url = (
sys.argv[1]
if len(sys.argv) > 1
else (
"http://127.0.0.1:9040/api/ants/availableTimeSlots?meeting_point_ids=1&meeting_point_ids=2&meeting_point_ids=3&start_date=2023-04-13&end_date=2023-04-17"
)
)
async def make_request(i):
global requests
async with aiohttp.ClientSession() as client:
for i in range(200):
try:
response = await client.get(
url,
headers={
"X-HUB-RDV-AUTH-TOKEN": "5c55f859e66e2f6dee41a55c6ebd8b04f9dd88f4b22dd451b2663797edff7fc3"
},
)
except Exception:
pass
else:
requests += 1
content = await response.json()
assert isinstance(content, dict), content
await client.close()
async def main():
global requests
async with asyncio.TaskGroup() as tg:
for i in range(30):
tg.create_task(make_request(i))
start = time.time()
asyncio.run(main())
duration = time.time() - start
print("Did", requests, "requests in", duration, "seconds RPS:", requests / duration)

13
scripts/bench.sh Normal file
View File

@ -0,0 +1,13 @@
set -e
bash ./scripts/uwsgi.sh &
PID=$!
sleep 2
echo $PID
sleep 3
python3 ./scripts/bench.py
kill -KILL $PID

View File

@ -0,0 +1,38 @@
SECRET_KEY = '1234'
LOGGING = {
'version': 1,
'formatters': {
'stream': {
'format': '%(asctime)s %(levelname)s %(name)s: %(message)s',
'datefmt': '%Y-%m-%d %a %H:%M:%S',
},
},
'handlers': {
'stream': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'stream',
# fd3 is redirected to stdout in scripts/uwsgi.sh
# to prevent logs to go to uwsgi log file.
'filename': '/dev/fd/3',
},
},
'loggers': {
# even when debugging seeing SQL queries is too much, activate it
# explicitly using DEBUG_DB
'django.db': {
'level': 'INFO',
'handlers': [],
'propagate': True,
},
'django': {
'level': 'INFO',
'handlers': [],
'propagate': True,
},
'': {
'handlers': ['stream'],
'level': 'ERROR',
},
},
}

54
scripts/uwsgi.sh Executable file
View File

@ -0,0 +1,54 @@
set -e
function silent() {
LOG=`mktemp`
"$@" >$LOG 2>&1 || (tail $LOG; rm $LOG; exit 1)
rm $LOG
}
mkdir -p $TOX_WORK_DIR/spooler
silent ./manage.py migrate 3>&1
silent ./manage.py loaddata fixtures/admin.json 3>&1
silent ./manage.py loaddata fixtures/example1.json 3>&1
silent ./manage.py loaddata fixtures/config.json 3>&1
UWSGI_INI=$TOX_WORK_DIR/uwsgi.ini
cat >$UWSGI_INI <<EOF
[uwsgi]
plugin = python3
single-interpreter = true
module = ants_hub.wsgi:application
need-app = true
plugin = logfile
logger = file:$TOX_WORK_DIR/uwsgi.log
req-logger = file:/dev/fd/3
logformat-strftime = true
log-date = %%Y-%%m-%%d %%a %%H:%%M:%%S
log-format = %(ftime) INFO %(method) %(uri) (%(rsize) bytes, %(msecs) msecs, status %(status))
virtualenv = $TOX_ENV_DIR
auto-procname = true
procname-prefix-spaced = ants-hub
master = true
enable-threads = true
listen = 1000
processes = 5
pidfile = $TOX_WORK_DIR/uwsgi.pid
http-socket = 127.0.0.1:9040
# spooler-python-import = ants_hub.spooler
# spooler = $TOX_WORK_DIR/spooler
python-autoreload = 1
EOF
echo
echo - uwsgi.ini: $UWSGI_INI
echo - pidfile: $TOX_WORK_DIR/uwsgi.pid
echo - uwsgi.log: $TOX_WORK_DIR/uwsgi.log
echo - spooler: $TOX_WORK_DIR/spooler
echo
echo
echo " Open http://127.0.0.1:9040/ with login/password: admin / admin"
exec uwsgi $UWSGI_INI 3>&1

23
tox.ini
View File

@ -92,6 +92,29 @@ commands =
./manage.py loaddata fixtures/example1.json
./manage.py runserver {posargs:9000}
[testenv:runuwsgi]
usedevelop = True
setenv =
DJANGO_SETTINGS_MODULE=ants_hub.settings
ANTS_HUB_SETTINGS_FILE=scripts/runuwsgi_settings.py
deps =
allowlist_externals=
bash
commands =
bash scripts/uwsgi.sh
[testenv:bench]
usedevelop = True
setenv =
DJANGO_SETTINGS_MODULE=ants_hub.settings
ANTS_HUB_SETTINGS_FILE=scripts/runuwsgi_settings.py
deps =
aiohttp
allowlist_externals=
bash
commands =
bash scripts/bench.sh
[pytest]
filterwarnings = error