perf: Json dump speedup (#954)

Use ujson if available
This commit is contained in:
Martin Asell Backlund 2017-01-18 01:18:01 +08:00 committed by Kurt Griffiths
parent e97e9aedaa
commit f552316681
12 changed files with 52 additions and 17 deletions

View File

@ -14,7 +14,10 @@
"""HTTPError exception class."""
import json
try:
import ujson as json
except ImportError:
import json
import xml.etree.ElementTree as et
try:
@ -170,8 +173,7 @@ class HTTPError(Exception):
"""
obj = self.to_dict(OrderedDict)
return json.dumps(obj, indent=4, separators=(',', ': '),
ensure_ascii=False)
return json.dumps(obj, ensure_ascii=False)
def to_xml(self):
"""Returns an XML-encoded representation of the error.

View File

@ -13,7 +13,10 @@
"""Request class."""
from datetime import datetime
import json
try:
import ujson as json
except ImportError:
import json
try:
# NOTE(kgrifs): In Python 2.6 and 2.7, socket._fileobject is a

View File

@ -32,7 +32,10 @@ This package includes utilities for simulating HTTP requests against a
WSGI callable, without having to stand up a WSGI server.
"""
import json
try:
import ujson as json
except ImportError:
import json
import platform
import re
import wsgiref.validate

View File

@ -24,7 +24,10 @@ directly from the `testing` package::
"""
from json import dumps as json_dumps
try:
from ujson import dumps as json_dumps
except ImportError:
from json import dumps as json_dumps
import falcon
from .helpers import rand_string

View File

@ -1,5 +1,8 @@
import functools
import json
try:
import ujson as json
except ImportError:
import json
import falcon
from falcon import testing
@ -179,11 +182,11 @@ class TestHooks(testing.TestCase):
def test_output_validator(self):
result = self.simulate_get()
self.assertEqual(result.status_code, 723)
self.assertEqual(result.text, '{\n "title": "Tricky"\n}')
self.assertEqual(result.text, json.dumps({'title': 'Tricky'}))
def test_serializer(self):
result = self.simulate_put()
self.assertEqual('{"animal": "falcon"}', result.text)
self.assertEqual(result.text, json.dumps({'animal': 'falcon'}))
def test_hook_as_callable_class(self):
result = self.simulate_post()

View File

@ -1,6 +1,9 @@
import functools
import io
import json
try:
import ujson as json
except ImportError:
import json
import falcon
import falcon.testing as testing

View File

@ -1,4 +1,7 @@
import json
try:
import ujson as json
except ImportError:
import json
import logging
import uuid
from wsgiref import simple_server

View File

@ -1,7 +1,10 @@
# -*- coding: utf-8
import datetime
import json
try:
import ujson as json
except ImportError:
import json
import xml.etree.ElementTree as et
import ddt
@ -284,7 +287,7 @@ class TestHTTPError(testing.TestBase):
def test_no_description_json(self):
body = self.simulate_request('/fail', method='PATCH')
self.assertEqual(self.srmock.status, falcon.HTTP_400)
self.assertEqual(body, [b'{\n "title": "400 Bad Request"\n}'])
self.assertEqual(body, [json.dumps({'title': '400 Bad Request'}).encode('utf8')])
def test_no_description_xml(self):
body = self.simulate_request('/fail', method='PATCH',

View File

@ -1,5 +1,8 @@
from datetime import datetime
import json
try:
import ujson as json
except ImportError:
import json
import falcon
import falcon.testing as testing

View File

@ -1,5 +1,8 @@
from datetime import date
import json
try:
import ujson as json
except ImportError:
import json
import ddt

View File

@ -3,7 +3,10 @@
from datetime import datetime
import functools
import io
import json
try:
import ujson as json
except ImportError:
import json
import random
import sys

View File

@ -1,4 +1,7 @@
import json
try:
import ujson as json
except ImportError:
import json
import falcon
from falcon import testing