Skip to content

Commit 0af5be1

Browse files
committed
Pre-read text, output to string
1 parent c5de64c commit 0af5be1

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

pyperformance/data-files/benchmarks/bm_docutils/run_benchmark.py

+27-30
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Convert Docutils' documentation from reStructuredText to <format>.
33
"""
44

5+
import contextlib
56
from pathlib import Path
6-
import shutil
7+
import time
78

89
import docutils
910
from docutils import core
@@ -16,36 +17,35 @@
1617
else:
1718
Trace.show = lambda message, channel: ... # don't print to console
1819

20+
DOC_ROOT = Path("data/docs").resolve()
1921

20-
def build_html(doc_root, out_root):
22+
23+
def build_html(doc_root):
24+
elapsed = 0
2125
for file in doc_root.rglob("*.txt"):
22-
try:
23-
dest = out_root / file.relative_to(doc_root).with_suffix(".html")
24-
dest.parent.mkdir(parents=True, exist_ok=True)
25-
core.publish_file(source_path=str(file),
26-
destination_path=str(dest),
27-
reader_name="standalone",
28-
parser_name="restructuredtext",
29-
writer_name="html5",
30-
settings_overrides={
31-
"input_encoding": "utf-8",
32-
"report_level": 5,
33-
})
34-
except docutils.ApplicationError:
35-
...
36-
37-
38-
def bench_docutils(loops, doc_root, out_root):
39-
runs = []
26+
file_contents = file.read_text(encoding="utf-8")
27+
t0 = time.perf_counter_ns()
28+
with contextlib.suppress(docutils.ApplicationError):
29+
core.publish_string(source=file_contents,
30+
reader_name="standalone",
31+
parser_name="restructuredtext",
32+
writer_name="html5",
33+
settings_overrides={
34+
"input_encoding": "unicode",
35+
"output_encoding": "unicode",
36+
"report_level": 5,
37+
})
38+
elapsed += time.perf_counter_ns() - t0
39+
return elapsed
40+
41+
42+
def bench_docutils(loops, doc_root):
43+
runs_total_ns = 0
4044

4145
for _ in range(loops):
42-
t0 = pyperf.perf_counter()
43-
build_html(doc_root, out_root)
44-
runs.append(pyperf.perf_counter() - t0)
45-
46-
shutil.rmtree(out_root, ignore_errors=True)
46+
runs_total_ns += build_html(doc_root)
4747

48-
return sum(runs)
48+
return runs_total_ns / 10**9
4949

5050

5151
if __name__ == "__main__":
@@ -54,7 +54,4 @@ def bench_docutils(loops, doc_root, out_root):
5454
runner.metadata['description'] = "Render documentation with Docutils"
5555
args = runner.parse_args()
5656

57-
DOC_ROOT = Path("data/docs").resolve()
58-
OUT_ROOT = Path("data/out").resolve()
59-
60-
runner.bench_time_func("docutils", bench_docutils, DOC_ROOT, OUT_ROOT)
57+
runner.bench_time_func("docutils", bench_docutils, DOC_ROOT)

0 commit comments

Comments
 (0)