Skip to content

Commit

Permalink
pymem
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Sep 25, 2019
1 parent 0c281a0 commit 1b64cb1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bench/run_mem
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import sys
import lzma
import gc

import psutil

filename = sys.argv[1]

with lzma.open(filename, "r") as fileh:
file_utf8 = fileh.read()

proc = psutil.Process()

lib_name = sys.argv[2]
if lib_name == "json":
from json import dumps, loads
elif lib_name == "orjson":
from orjson import dumps, loads
elif lib_name == "rapidjson":
from rapidjson import dumps, loads
elif lib_name == "simplejson":
from simplejson import dumps, loads
elif lib_name == "ujson":
from ujson import dumps, loads
else:
raise NotImplementedError

gc.collect()

mem_before = proc.memory_info().rss

val = loads(file_utf8)

mem_after = proc.memory_info().rss

mem_diff = mem_after - mem_before

print(f"{mem_before},{mem_diff}")
37 changes: 37 additions & 0 deletions pymem
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import io
import subprocess

from tabulate import tabulate

buf = io.StringIO()

headers = ("Library", "import, read() RSS (MiB)", "loads() increase in RSS (MiB)")

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

FIXTURES = ("canada.json", "citm_catalog.json", "github.json", "twitter.json")

for fixture in sorted(FIXTURES, reverse=True):
table = []
buf.write("\n" + "#### " + fixture + "\n\n")
for lib_name in LIBRARIES:
proc = subprocess.Popen(
("bench/run_mem", f"data/{fixture}.xz", lib_name), stdout=subprocess.PIPE
)
output = proc.stdout.readline().decode("utf-8").split(",")
mem_base = int(output[0]) / 1024 / 1024
mem_diff = int(output[1]) / 1024 / 1024
table.append((lib_name, f"{mem_base:,.1f}", f"{mem_diff:,.1f}"))
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")

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

0 comments on commit 1b64cb1

Please sign in to comment.