From 2001ad30fe097497d687cf82b1f4537b867f7ca8 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Sat, 5 Feb 2022 11:16:04 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20MAINTAIN:=20Add=20a=20profiler?= =?UTF-8?q?=20tox=20env?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MANIFEST.in | 1 + profiler.py | 19 +++++++++++++++++++ setup.cfg | 2 ++ tox.ini | 13 +++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 profiler.py diff --git a/MANIFEST.in b/MANIFEST.in index 99f9f382..1302ef01 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -14,6 +14,7 @@ exclude .circleci exclude .circleci/config.yml exclude codecov.yml exclude .mypy.ini +exclude profiler.py include LICENSE include LICENSE.markdown-it diff --git a/profiler.py b/profiler.py new file mode 100644 index 00000000..414a7727 --- /dev/null +++ b/profiler.py @@ -0,0 +1,19 @@ +"""A script for profiling. + +To generate and read results: + - `tox -e profile` + - `firefox .tox/prof/output.svg` +""" +from pathlib import Path + +from markdown_it import MarkdownIt + +commonmark_spec = ( + (Path(__file__).parent / "tests" / "test_cmark_spec" / "spec.md") + .read_bytes() + .decode() +) + +# Run this a few times to emphasize over imports and other overhead above +for _ in range(10): + MarkdownIt().render(commonmark_spec) diff --git a/setup.cfg b/setup.cfg index 2815e3eb..d5859efb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -70,6 +70,8 @@ benchmarking = psutil pytest pytest-benchmark~=3.2 +profiling = + gprof2dot [options.packages.find] exclude = diff --git a/tox.ini b/tox.ini index 158faff1..c169f982 100644 --- a/tox.ini +++ b/tox.ini @@ -43,3 +43,16 @@ setenv = commands = clean: rm -rf docs/_build sphinx-build -nW --keep-going -b {posargs:html} docs/ docs/_build/{posargs:html} + +[testenv:profile] +description = run profiler (use e.g. `firefox .tox/prof/output.svg` to open) +extras = profiling +allowlist_externals = + mkdir + dot +commands = + mkdir -p "{toxworkdir}/prof" + python -m cProfile -o "{toxworkdir}/prof/output.pstats" profiler.py + gprof2dot -f pstats -o "{toxworkdir}/prof/output.dot" "{toxworkdir}/prof/output.pstats" + dot -Tsvg -o "{toxworkdir}/prof/output.svg" "{toxworkdir}/prof/output.dot" + python -c 'import pathlib; print("profiler svg output under file://\{0\}".format(pathlib.Path(r"{toxworkdir}") / "prof" / "output.svg"))'