I compared Django, Flask, and Pyramid in previous article, and Flask was good, so I tried it immediately.
We benchmark and compare the features built according to the Blueprint Tutorial, which is said to be for large scale in the Python web framework called Flask. Blueprint is a Flask function that separates Views for each of multiple small apps (each function).
After building, I used Apache Bench to get a ** benchmark ** comparing PyPy3 2.4 and Python 3.5.
Unlike the default single view, a views directory is set up, and views for each function are set up in that directory. This time I added the root and report functions.
■ Directory structure

Separates common parts such as HTML HEAD and BODY tags by separating them from master.html.
master.html
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset="utf-8" />
  <title>{% block title %}{% endblock %} - Hello Flask!</title>
  <link type="text/css" rel="stylesheet"
       href="{{ url_for('static',
                        filename='hello.css')}}" />
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
index.html
{% extends "master.html" %}
{% block title %}index{% endblock %}
{% block body %}
<h1>this is index page</h1><br />
<h3>report</h3>
<a href="{{ url_for('report.index') }}">report - index</a><br />
<a href="{{ url_for('report.report_list') }}">report - list</a><br />
{% endblock %}
■ Depiction result of index.html

The Flask server is started with the python main.py command. Associate the URL for each function with main.py.
main.py
# -*- coding: utf-8 -*-
from flask import Flask
from views import root, report
app = Flask(__name__)
#Define URL for each function
app.register_blueprint(root.app, url_prefix="/")
app.register_blueprint(report.app, url_prefix="/report")
if __name__ == '__main__':
    app.run(debug=True)
Define the site top page.
root.py
# -*- coding: utf-8 -*-
from flask import render_template, Blueprint
#The name of the first argument is the template url_Linked with the name when calling in for
app = Blueprint("index",
                __name__,
                url_prefix='/<user_url_slug>')
#Url when calling in template_for('index.index')
@app.route("/")
def index():
    return render_template('root/index.html')
report.py
# -*- coding: utf-8 -*-
from flask import Module, render_template, Blueprint
app = Blueprint('report',
                __name__,
                url_prefix='/<user_url_slug>')
#Url when calling in template_for('report.index')
@app.route('/', methods=['GET'], strict_slashes=False)
def index():
    return 'report index'
#Url when calling in template_for('report.report_list')
@app.route('/report_list', methods=['GET'], strict_slashes=False)
def report_list():
    return 'report - list'
I set debug = False and tried to request 10,000 times with 100 parallelism with Apache Bench command. Python 3.5 ran 26% faster. Since the server was started with the python main.py command, it is a benchmark in a single process. I expected PyPy to hit at an overwhelming speed, but when I opened the lid, I ended up with a mysterious result that Python 3.5 was faster. flask is Flask == 0.10.1. Next, I would like to investigate with DB access.

PyPy3 bench results
>>>ab -n 10000 -c 100 http://localhost:5000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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:        Werkzeug/0.11.2
Server Hostname:        localhost
Server Port:            5000
Document Path:          /
Document Length:        398 bytes
Concurrency Level:      100
Time taken for tests:   20.783 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      5530000 bytes
HTML transferred:       3980000 bytes
Requests per second:    481.16 [#/sec](mean)
Time per request:       207.829 [ms](mean)
Time per request:       2.078 [ms](mean, across all concurrent requests)
Transfer rate:          259.85 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.6      1      74
Processing:     3  205  53.6    199     762
Waiting:        3  205  53.1    199     760
Total:         24  206  53.0    199     763
Percentage of the requests served within a certain time (ms)
  50%    199
  66%    204
  75%    211
  80%    214
  90%    225
  95%    236
  98%    295
  99%    571
 100%    763 (longest request)
++++++++++++++++++++++++++
>>> python --version
Python 3.2.5 (b2091e973da6, Oct 19 2014, 18:30:58)
[PyPy 2.4.0 with GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)]
Python3.5 bench results
>>>ab -n 10000 -c 100 http://localhost:5000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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:        Werkzeug/0.11.2
Server Hostname:        localhost
Server Port:            5000
Document Path:          /
Document Length:        398 bytes
Concurrency Level:      100
Time taken for tests:   16.380 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      5530000 bytes
HTML transferred:       3980000 bytes
Requests per second:    610.52 [#/sec](mean)
Time per request:       163.795 [ms](mean)
Time per request:       1.638 [ms](mean, across all concurrent requests)
Transfer rate:          329.70 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   5.8      0     195
Processing:     3  161  44.7    148     475
Waiting:        3  161  44.5    148     475
Total:         59  162  43.9    148     476
Percentage of the requests served within a certain time (ms)
  50%    148
  66%    151
  75%    154
  80%    156
  90%    239
  95%    276
  98%    299
  99%    322
 100%    476 (longest request)
++++++++++++++++++++++++++4
>>> python --version
Python 3.5.0
If the degree of parallelism is 1, the response speed is 1.8-2.5ms. So far Flask is very fast (・ ㅂ ・) و
Recommended Posts