This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pytest.benchmark * pytest benchmarks * pytest benchmark * pytest benchmark * pytest benchmark * pytest benchmark * pytest benchmark * pytest benchmark * pytest benchmark
- Loading branch information
Showing
7 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import sys | ||
import random | ||
import pytest | ||
|
||
benchmark = pytest.importorskip('pytest_benchmark') | ||
|
||
doubles = [] | ||
numbers = [] | ||
unicode_strings = [] | ||
strings = [] | ||
booleans = [] | ||
list_dicts = [] | ||
dict_lists = {} | ||
|
||
composite_object = { | ||
'words': """ | ||
Lorem ipsum dolor sit amet, consectetur adipiscing | ||
elit. Mauris adipiscing adipiscing placerat. | ||
Vestibulum augue augue, | ||
pellentesque quis sollicitudin id, adipiscing. | ||
""", | ||
'list': list(range(200)), | ||
'dict': dict((str(i), 'a') for i in list(range(200))), | ||
'int': 100100100, | ||
'float': 100999.123456 | ||
} | ||
|
||
doubles = [sys.maxsize * random.random() for _ in range(256)] | ||
unicode_strings = [ | ||
"نظام الحكم سلطاني وراثي في الذكور من ذرية السيد تركي بن سعيد بن سلطان ويشترط فيمن يختار لولاية الحكم من بينهم ان يكون مسلما رشيدا عاقلا ًوابنا شرعيا لابوين عمانيين " for _ in range(256)] | ||
strings = ["A pretty long string which is in a list"] * 256 | ||
|
||
booleans = [True] * 256 | ||
|
||
for y in range(100): | ||
arrays = [] | ||
list_dicts.append({str(random.random()*20): int(random.random()*1000000)}) | ||
|
||
for x in range(100): | ||
arrays.append({str(random.random() * 20): int(random.random()*1000000)}) | ||
dict_lists[str(random.random() * 20)] = arrays | ||
|
||
user = { | ||
"userId": 3381293, | ||
"age": 213, | ||
"username": "johndoe", | ||
"fullname": u"John Doe the Second", | ||
"isAuthorized": True, | ||
"liked": 31231.31231202, | ||
"approval": 31.1471, | ||
"jobs": [1, 2], | ||
"currJob": None | ||
} | ||
|
||
friends = [user, user, user, user, user, user, user, user] | ||
complex_object = [ | ||
[user, friends], [user, friends], [user, friends], | ||
[user, friends], [user, friends], [user, friends] | ||
] | ||
datasets = [('composite object', composite_object), | ||
('256 doubles array', doubles), | ||
('256 unicode array', unicode_strings), | ||
('256 ascii array', strings), | ||
('256 Trues array', booleans), | ||
('100 dicts array', list_dicts), | ||
# ('100 arrays dict', dict_lists), | ||
('complex object', complex_object), | ||
] | ||
|
||
|
||
@pytest.mark.benchmark(group='serialize') | ||
@pytest.mark.parametrize('data', [d[1] for d in datasets], ids=[d[0] for d in datasets]) | ||
def test_dumps(contender, data, benchmark): | ||
benchmark(contender.dumps, data) | ||
|
||
|
||
@pytest.mark.benchmark(group='deserialize') | ||
@pytest.mark.parametrize('data', [d[1] for d in datasets], ids=[d[0] for d in datasets]) | ||
def test_loads(contender, data, benchmark): | ||
data = contender.dumps(data) | ||
benchmark(contender.loads, data) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# based on https://github.com/lelit/python-rapidjson/blob/master/benchmarks/conftest.py | ||
import hyperjson | ||
from collections import namedtuple | ||
from operator import attrgetter | ||
|
||
Contender = namedtuple('Contender', 'name,dumps,loads') | ||
|
||
|
||
def pytest_benchmark_group_stats(config, benchmarks, group_by): | ||
result = {} | ||
for bench in benchmarks: | ||
engine, data_kind = bench['param'].split('-') | ||
group = result.setdefault("%s: %s" % (data_kind, bench['group']), []) | ||
group.append(bench) | ||
return sorted(result.items()) | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption('--compare', action='store_true', | ||
help='compare against other JSON engines') | ||
|
||
|
||
contenders = [] | ||
|
||
contenders.append(Contender('hyperjson', | ||
hyperjson.dumps, | ||
hyperjson.loads)) | ||
try: | ||
import simplejson | ||
except ImportError: | ||
pass | ||
else: | ||
contenders.append(Contender('simplejson', | ||
simplejson.dumps, | ||
simplejson.loads)) | ||
try: | ||
import ujson | ||
except ImportError: | ||
pass | ||
else: | ||
contenders.append(Contender('ujson', | ||
ujson.dumps, | ||
ujson.loads)) | ||
try: | ||
import yajl | ||
except ImportError: | ||
pass | ||
else: | ||
contenders.append(Contender('yajl', | ||
yajl.dumps, | ||
yajl.loads)) | ||
|
||
|
||
def pytest_generate_tests(metafunc): | ||
if 'contender' in metafunc.fixturenames: | ||
if metafunc.config.getoption('compare'): | ||
metafunc.parametrize('contender', contenders, | ||
ids=attrgetter('name')) | ||
else: | ||
metafunc.parametrize( | ||
'contender', contenders[:1], ids=attrgetter('name')) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pytest>=3.5.0 | ||
pytest-runner>=4.2 # add setup.py test support for pytest | ||
pytest-benchmark>=3.1 | ||
|