From 86c73553a50eedaa2df5dbf782ce6f2251e5cc9d Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Fri, 14 Jun 2024 21:38:55 -0400 Subject: [PATCH 01/11] test: add time-to-show --- tests/test_nd_viewer.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_nd_viewer.py b/tests/test_nd_viewer.py index 5ebff949..de9178e5 100644 --- a/tests/test_nd_viewer.py +++ b/tests/test_nd_viewer.py @@ -1,7 +1,10 @@ from __future__ import annotations import os +import subprocess import sys +import textwrap +import time from typing import TYPE_CHECKING, Any import dask.array as da @@ -57,3 +60,17 @@ def test_ndviewer(qtbot: QtBot, backend: str, monkeypatch: pytest.MonkeyPatch) - # wait until there are no running jobs, because the callbacks # in the futures hold a strong reference to the viewer qtbot.waitUntil(v._is_idle, timeout=3000) + + +def test_time_to_show() -> None: + script = """ + import ndv, numpy, unittest.mock + with unittest.mock.patch("qtpy.QtWidgets.QApplication.exec"): + ndv.imshow(numpy.empty((16, 16, 16))) + """ + cmd = ["python", "-c", textwrap.dedent(script)] + start = time.perf_counter() + subprocess.check_call(cmd) + end = time.perf_counter() + total = end - start + assert total < 1.5, "Viewer took too long to show up" From 015d46fec3a75528d4570ac075924a5da8a405d9 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 15 Jun 2024 17:12:56 -0400 Subject: [PATCH 02/11] change test --- tests/test_nd_viewer.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/test_nd_viewer.py b/tests/test_nd_viewer.py index de9178e5..0723b199 100644 --- a/tests/test_nd_viewer.py +++ b/tests/test_nd_viewer.py @@ -4,7 +4,6 @@ import subprocess import sys import textwrap -import time from typing import TYPE_CHECKING, Any import dask.array as da @@ -64,13 +63,16 @@ def test_ndviewer(qtbot: QtBot, backend: str, monkeypatch: pytest.MonkeyPatch) - def test_time_to_show() -> None: script = """ - import ndv, numpy, unittest.mock + import numpy, unittest.mock + from time import perf_counter + data = numpy.random.rand(16, 16, 16) with unittest.mock.patch("qtpy.QtWidgets.QApplication.exec"): - ndv.imshow(numpy.empty((16, 16, 16))) + start = perf_counter() + import ndv + ndv.imshow(data) + end = perf_counter() + print(end - start) """ - cmd = ["python", "-c", textwrap.dedent(script)] - start = time.perf_counter() - subprocess.check_call(cmd) - end = time.perf_counter() - total = end - start - assert total < 1.5, "Viewer took too long to show up" + output = subprocess.check_output(["python", "-c", textwrap.dedent(script)]) + time = float(output.decode().strip()) + assert time < 1, "Viewer took too long to show up" From 156b3f540fd9c4911682b13ac5777d5b98f68deb Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 15 Jun 2024 17:29:05 -0400 Subject: [PATCH 03/11] use codspeed --- .github/workflows/ci.yml | 18 ++++++++++++++++++ tests/test_benchmarks.py | 30 ++++++++++++++++++++++++++++++ tests/test_nd_viewer.py | 19 ------------------- 3 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 tests/test_benchmarks.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7f28129d..05ca9ae4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,6 +56,24 @@ jobs: secrets: codecov_token: ${{ secrets.CODECOV_TOKEN }} + benchmarks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: install + run: | + python -m pip install -e .[test] + python -m pip install pytest-codspeed + + - name: Run benchmarks + uses: CodSpeedHQ/action@v2 + with: + run: pytest --codspeed -v --color=yes + deploy: name: Deploy needs: test diff --git a/tests/test_benchmarks.py b/tests/test_benchmarks.py new file mode 100644 index 00000000..9efe70b6 --- /dev/null +++ b/tests/test_benchmarks.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +import sys +import unittest.mock +from typing import TYPE_CHECKING + +import numpy as np +import pytest + +if TYPE_CHECKING: + from pytest_benchmark.fixture import BenchmarkFixture + +if all(x not in {"--codspeed", "tests/test_benchmarks.py"} for x in sys.argv): + pytest.skip("use --benchmark to run benchmark", allow_module_level=True) + + +def _show_viewer(data: np.ndarray) -> None: + import ndv + + ndv.imshow(data) + + +def test_time_to_show(benchmark: BenchmarkFixture) -> None: + data = np.random.randint(0, 255, size=(10, 256, 256), dtype=np.uint8) + for k in list(sys.modules): + if k.startswith(("ndv", "PyQt5", "PySide2", "PySide6", "PyQt6", "superqt")): + del sys.modules[k] + + with unittest.mock.patch("qtpy.QtWidgets.QApplication.exec"): + benchmark.pedantic(_show_viewer, (data,), iterations=1, rounds=1) diff --git a/tests/test_nd_viewer.py b/tests/test_nd_viewer.py index 0723b199..5ebff949 100644 --- a/tests/test_nd_viewer.py +++ b/tests/test_nd_viewer.py @@ -1,9 +1,7 @@ from __future__ import annotations import os -import subprocess import sys -import textwrap from typing import TYPE_CHECKING, Any import dask.array as da @@ -59,20 +57,3 @@ def test_ndviewer(qtbot: QtBot, backend: str, monkeypatch: pytest.MonkeyPatch) - # wait until there are no running jobs, because the callbacks # in the futures hold a strong reference to the viewer qtbot.waitUntil(v._is_idle, timeout=3000) - - -def test_time_to_show() -> None: - script = """ - import numpy, unittest.mock - from time import perf_counter - data = numpy.random.rand(16, 16, 16) - with unittest.mock.patch("qtpy.QtWidgets.QApplication.exec"): - start = perf_counter() - import ndv - ndv.imshow(data) - end = perf_counter() - print(end - start) - """ - output = subprocess.check_output(["python", "-c", textwrap.dedent(script)]) - time = float(output.decode().strip()) - assert time < 1, "Viewer took too long to show up" From fb4989e8a70bfba9175a8778a467881f8416b0cc Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 15 Jun 2024 17:35:11 -0400 Subject: [PATCH 04/11] fix --- .github/workflows/ci.yml | 3 +-- pyproject.toml | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 05ca9ae4..980623e9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -66,8 +66,7 @@ jobs: - name: install run: | - python -m pip install -e .[test] - python -m pip install pytest-codspeed + python -m pip install -e .[benchmarks] - name: Run benchmarks uses: CodSpeedHQ/action@v2 diff --git a/pyproject.toml b/pyproject.toml index d575fd4d..ca2bea88 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,6 +60,7 @@ test = [ "pytest-qt", "pytest", ] +benchmarks = ["ndv[vispy,pyqt6]", "pytest", "pytest-codspeed"] dev = ["ipython", "mypy", "pdbpp", "pre-commit", "rich", "ruff"] [project.urls] From 2cf8f34af060ff1f84d20a215200184fa508de4f Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 15 Jun 2024 17:36:53 -0400 Subject: [PATCH 05/11] fix extra --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ca2bea88..b136967e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,7 @@ test = [ "pytest-qt", "pytest", ] -benchmarks = ["ndv[vispy,pyqt6]", "pytest", "pytest-codspeed"] +benchmarks = ["ndv[vispy,pyqt]", "pytest", "pytest-codspeed"] dev = ["ipython", "mypy", "pdbpp", "pre-commit", "rich", "ruff"] [project.urls] From abd1429132dc1f3a25aea5239b66a9f28b864678 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 15 Jun 2024 17:40:30 -0400 Subject: [PATCH 06/11] add qt libs --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 980623e9..ac983395 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,6 +60,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: tlambert03/setup-qt-libs@v1 - uses: actions/setup-python@v5 with: python-version: "3.12" From 8688d4834bc8d31e17a04d0fdfa9f7b4fede3780 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 15 Jun 2024 17:44:27 -0400 Subject: [PATCH 07/11] change agin --- .github/workflows/ci.yml | 3 ++- pyproject.toml | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac983395..e5801cb5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,7 +67,8 @@ jobs: - name: install run: | - python -m pip install -e .[benchmarks] + python -m pip install .[test,pyqt] + python -m pip install pytest-codspeed - name: Run benchmarks uses: CodSpeedHQ/action@v2 diff --git a/pyproject.toml b/pyproject.toml index b136967e..d575fd4d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,6 @@ test = [ "pytest-qt", "pytest", ] -benchmarks = ["ndv[vispy,pyqt]", "pytest", "pytest-codspeed"] dev = ["ipython", "mypy", "pdbpp", "pre-commit", "rich", "ruff"] [project.urls] From 17e62e44c022ab31f2a336aaae38eb0703df5299 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 15 Jun 2024 17:53:47 -0400 Subject: [PATCH 08/11] try different patch --- tests/test_benchmarks.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_benchmarks.py b/tests/test_benchmarks.py index 9efe70b6..27b494fd 100644 --- a/tests/test_benchmarks.py +++ b/tests/test_benchmarks.py @@ -2,7 +2,7 @@ import sys import unittest.mock -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import numpy as np import pytest @@ -20,11 +20,10 @@ def _show_viewer(data: np.ndarray) -> None: ndv.imshow(data) -def test_time_to_show(benchmark: BenchmarkFixture) -> None: +def test_time_to_show(benchmark: BenchmarkFixture, qapp: Any) -> None: data = np.random.randint(0, 255, size=(10, 256, 256), dtype=np.uint8) for k in list(sys.modules): - if k.startswith(("ndv", "PyQt5", "PySide2", "PySide6", "PyQt6", "superqt")): + if k.startswith(("ndv", "superqt")): del sys.modules[k] - - with unittest.mock.patch("qtpy.QtWidgets.QApplication.exec"): + with unittest.mock.patch.object(qapp, "exec"): benchmark.pedantic(_show_viewer, (data,), iterations=1, rounds=1) From ba9b2e0dbe0751467d77f8c6130edec4ff86702a Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 15 Jun 2024 17:57:03 -0400 Subject: [PATCH 09/11] try pyside --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e5801cb5..07df6ada 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,7 +67,7 @@ jobs: - name: install run: | - python -m pip install .[test,pyqt] + python -m pip install .[test,pyside] python -m pip install pytest-codspeed - name: Run benchmarks From 9fb4daa1f6d995158d92fc840a8f33a92bcc21ed Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sun, 19 Jan 2025 12:26:00 -0500 Subject: [PATCH 10/11] use v3 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1d8a938b..4e996dbd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -157,7 +157,7 @@ jobs: python -m pip install pytest-codspeed - name: Run benchmarks - uses: CodSpeedHQ/action@v2 + uses: CodSpeedHQ/action@v3 with: run: pytest --codspeed -v --color=yes From c8a8838963335b0bead0807f74055f30a068de6f Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sun, 19 Jan 2025 12:29:05 -0500 Subject: [PATCH 11/11] update test --- .github/workflows/ci.yml | 2 +- tests/test_benchmarks.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4e996dbd..ab5ce1d5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -153,7 +153,7 @@ jobs: - name: install run: | - python -m pip install .[test,pyside] + python -m pip install .[test,pyqt,vispy,pygfx] python -m pip install pytest-codspeed - name: Run benchmarks diff --git a/tests/test_benchmarks.py b/tests/test_benchmarks.py index 27b494fd..7c121f71 100644 --- a/tests/test_benchmarks.py +++ b/tests/test_benchmarks.py @@ -23,7 +23,7 @@ def _show_viewer(data: np.ndarray) -> None: def test_time_to_show(benchmark: BenchmarkFixture, qapp: Any) -> None: data = np.random.randint(0, 255, size=(10, 256, 256), dtype=np.uint8) for k in list(sys.modules): - if k.startswith(("ndv", "superqt")): + if k.startswith(("ndv", "superqt", "vispy", "pygfx", "wgpu", "PyQt", "PySide")): del sys.modules[k] with unittest.mock.patch.object(qapp, "exec"): benchmark.pedantic(_show_viewer, (data,), iterations=1, rounds=1)