forked from mre/hyperjson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
79 lines (67 loc) · 2.13 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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))
try:
import rapidjson
except ImportError:
pass
else:
contenders.append(Contender('rapidjson',
rapidjson.dumps,
rapidjson.loads))
try:
import orjson
except ImportError:
pass
else:
contenders.append(Contender('orjson',
orjson.dumps,
orjson.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'))