Skip to content

Commit 8b6a4cf

Browse files
authored
Tuning
1 parent 3a2ec40 commit 8b6a4cf

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

frameworks/Python/blacksheep/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
READ_ROW_SQL = 'SELECT "id", "randomnumber" FROM "world" WHERE id = $1'
1717
WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
1818
ADDITIONAL_ROW = [0, "Additional fortune added at request time."]
19-
MAX_CONNECTIONS = 2000
19+
MAX_CONNECTIONS = 1900
2020
CORE_COUNT = multiprocessing.cpu_count()
21+
PROCESSES = CPU_CORES if os.getenv('GUNICORN') else min(8, CORE_COUNT // 4) if CORE_COUNT > 4 else CORE_COUNT
22+
MAX_POOL_SIZE = max(1,int(os.getenv('MAX_POOL_SIZE', MAX_CONNECTIONS // PROCESSES)))
23+
MIN_POOL_SIZE = max(1,int(os.getenv('MIN_POOL_SIZE', MAX_POOL_SIZE // 2)))
24+
2125
WORKER_PROCESSES = CORE_COUNT
2226
MAX_POOL_SIZE = max(1, MAX_CONNECTIONS // WORKER_PROCESSES)
2327
MIN_POOL_SIZE = max(1, MAX_POOL_SIZE // 2)

frameworks/Python/blacksheep/blacksheep-nginx-unit.dockerfile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ RUN pip3 install -r /blacksheep/requirements-uvicorn.txt -q
1111

1212
ENV PGSSLMODE=disable
1313
RUN CORE_COUNT=$(nproc) && \
14-
MAX_PROCESSES=$((CORE_COUNT <= 4 ? CORE_COUNT : CORE_COUNT / 2)) && \
15-
SPARE_PROCESSES=$((MAX_PROCESSES / 4 > 0 ? MAX_PROCESSES / 4 : 1)) && \
16-
sed -i "s|\"max\": [0-9]*|\"max\": $MAX_PROCESSES|g" /blacksheep/unit-config.json && \
17-
sed -i "s|\"spare\": [0-9]*|\"spare\": $SPARE_PROCESSES|g" /blacksheep/unit-config.json && \
18-
sed -i "s|\"idle_timeout\": [0-9]*|\"idle_timeout\": 3600|g" /blacksheep/unit-config.json
14+
PROCESSES=$(($CORE_COUNT <= 4 ? $CORE_COUNT : $CORE_COUNT / 4)) && \
15+
PROCESSES=$(($PROCESSES < 4 ? 4 : $PROCESSES > 16 ? 16 : $PROCESSES)) && \
16+
THREADS=$(($CORE_COUNT / $PROCESSES)) && \
17+
THREADS=$(($THREADS < 10 ? 10 : $THREADS)) && \
18+
MAX_POOL_SIZE=$((2000 / $PROCESSES)) && \
19+
MIN_POOL_SIZE=$(($MAX_POOL_SIZE / 2)) && \
20+
sed -i "s|\"processes\": [0-9]*.*\"threads\": [0-9]*|\"processes\": $PROCESSES, \"threads\": $THREADS|g" /blacksheep/unit-config.json && \
21+
echo "export MAX_POOL_SIZE=$MAX_POOL_SIZE" >> /blacksheep/env.sh && \
22+
echo "export MIN_POOL_SIZE=$MIN_POOL_SIZE" >> /blacksheep/env.sh
1923

2024
RUN chmod +x start-unit.sh
25+
RUN chmod +x env.sh
2126
ENTRYPOINT []
2227
EXPOSE 8080
2328

2429
# CMD ["unitd", "--no-daemon", "--control", "unix:/var/run/control.unit.sock"]
25-
CMD ["./start-unit.sh"]
30+
# CMD ["./start-unit.sh"]
31+
CMD ["bash", "-c", "source /blacksheep/env.sh && ./start-unit.sh"]

frameworks/Python/blacksheep/blacksheep.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ RUN pip3 install Cython==3.0.12 -q
99
RUN pip3 install -r /blacksheep/requirements.txt -q
1010
RUN pip3 install -r /blacksheep/requirements-gunicorn.txt -q
1111
RUN pip3 install -r /blacksheep/requirements-uvicorn.txt -q
12-
12+
ENV GUNICORN=1
1313
EXPOSE 8080
1414

1515
CMD gunicorn app:app -k uvicorn.workers.UvicornWorker -c blacksheep_conf.py

frameworks/Python/blacksheep/blacksheep_conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
_is_travis = os.environ.get('TRAVIS') == 'true'
55
CPU_CORES = multiprocessing.cpu_count()
6-
MAX_CONNECTIONS = 2000
7-
CONNECTIONS_PER_WORKER = 50
6+
MAX_CONNECTIONS = 1900
7+
CONNECTIONS_PER_WORKER = 100
88
max_pg_workers = MAX_CONNECTIONS // CONNECTIONS_PER_WORKER
99

10-
workers = min(CPU_CORES, max_pg_workers)
10+
workers = CPU_CORES
1111
if _is_travis:
1212
workers = 2
1313

frameworks/Python/blacksheep/start-unit.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env bash
2+
CORE_COUNT=$(nproc)
3+
sysctl -w net.core.somaxconn=65535
4+
sysctl -w net.ipv4.tcp_max_syn_backlog=65535
5+
ulimit -n 65535
6+
taskset -c 0-$(($CORE_COUNT-1)) unitd --no-daemon --control unix:/var/run/control.unit.sock &
27

3-
unitd --no-daemon --control unix:/var/run/control.unit.sock &
8+
# unitd --no-daemon --control unix:/var/run/control.unit.sock &
49

510
# wait UNIT started
611
sleep 1

frameworks/Python/blacksheep/unit-config.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
"type": "python",
1010
"path": "/blacksheep",
1111
"working_directory": "/blacksheep",
12+
"processes": 14,
13+
"threads": 10,
1214
"protocol": "asgi",
1315
"module": "app",
14-
"callable": "app",
15-
"processes": {
16-
"max": 56,
17-
"spare": 14,
18-
"idle_timeout": 3600
19-
}
16+
"callable": "app"
2017
}
2118
},
2219
"access_log": "/dev/null"

0 commit comments

Comments
 (0)