From 07c86326fbc6af7503d3cebe0b75e9d58c078481 Mon Sep 17 00:00:00 2001 From: Thomas Aarholt Date: Wed, 13 Sep 2023 11:01:10 +0200 Subject: [PATCH 1/4] write_html directly to file --- pyinstrument/profiler.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pyinstrument/profiler.py b/pyinstrument/profiler.py index 74dd8447..7cf4b38f 100644 --- a/pyinstrument/profiler.py +++ b/pyinstrument/profiler.py @@ -1,6 +1,8 @@ from __future__ import annotations import inspect +import os +from pathlib import Path import sys import time import types @@ -304,6 +306,13 @@ def output_html(self, timeline: bool = False) -> str: """ return self.output(renderer=renderers.HTMLRenderer(timeline=timeline)) + def write_html(self, path: str | os.PathLike[str], timeline: bool = False): + """ + Return the profile output as HTML, as rendered by :class:`HTMLRenderer` + """ + file = Path(path) + file.write_text(self.output(renderer=renderers.HTMLRenderer(timeline=timeline))) + def open_in_browser(self, timeline: bool = False): """ Opens the last profile session in your web browser. From 5a38e37ad4969cd7f67ede312f90a427e63aa642 Mon Sep 17 00:00:00 2001 From: Thomas Aarholt Date: Wed, 13 Sep 2023 11:48:03 +0200 Subject: [PATCH 2/4] update docstring --- pyinstrument/profiler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyinstrument/profiler.py b/pyinstrument/profiler.py index 7cf4b38f..3225d552 100644 --- a/pyinstrument/profiler.py +++ b/pyinstrument/profiler.py @@ -2,10 +2,10 @@ import inspect import os -from pathlib import Path import sys import time import types +from pathlib import Path from time import process_time from typing import IO, Any @@ -308,7 +308,7 @@ def output_html(self, timeline: bool = False) -> str: def write_html(self, path: str | os.PathLike[str], timeline: bool = False): """ - Return the profile output as HTML, as rendered by :class:`HTMLRenderer` + Writes the profile output as HTML to a file, as rendered by :class:`HTMLRenderer` """ file = Path(path) file.write_text(self.output(renderer=renderers.HTMLRenderer(timeline=timeline))) From 22a0c616509327f8da2a2e0cf54d7ac0d07ed1f2 Mon Sep 17 00:00:00 2001 From: Thomas Aarholt Date: Wed, 13 Sep 2023 14:16:32 +0200 Subject: [PATCH 3/4] use new method instead of output_html in guide and metrics --- docs/guide.md | 3 +-- metrics/interrupt.py | 3 +-- metrics/overflow.py | 3 +-- metrics/overhead.py | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/docs/guide.md b/docs/guide.md index d2177874..b6fb68d8 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -294,8 +294,7 @@ def auto_profile(request): profiler.stop() PROFILE_ROOT.mkdir(exist_ok=True) results_file = PROFILE_ROOT / f"{request.node.name}.html" - with open(results_file, "w", encoding="utf-8") as f_html: - f_html.write(profiler.output_html()) + profiler.write_html(results_file) ``` This will generate a HTML file for each test node in your test suite inside diff --git a/metrics/interrupt.py b/metrics/interrupt.py index 76f1baa5..aa446df5 100644 --- a/metrics/interrupt.py +++ b/metrics/interrupt.py @@ -21,5 +21,4 @@ def func(): print(p.output_text()) -with open("ioerror_out.html", "w") as f: - f.write(p.output_html()) +p.write_html("ioerror_out.html") diff --git a/metrics/overflow.py b/metrics/overflow.py index 912486f3..a5c61756 100644 --- a/metrics/overflow.py +++ b/metrics/overflow.py @@ -21,5 +21,4 @@ def func(num): print(p.output_text()) -with open("overflow_out.html", "w") as f: - f.write(p.output_html()) +p.write_html("overflow_out.html") diff --git a/metrics/overhead.py b/metrics/overhead.py index ca906b8d..a71b53a7 100644 --- a/metrics/overhead.py +++ b/metrics/overhead.py @@ -51,8 +51,7 @@ def test_func_template(): # pyinstrument_timeline_timings = test_func() # profiler.stop() -with open("out.html", "w") as f: - f.write(profiler.output_html()) +profiler.write_html("out.html") print(profiler.output_text(unicode=True, color=True)) From e94b294fe9dd3867b7359f3b1c252ac5f2aa9042 Mon Sep 17 00:00:00 2001 From: Thomas Aarholt Date: Fri, 6 Oct 2023 16:56:25 +0200 Subject: [PATCH 4/4] write as utf-8 --- pyinstrument/profiler.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyinstrument/profiler.py b/pyinstrument/profiler.py index 3225d552..eef5f449 100644 --- a/pyinstrument/profiler.py +++ b/pyinstrument/profiler.py @@ -311,7 +311,10 @@ def write_html(self, path: str | os.PathLike[str], timeline: bool = False): Writes the profile output as HTML to a file, as rendered by :class:`HTMLRenderer` """ file = Path(path) - file.write_text(self.output(renderer=renderers.HTMLRenderer(timeline=timeline))) + file.write_text( + self.output(renderer=renderers.HTMLRenderer(timeline=timeline)), + encoding="utf-8", + ) def open_in_browser(self, timeline: bool = False): """