Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
Add orjson to benchmark suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Packy White committed Sep 20, 2019
1 parent e8555aa commit 02e1884
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bench: ## Run benchmarks
pipenv run pytest benchmarks

.PHONY: bench-compare
bench-compare: ## Run benchmarks and compare results with other JSON encoders
bench-compare: nightly dev-packages install ## Run benchmarks and compare results with other JSON encoders
pipenv run pytest benchmarks --compare

.PHONY: plot
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pytest-benchmark = "*"
simplejson = "*"
ujson = "*"
yajl = "*"
orjson = "*"
python-rapidjson = "*"
wheel = "*"
pylint = "*"
Expand Down
18 changes: 15 additions & 3 deletions benchmarks/benchmark_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ujson
import simplejson
import yajl
import orjson

benchmark_results = []

Expand Down Expand Up @@ -55,7 +56,7 @@ def results_record_result(callback, is_encode, count):


def results_output_table():
LIBRARIES = ("hyperjson", "ujson", "yajl", "simplejson", "json")
LIBRARIES = ("hyperjson", "ujson", "yajl", "simplejson", "json", "orjson")
ENDC = '\033[0m'
GREEN = '\033[92m'

Expand Down Expand Up @@ -143,6 +144,9 @@ def dumps_with_yajl():
yajl.dumps(test_object)


def dumps_with_orjson():
orjson.dumps(test_object)

# =============================================================================
# JSON encoding with sort_keys=True.
# =============================================================================
Expand All @@ -166,6 +170,9 @@ def dumps_sorted_with_ujson():
ujson.dumps(test_object, ensure_ascii=False, sort_keys=True)


def dumps_sorted_with_orjson():
orjson.dumps(test_object, sort_keys=True)

# =============================================================================
# JSON decoding.
# =============================================================================
Expand All @@ -189,6 +196,9 @@ def loads_with_yajl():
yajl.loads(decode_data)


def load_with_orjson():
orjson.loads(test_object)

# =============================================================================
# Benchmarks.
# =============================================================================
Expand All @@ -199,6 +209,7 @@ def run_decode(count):
results_record_result(loads_with_simplejson, False, count)
results_record_result(loads_with_yajl, False, count)
results_record_result(loads_with_json, False, count)
results_record_result(loads_with_orjson, False, count)


def run_encode(count):
Expand All @@ -208,6 +219,7 @@ def run_encode(count):
results_record_result(dumps_with_simplejson, True, count)
results_record_result(dumps_with_yajl, True, count)
results_record_result(dumps_with_json, True, count)
results_record_result(dumps_with_orjson, True, count)


def run_encode_sort_keys(count):
Expand All @@ -217,6 +229,7 @@ def run_encode_sort_keys(count):
results_record_result(dumps_sorted_with_simplejson, True, count)
results_record_result(dumps_sorted_with_yajl, True, count)
results_record_result(dumps_sorted_with_json, True, count)
results_record_result(dumps_sorted_with_orjson, True, count)


def benchmark_array_doubles():
Expand Down Expand Up @@ -370,7 +383,6 @@ def benchmark_complex_object():
benchmark_array_true_values()
benchmark_array_of_dict_string_int_pairs()
benchmark_dict_of_arrays_of_dict_string_int_pairs()
# Disabled for now because of https://github.com/PyO3/pyo3/issues/177
# benchmark_complex_object()
benchmark_complex_object()
if not skip_lib_comparisons:
results_output_table()
9 changes: 9 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def pytest_addoption(parser):
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:
Expand Down

0 comments on commit 02e1884

Please sign in to comment.