|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see plotpy/LICENSE for details) |
| 5 | + |
| 6 | +"""Curve plotting test with large datasets""" |
| 7 | + |
| 8 | +# guitest: show |
| 9 | + |
| 10 | +import timeit |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import pytest |
| 14 | +from guidata.qthelpers import qt_app_context |
| 15 | +from qtpy import QtWidgets as QW |
| 16 | + |
| 17 | +from plotpy.builder import make |
| 18 | +from plotpy.tests import vistools as ptv |
| 19 | + |
| 20 | + |
| 21 | +def __create_curve(npts: int, linewidth: float): |
| 22 | + """Create a curve with random data""" |
| 23 | + x = np.linspace(-10, 10, npts) |
| 24 | + y = np.random.normal(size=npts) |
| 25 | + curve = make.curve(x, y, color="g", linewidth=linewidth) |
| 26 | + return curve |
| 27 | + |
| 28 | + |
| 29 | +def __show_curve(curve, benchmark: bool = False): |
| 30 | + """Show the curve in a plot window""" |
| 31 | + with qt_app_context(exec_loop=not benchmark): |
| 32 | + _win = ptv.show_items( |
| 33 | + [curve], |
| 34 | + wintitle=test_curve_rendering_performance.__doc__, |
| 35 | + title="Curves", |
| 36 | + plot_type="curve", |
| 37 | + disable_readonly_for_items=False, |
| 38 | + ) |
| 39 | + if benchmark: |
| 40 | + QW.QApplication.processEvents() |
| 41 | + return _win |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.skip(reason="Not relevant in automated test suite") |
| 45 | +def test_interactive_curve_rendering(npts: int = 500000, linewidth: float = 1.0): |
| 46 | + """Interactive test for curve rendering with large datasets""" |
| 47 | + curve = __create_curve(npts, linewidth) |
| 48 | + _win = __show_curve(curve, benchmark=False) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.skip(reason="Not relevant in automated test suite") |
| 52 | +def test_curve_rendering_performance(npts: int = 500000, linewidth: float = 1.0): |
| 53 | + """Test curve rendering performance with large datasets""" |
| 54 | + curve = __create_curve(npts, linewidth) |
| 55 | + |
| 56 | + # Show once before benchmarking |
| 57 | + _win = __show_curve(curve, benchmark=True) |
| 58 | + |
| 59 | + # Benchmark with multiple runs |
| 60 | + n_runs = 5 |
| 61 | + times = [] |
| 62 | + |
| 63 | + print("Benchmarking in progress", end="") |
| 64 | + for i in range(n_runs): |
| 65 | + start_time = timeit.default_timer() |
| 66 | + _win = __show_curve(curve, benchmark=True) |
| 67 | + elapsed_time = timeit.default_timer() - start_time |
| 68 | + times.append(elapsed_time) |
| 69 | + print(".", end="", flush=True) |
| 70 | + print(" done.") |
| 71 | + |
| 72 | + # Display statistics |
| 73 | + print( |
| 74 | + f"Results ({n_runs} runs): {np.mean(times):.4f} ± {np.std(times):.4f} seconds" |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + # test_interactive_curve_rendering(10000, 2.0, False) |
| 80 | + # Test with different linewidths and antialiasing settings |
| 81 | + print("\n" + "=" * 70) |
| 82 | + print("Curve Rendering Performance Benchmark") |
| 83 | + print("=" * 70) |
| 84 | + |
| 85 | + print("\n--- Test 1: linewidth=1.0 ---") |
| 86 | + test_curve_rendering_performance(10000, 1.0) |
| 87 | + |
| 88 | + print("\n--- Test 2: linewidth=2.0 ---") |
| 89 | + test_curve_rendering_performance(10000, 2.0) |
0 commit comments