Skip to content

Instantly share code, notes, and snippets.

@pax0r
Last active November 2, 2017 22:38
Show Gist options
  • Save pax0r/bc6ebe0bdcbcc0fd16c8f76ff1c2f76e to your computer and use it in GitHub Desktop.
Save pax0r/bc6ebe0bdcbcc0fd16c8f76ff1c2f76e to your computer and use it in GitHub Desktop.
flask-restplus performance test
'''
Model.restricted optimalization
WITHOUT cached_property:
------------------------
PATH: '/test/'
828222 function calls (682094 primitive calls) in 0.992 second
Ordered by: internal time, call count
List reduced from 207 to 10 due to restriction <10>
ncalls tottime percall cumtime percall filename:lineno(function)
118110/2001 0.344 0.000 0.814 0.000 /usr/local/lib/python3.6/copy.py:132(deepcopy)
4004/4001 0.093 0.000 0.490 0.000 /usr/local/lib/python3.6/copy.py:236(_deepcopy_dict)
240238 0.091 0.000 0.091 0.000 {method 'get' of 'dict' objects}
139131 0.043 0.000 0.043 0.000 {built-in method builtins.id}
4004/4001 0.038 0.000 0.617 0.000 /usr/local/lib/python3.6/copy.py:268(_reconstruct)
2002/2001 0.034 0.000 0.786 0.000 /home/vagrant/.venv/lib/python3.6/site-packages/flask_restplus/model.py:217(__deepcopy__)
108100 0.028 0.000 0.028 0.000 /usr/local/lib/python3.6/copy.py:190(_deepcopy_atomic)
17035/3024 0.022 0.000 0.854 0.000 {built-in method builtins.getattr}
10010 0.021 0.000 0.028 0.000 /usr/local/lib/python3.6/copy.py:252(_keep_alive)
2002 0.021 0.000 0.021 0.000 /home/vagrant/.venv/lib/python3.6/site-packages/flask_restplus/model.py:38(__init__)
As you can see there is 2001 primitive calls for deepcopy.
This number is equal to (2*n)+1 where n is a number of elements on nested List.
WITH cached_property:
---------------------
PATH: '/test/'
112941 function calls (96683 primitive calls) in 0.129 seconds
Ordered by: internal time, call count
List reduced from 207 to 10 due to restriction <10>
ncalls tottime percall cumtime percall filename:lineno(function)
24009/12009 0.015 0.000 0.027 0.000 /usr/local/lib/python3.6/json/encoder.py:333(_iterencode_dict)
1001/1 0.014 0.000 0.088 0.088 /home/vagrant/.venv/lib/python3.6/site-packages/flask_restplus/marshalling.py:13(marshal)
3002/2 0.009 0.000 0.086 0.043 /home/vagrant/.venv/lib/python3.6/site-packages/flask_restplus/marshalling.py:52(<genexpr>)
19081 0.008 0.000 0.008 0.000 {built-in method builtins.isinstance}
6025 0.008 0.000 0.008 0.000 {built-in method builtins.hasattr}
12003 0.008 0.000 0.023 0.000 /usr/local/lib/python3.6/json/encoder.py:277(_iterencode_list)
3001 0.007 0.000 0.034 0.000 /home/vagrant/.venv/lib/python3.6/site-packages/flask_restplus/fields.py:41(get_value)
3001 0.006 0.000 0.018 0.000 /home/vagrant/.venv/lib/python3.6/site-packages/flask_restplus/fields.py:59(_get_value_for_key)
2000 0.005 0.000 0.036 0.000 /home/vagrant/.venv/lib/python3.6/site-packages/flask_restplus/fields.py:139(output)
12009 0.005 0.000 0.032 0.000 /usr/local/lib/python3.6/json/encoder.py:412(_iterencode)
'''
from flask import Flask
from flask_restplus import Api, Resource, fields, Model
from werkzeug.contrib.profiler import ProfilerMiddleware
app = Flask(__name__)
api = Api(version='1.0', title='Test')
ns = api.namespace('test')
test_element = api.model('Element', {
'a': fields.Integer(),
'b': fields.String(),
})
test_list = api.model('TestList', {
'data': fields.List(fields.Nested(test_element))
})
@ns.route('/')
class TestCollection(Resource):
@api.marshal_list_with(test_list)
def get(self):
return {
'data': [
{'a': x, 'b': x}
for x in range(0, 1000)
]
}
if __name__ == '__main__':
app.config['PROFILE'] = True
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=(10, ))
api.init_app(app)
api.add_namespace(ns)
app.run(host='0.0.0.0', debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment