Created
August 12, 2016 16:45
-
-
Save cristiklein/86353b559dfda1e6df1217c677d88fe2 to your computer and use it in GitHub Desktop.
MVE for performance bug in `flask_restful.fields.Url`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from flask import Flask | |
from flask_restful import fields, reqparse, Api, Resource, marshal | |
import time | |
app = Flask(__name__) | |
api = Api(app) | |
items_fields = { | |
'info': fields.String, | |
'large_info_url': fields.Url('large_info'), | |
} | |
items_fields_without_url = { | |
'info': fields.String, | |
} | |
# Large object, created outside the code whose performance is measured | |
LARGE_INFO = [ 'lots_of_information_goes_here' for _ in range(0, 100000) ] | |
# Constant list of items to return | |
items = [ { | |
'id': 0, | |
'info': 'hello', | |
'large_info': LARGE_INFO, | |
} ] | |
parser = reqparse.RequestParser() | |
parser.add_argument('omit_url', type=bool, help='Do not serialize URLs', | |
default=False) | |
class Items(Resource): | |
def get(self): | |
args = parser.parse_args() | |
if args['omit_url']: | |
return marshal(items, items_fields_without_url) | |
else: | |
return marshal(items, items_fields) | |
class LargeInfo(Resource): | |
def get(self, id): | |
pass | |
api.add_resource(Items, | |
'/items/', | |
endpoint='items') | |
api.add_resource(LargeInfo, | |
'/items/<id>/large_info', | |
endpoint='large_info') | |
if __name__=='__main__': | |
app = app.test_client() | |
started_at = time.time() | |
app.get('/items/') | |
ended_at = time.time() | |
print('GET w/ URL took: {0}s'.format(ended_at - started_at)) | |
started_at = time.time() | |
app.get('/items/', data = { 'omit_url': True }) | |
ended_at = time.time() | |
print('GET w/o URL took: {0}s'.format(ended_at - started_at)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET w/ URL took: 1.2314190864562988s | |
GET w/o URL took: 0.0008442401885986328s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment