From f8f4c16020b8aff3fca398e9f8c6d16f8c001fed Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 24 Oct 2018 16:00:33 +0200 Subject: [PATCH] TerminalWriter: write "collecting" msg only once every 0.1s Running `pytest -k doesnotmatch` on pytest's own tests takes ~3s with Kitty terminal for me, but only ~1s with `-q`. It also is faster with urxvt, but still takes 2.2s there. This patch only calls `report_collect` every 0.1s, which is good enough for reporting collection progress, and improves the time with both Kitty and urxvt to ~1.2s for me. --- changelog/4225.feature.rst | 3 +++ src/_pytest/terminal.py | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 changelog/4225.feature.rst diff --git a/changelog/4225.feature.rst b/changelog/4225.feature.rst new file mode 100644 index 000000000..ffdf0e83f --- /dev/null +++ b/changelog/4225.feature.rst @@ -0,0 +1,3 @@ +Improve performance with collection reporting in non-quiet mode with terminals. + +The "collecting …" message is only printed/updated every 0.5s. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index d207dd785..47d8656d7 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -248,6 +248,7 @@ class TerminalReporter(object): self.isatty = file.isatty() self._progress_nodeids_reported = set() self._show_progress_info = self._determine_show_progress_info() + self._collect_report_last_write = None def _determine_show_progress_info(self): """Return True if we should display progress information based on the current config""" @@ -474,7 +475,11 @@ class TerminalReporter(object): return self._tw.chars_on_current_line def pytest_collection(self): - if not self.isatty and self.config.option.verbose >= 1: + if self.isatty: + if self.config.option.verbose >= 0: + self.write("collecting ... ", bold=True) + self._collect_report_last_write = time.time() + elif self.config.option.verbose >= 1: self.write("collecting ... ", bold=True) def pytest_collectreport(self, report): @@ -485,13 +490,19 @@ class TerminalReporter(object): items = [x for x in report.result if isinstance(x, pytest.Item)] self._numcollected += len(items) if self.isatty: - # self.write_fspath_result(report.nodeid, 'E') self.report_collect() def report_collect(self, final=False): if self.config.option.verbose < 0: return + if not final: + # Only write "collecting" report every 0.5s. + t = time.time() + if self._collect_report_last_write > t - 0.5: + return + self._collect_report_last_write = t + errors = len(self.stats.get("error", [])) skipped = len(self.stats.get("skipped", [])) deselected = len(self.stats.get("deselected", []))