Python flack+bjoern sample
- f01.py
#!/usr/bin/env python3.8
#
# Author, Copyright: Oleg Borodin <onborodin@gmail.com>
#
import sys
import os
import bjoern
from flask import Flask, jsonify
class daemon:
def fork():
pid = os.fork()
if pid > 0:
sys.exit(0)
os.chdir("/")
os.setsid()
os.umask(0)
pid = os.fork()
if pid > 0:
sys.exit(0)
def redirectIO():
handler = open('/dev/null', 'w')
sys.stdout = handler
sys.stderr = handler
app = Flask("hello")
@app.route('/', methods=['GET'])
def hello():
return jsonify({
'error': False,
'data': 'hello',
}), 200
daemon.fork()
daemon.redirectIO()
host = '0.0.0.0'
port = 5000
bjoern.listen(app, host, port)
bjoern.run()
Out
# python3.8 f01.py
# curl -v http://127.0.0.1:5000/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.63.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 31
< Connection: Keep-Alive
<
{"data":"hello","error":false}
1500 concurrent
PID USERNAME PRI NICE SIZE RES STATE C TIME WCPU COMMAND
7115 ziggi 103 0 42M 32M CPU3 3 1:20 99.87% python3.8
# ab -c 1500 -n 10000 http://localhost:5000/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software:
Server Hostname: localhost
Server Port: 5000
Document Path: /
Document Length: 31 bytes
Concurrency Level: 1500
Time taken for tests: 3.238 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1210000 bytes
HTML transferred: 310000 bytes
Requests per second: 3088.13 [#/sec] (mean)
Time per request: 485.731 [ms] (mean)
Time per request: 0.324 [ms] (mean, across all concurrent requests)
Transfer rate: 364.91 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 26 134.3 0 1002
Processing: 26 425 195.1 395 1095
Waiting: 1 425 193.9 395 1037
Total: 32 451 243.2 398 1420
Percentage of the requests served within a certain time (ms)
50% 398
66% 412
75% 416
80% 418
90% 863
95% 868
98% 1087
99% 1395
100% 1420 (longest request)
1700 concurrent
# ab -c 1700 -n 10000 http://localhost:5000/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Send request failed!
apr_socket_recv: Connection reset by peer (54)
Total of 5277 requests completed