Skip to content

Commit 9c58774

Browse files
Add tomli_loads benchmark (#200)
1 parent 4a69828 commit 9c58774

File tree

6 files changed

+343084
-0
lines changed

6 files changed

+343084
-0
lines changed

Diff for: pyperformance/data-files/benchmarks/MANIFEST

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ sqlglot <local>
6666
sqlite_synth <local>
6767
sympy <local>
6868
telco <local>
69+
tomli_loads <local>
6970
tornado_http <local>
7071
unpack_sequence <local>
7172
unpickle <local:pickle>

Diff for: pyperformance/data-files/benchmarks/bm_tomli_loads/data/tomli-bench-data.toml

+343,022
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from urllib.request import urlopen
2+
import json
3+
import toml
4+
5+
BASE_URL = "https://api.github.com/repos/python/cpython/pulls?per_page=1000&state=all"
6+
7+
def main():
8+
all_issues = []
9+
for page in range(1, 11):
10+
with urlopen(f"{BASE_URL}&page={page}") as response:
11+
issues = json.loads(response.read())
12+
if not issues:
13+
break
14+
all_issues.extend(issues)
15+
print(f"Page: {page} Total Issues: {len(all_issues)}")
16+
with open("issues.toml", "w") as f:
17+
f.write(toml.dumps({"data": all_issues}))
18+
19+
if __name__ == "__main__":
20+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[project]
2+
name = "pyperformance_bm_tomli_loads"
3+
requires-python = ">=3.8"
4+
dependencies = ["pyperf", "tomli"]
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "tomli_loads"
10+
tags = "serialize"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tomli==2.0.1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Benchmark ``loads()`` function of the ``tomli`` module
3+
on a large TOML file of GitHub's real world data generated by
4+
the ``generate_data.py`` script.
5+
6+
It heavily exercises string operations such as concatenation,
7+
subscripting and iteration.
8+
9+
Author: Kumar Aditya
10+
"""
11+
12+
from pathlib import Path
13+
14+
import pyperf
15+
import tomli
16+
17+
DATA_FILE = Path(__file__).parent / "data" / "tomli-bench-data.toml"
18+
19+
def bench_tomli_loads(loops: int) -> float:
20+
data = DATA_FILE.read_text('utf-8')
21+
range_it = range(loops)
22+
t0 = pyperf.perf_counter()
23+
for _ in range_it:
24+
tomli.loads(data)
25+
return pyperf.perf_counter() - t0
26+
27+
if __name__ == "__main__":
28+
runner = pyperf.Runner()
29+
runner.metadata['description'] = "Benchmark tomli.loads()"
30+
runner.bench_time_func('tomli_loads', bench_tomli_loads)

0 commit comments

Comments
 (0)