Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions frameworks/Python/aiohttp/aiohttp-nginx.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM python:3.13

RUN apt-get update && apt-get install -y \
nginx \
netcat-openbsd

ADD ./requirements.txt /aiohttp/requirements.txt

RUN pip3 install cython==3.0.11 && \
pip3 install -r /aiohttp/requirements.txt

ADD ./ /aiohttp

WORKDIR /aiohttp

ENV CONNECTION=RAW

EXPOSE 8080

RUN chmod +x /aiohttp/nginx-entrypoint.sh

ENTRYPOINT ["/aiohttp/nginx-entrypoint.sh"]
2 changes: 0 additions & 2 deletions frameworks/Python/aiohttp/aiohttp-orm.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ WORKDIR aiohttp
RUN pip3 install cython==3.0.11 && \
pip3 install -r /aiohttp/requirements.txt

WORKDIR /aiohttp

EXPOSE 8080

CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py
14 changes: 14 additions & 0 deletions frameworks/Python/aiohttp/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import uvloop
from aiohttp import web
import argparse
from .main import create_app


if __name__ == '__main__':
uvloop.install()
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, required=True)
args = parser.parse_args()

app = create_app()
web.run_app(app, port=args.port)
23 changes: 23 additions & 0 deletions frameworks/Python/aiohttp/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@
"display_name": "aiohttp",
"notes": "uses asyncpg for database access"
},
"nginx": {
"json_url": "/json",
"db_url": "/db",
"query_url": "/queries/",
"fortune_url": "/fortunes",
"update_url": "/updates/",
"plaintext_url": "/plaintext",
"port": 8080,
"approach": "Realistic",
"classification": "Micro",
"database": "Postgres",
"framework": "aiohttp",
"language": "Python",
"flavor": "Python3",
"orm": "Raw",
"platform": "asyncio",
"webserver": "nginx",
"os": "Linux",
"database_os": "Linux",
"display_name": "aiohttp-nginx",
"notes": "uses nginx as proxy server",
"versus": "default"
},
"orm": {
"db_url": "/db",
"query_url": "/queries/",
Expand Down
17 changes: 17 additions & 0 deletions frameworks/Python/aiohttp/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ platform = "asyncio"
webserver = "gunicorn"
versus = "None"

[nginx]
urls.plaintext = "/plaintext"
urls.json = "/json"
urls.db = "/db"
urls.query = "/queries/"
urls.update = "/updates/"
urls.fortune = "/fortunes"
approach = "Realistic"
classification = "Micro"
database = "Postgres"
database_os = "Linux"
os = "Linux"
orm = "Raw"
platform = "asyncio"
webserver = "nginx"
versus = "None"

[orm]
urls.db = "/db"
urls.query = "/queries/"
Expand Down
61 changes: 61 additions & 0 deletions frameworks/Python/aiohttp/nginx-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
set -e
CORES=$(nproc)
echo "$CORES cores detected, starting $CORES aiohttp workers..."

for i in $(seq 0 $((CORES-1))); do
PORT=$((8000 + i))
echo "Starting worker on port $PORT"
python3 -O -m app.app --port $PORT &
done

echo "Waiting for all workers to be ready..."
for i in $(seq 0 $((CORES-1))); do
PORT=$((8000 + i))
until nc -z localhost $PORT; do
echo "Waiting for port $PORT..."
sleep 0.1
done
done

cat > /aiohttp/nginx.conf <<EOF
worker_processes auto;

events {
worker_connections 65535;
}

http {
keepalive_requests 10000000;

upstream aiohttp {
least_conn;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any difference in performance if we drop this and use the default round-robin? Guessing this isn't really needed given that requests should be around 1ms.

EOF

for i in $(seq 0 $((CORES-1))); do
echo " server 127.0.0.1:$((8000 + i));" >> /aiohttp/nginx.conf
done

cat >> /aiohttp/nginx.conf <<EOF
keepalive 32;
Copy link
Contributor

@Dreamsorcerer Dreamsorcerer Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also wondering if this should be much higher? i.e. The aiohttp servers can handle many simultaneous connections.

With the above config, it might be that the workers are creating upto 65535 connections, of which 32 of them are allowed to be kept alive. So, we probably want this to be the same value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

65535 connections could also start slowing down the event loop, so also worth testing with lower numbers too, once these are aligned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried different options, someone helps but all of them insignificantly. So i decided to revert nginx as default proxy and keep it as separate docker file #9807

}

server {
listen 8080 reuseport;

access_log off;
error_log stderr error;

location / {
proxy_pass http://aiohttp;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_redirect off;
proxy_buffering off;
}
}
}
EOF

echo "Starting Nginx..."
nginx -c /aiohttp/nginx.conf -g "daemon off;"
Loading