Flask + PyPy I took a speed benchmark with Blueprint for large-scale Web

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.

Directory structure

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 スクリーンショット 2015-12-11 16.36.56.png

Template abstraction

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 スクリーンショット 2015-12-11 16.41.33.png

URL abstraction and top page definition

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)

Definition of root function

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 function definition

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'

Benchmark with PyPy3 2.4 and Python 3.5

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.

スクリーンショット 2015-12-11 17.20.24.png

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

Flask + PyPy I took a speed benchmark with Blueprint for large-scale Web
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Create a simple web app with flask
I took a benchmark of h5py compression
Create a web service with Docker + Flask
I made a WEB application with Django
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Launch a web server with Python and Flask
I made a Mattermost bot with Python (+ Flask)
I made a web application that maps IT event information with Vue and Flask
I want to transition with a button in flask
I made a window for Log output with Tkinter
I tried a simple RPA for login with selenium
I created an environment for Masonite, a Python web framework similar to Laravel, with Docker!
I made a simple book application with python + Flask ~ Introduction ~
(Failure) Deploy a web app made with Flask on heroku
I made a resource monitor for Raspberry Pi with a spreadsheet
Build a speed of light web API server with Falcon
(For beginners) Try creating a simple web API with Django
Web application development with Flask
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
I made a Nyanko tweet form with Python, Flask and Heroku
I searched for a similar card of Hearthstone with Deep Learning
I made a lot of files for RDP connection with Python
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
I made a scaffolding tool for the Python web framework Bottle
I tried to make a strange quote for Jojo with LSTM