Skip to content

Commit

Permalink
benchmark misc
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Jun 1, 2022
1 parent a956bed commit d7f9acc
Show file tree
Hide file tree
Showing 20 changed files with 35 additions and 114 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -986,15 +986,6 @@ format, containing floats and arrays, indented.

### Latency

![alt text](doc/twitter_serialization.png "twitter.json serialization")
![alt text](doc/twitter_deserialization.png "twitter.json deserialization")
![alt text](doc/github_serialization.png "github.json serialization")
![alt text](doc/github_deserialization.png "github.json deserialization")
![alt text](doc/citm_catalog_serialization.png "citm_catalog.json serialization")
![alt text](doc/citm_catalog_deserialization.png "citm_catalog.json deserialization")
![alt text](doc/canada_serialization.png "canada.json serialization")
![alt text](doc/canada_deserialization.png "canada.json deserialization")

#### twitter.json serialization

| Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
Expand Down
4 changes: 2 additions & 2 deletions bench/benchmark_dumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from .data import fixtures, libraries
from .util import read_fixture_str
from .util import read_fixture_obj


@pytest.mark.parametrize("library", libraries)
Expand All @@ -14,6 +14,6 @@ def test_dumps(benchmark, fixture, library):
dumper, loader = libraries[library]
benchmark.group = f"{fixture} serialization"
benchmark.extra_info["lib"] = library
data = read_fixture_str(f"{fixture}.xz")
data = read_fixture_obj(f"{fixture}.xz")
benchmark.extra_info["correct"] = json_loads(dumper(data)) == data
benchmark(dumper, data)
4 changes: 2 additions & 2 deletions bench/benchmark_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from .data import fixtures, libraries
from .util import read_fixture_str
from .util import read_fixture


@pytest.mark.parametrize("fixture", fixtures)
Expand All @@ -14,7 +14,7 @@ def test_loads(benchmark, fixture, library):
dumper, loader = libraries[library]
benchmark.group = f"{fixture} deserialization"
benchmark.extra_info["lib"] = library
data = read_fixture_str(f"{fixture}.xz")
data = read_fixture(f"{fixture}.xz")
benchmark.extra_info["correct"] = json_loads(dumper(loader(data))) == json_loads(
data
)
Expand Down
1 change: 0 additions & 1 deletion bench/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
matplotlib
memory-profiler
pytest-benchmark
pytest-random-order
Expand Down
6 changes: 3 additions & 3 deletions bench/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@


@lru_cache(maxsize=None)
def read_fixture_str(filename: str) -> str:
def read_fixture(filename: str) -> bytes:
path = Path(dirname, filename)
if path.suffix == ".xz":
contents = lzma.decompress(path.read_bytes())
else:
contents = path.read_bytes()
return contents.decode("utf-8")
return contents


@lru_cache(maxsize=None)
def read_fixture_obj(filename: str) -> Any:
return orjson.loads(read_fixture_str(filename))
return orjson.loads(read_fixture(filename))
Binary file removed doc/canada_deserialization.png
Binary file not shown.
Binary file removed doc/canada_serialization.png
Binary file not shown.
Binary file removed doc/citm_catalog_deserialization.png
Binary file not shown.
Binary file removed doc/citm_catalog_serialization.png
Binary file not shown.
Binary file removed doc/github_deserialization.png
Binary file not shown.
Binary file removed doc/github_serialization.png
Binary file not shown.
Binary file removed doc/twitter_deserialization.png
Binary file not shown.
Binary file removed doc/twitter_serialization.png
Binary file not shown.
39 changes: 6 additions & 33 deletions script/graph
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

import collections
import io
import json
import os
import sys

import matplotlib.pyplot as plt
from tabulate import tabulate

import orjson

LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json")


Expand All @@ -18,7 +17,7 @@ def aggregate():
res = collections.defaultdict(dict)
for filename in os.listdir(benchmarks_dir):
with open(os.path.join(benchmarks_dir, filename), "r") as fileh:
data = json.loads(fileh.read())
data = orjson.loads(fileh.read())

for each in data["benchmarks"]:
res[each["group"]][each["extra_info"]["lib"]] = {
Expand All @@ -30,21 +29,6 @@ def aggregate():
return res


def box(obj):
for group, val in sorted(obj.items()):
data = []
for lib in LIBRARIES:
data.append(val[lib]["data"] if val[lib]["correct"] else -1)
fig = plt.figure(1, figsize=(9, 6))
ax = fig.add_subplot(111)
bp = ax.boxplot(data, vert=False, labels=LIBRARIES)
ax.set_xlim(left=0)
ax.set_xlabel("milliseconds")
plt.title(group)
plt.savefig("doc/{}.png".format(group.replace(" ", "_").replace(".json", "")))
plt.close()


def tab(obj):
buf = io.StringIO()
headers = (
Expand Down Expand Up @@ -72,20 +56,9 @@ def tab(obj):
"%.2f" % (each[1] / baseline) if isinstance(each[1], float) else None
)
each[1] = "%.2f" % each[1] if isinstance(each[1], float) else None
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")
buf.write(tabulate(table, headers, tablefmt="github") + "\n")

print(
buf.getvalue()
.replace("-", "")
.replace("=", "-")
.replace("+", "|")
.replace("|||||", "")
.replace("\n\n", "\n")
)
print(buf.getvalue())


try:
locals()[sys.argv[1]](aggregate())
except KeyError:
sys.stderr.write("usage: graph (box|tab)\n")
sys.exit(1)
tab(aggregate())
15 changes: 4 additions & 11 deletions script/pydataclass
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,7 @@ for lib_name in LIBRARIES:
)

buf = io.StringIO()
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")

print(
buf.getvalue()
.replace("-", "")
.replace("*", "-")
.replace("=", "-")
.replace("+", "|")
.replace("|||||", "")
.replace("\n\n", "\n")
)
buf.write(tabulate(table, headers, tablefmt="github"))
buf.write("\n")

print(buf.getvalue())
13 changes: 3 additions & 10 deletions script/pyindent
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,7 @@ for lib_name in LIBRARIES:
)

buf = io.StringIO()
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")
buf.write(tabulate(table, headers, tablefmt="github"))
buf.write("\n")

print(
buf.getvalue()
.replace("-", "")
.replace("*", "-")
.replace("=", "-")
.replace("+", "|")
.replace("|||||", "")
.replace("\n\n", "\n")
)
print(buf.getvalue())
13 changes: 3 additions & 10 deletions script/pymem
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ for fixture in sorted(FIXTURES, reverse=True):
table.append((lib_name, f"{mem_base:,.1f}", f"{mem_diff:,.1f}"))
else:
table.append((lib_name, "", ""))
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")

print(
buf.getvalue()
.replace("-", "")
.replace("=", "-")
.replace("+", "|")
.replace("||||", "")
.replace("\n\n", "\n")
)
buf.write(tabulate(table, headers, tablefmt="github") + "\n")

print(buf.getvalue())
15 changes: 4 additions & 11 deletions script/pynonstr
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,7 @@ for lib_name in LIBRARIES:
)

buf = io.StringIO()
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")

print(
buf.getvalue()
.replace("-", "")
.replace("*", "-")
.replace("=", "-")
.replace("+", "|")
.replace("|||||", "")
.replace("\n\n", "\n")
)
buf.write(tabulate(table, headers, tablefmt="github"))
buf.write("\n")

print(buf.getvalue())
15 changes: 4 additions & 11 deletions script/pynumpy
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,7 @@ for lib_name in LIBRARIES:
)

buf = io.StringIO()
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")

print(
buf.getvalue()
.replace("-", "")
.replace("*", "-")
.replace("=", "-")
.replace("+", "|")
.replace("|||||", "")
.replace("\n\n", "\n")
)
buf.write(tabulate(table, headers, tablefmt="github"))
buf.write("\n")

print(buf.getvalue())
15 changes: 4 additions & 11 deletions script/pysort
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,7 @@ for lib_name in LIBRARIES:
)

buf = io.StringIO()
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")

print(
buf.getvalue()
.replace("-", "")
.replace("*", "-")
.replace("=", "-")
.replace("+", "|")
.replace("|||||", "")
.replace("\n\n", "\n")
)
buf.write(tabulate(table, headers, tablefmt="github"))
buf.write("\n")

print(buf.getvalue())

0 comments on commit d7f9acc

Please sign in to comment.