Skip to content

Commit

Permalink
[qa] use orjson instead of json to improve performances
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBldy committed Sep 25, 2023
1 parent 1ec66b6 commit 207163b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ install_requires =
matterhook==0.2
meilisearch==0.28.3
OpenTimelineIO==0.15.0
orjson==3.9.7
pillow==10.0.1
psutil==5.9.5
psycopg[binary]==3.1.11
Expand Down
3 changes: 2 additions & 1 deletion zou/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@

from zou.app.utils import cache, fs, logs
from zou.app.utils.sentry import init_sentry
from zou.app.utils.user_agent import ParsedUserAgent
from zou.app.utils.flask import ParsedUserAgent, ORJSONProvider

init_sentry()
app = Flask(__name__)
app.json = ORJSONProvider(app)
app.request_class.user_agent_class = ParsedUserAgent
app.config.from_object(config)

Expand Down
17 changes: 17 additions & 0 deletions zou/app/utils/user_agent.py → zou/app/utils/flask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
from ua_parser import user_agent_parser
from werkzeug.user_agent import UserAgent
from werkzeug.utils import cached_property
from flask.json.provider import JSONProvider
import orjson


class ORJSONProvider(JSONProvider):
def __init__(self, *args, **kwargs):
self.options = kwargs
super().__init__(*args, **kwargs)

def loads(self, s, **kwargs):
return orjson.loads(s)

def dumps(self, obj, **kwargs):
# decode back to str, as orjson returns bytes
return orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS).decode(
"utf-8"
)


class ParsedUserAgent(UserAgent):
Expand Down
2 changes: 2 additions & 0 deletions zou/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from zou.app import config
from zou.app.stores import auth_tokens_store
from zou.app.utils.sentry import init_sentry
from zou.app.utils.flask import ORJSONProvider

server_stats = {"nb_connections": 0}
rooms_data = {}
Expand Down Expand Up @@ -216,6 +217,7 @@ def create_app():
)
init_sentry()
app = Flask(__name__)
app.json = ORJSONProvider(app)
app.config.from_object(config)
set_info_routes(socketio, app)
set_application_routes(socketio, app)
Expand Down

0 comments on commit 207163b

Please sign in to comment.