Debian Codegraph @

test/expensive_benchmarks/bm_docutils.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """
3 Convert Docutils' documentation from reStructuredText to <format>.
4 """
5
6 import contextlib
7 from pathlib import Path
8 import time
9
10 import docutils
11 from docutils import core
12 import pyperf
13 import builtins
14
15 try:
16 builtins.profile
17 except AttributeError:
18 # No line profiler, provide a pass-through version
19 def profile(func):
20 return func
21
22 builtins.profile = profile
23
24 try:
25 from docutils.utils.math.math2html import Trace
26 except ImportError:
27 pass
28 else:
29 Trace.show = lambda message, channel: ... # don't print to console
30
31 DOC_ROOT = (Path(__file__).parent / "docutils_data" / "docs").resolve()
32
33
34 @profile
35 def build_html(doc_root):
36 elapsed = 0
37 for file in doc_root.rglob("*.txt"):
38 file_contents = file.read_text(encoding="utf-8")
39 # t0 = pyperf.perf_counter()
40 # with contextlib.suppress(docutils.ApplicationError):
41 core.publish_string(
42 source=file_contents,
43 reader_name="standalone",
44 parser_name="restructuredtext",
45 writer_name="html5",
46 settings_overrides={
47 "input_encoding": "unicode",
48 "output_encoding": "unicode",
49 "report_level": 5,
50 },
51 )
52 # elapsed += pyperf.perf_counter() - t0
53 # return elapsed
54
55
56 @profile
57 def bench_docutils(loops, doc_root):
58
59 for _ in range(loops):
60 build_html(doc_root)
61
62
63 if __name__ == "__main__":
64 # runner = pyperf.Runner()
65 start_p = time.perf_counter()
66 bench_docutils(5, DOC_ROOT)
67 stop_p = time.perf_counter()
68 print("Time elapsed: ", stop_p - start_p)