|
| 1 | +""" |
| 2 | +Tests specific to the lines module. |
| 3 | +""" |
| 4 | + |
| 5 | +from nose.tools import assert_true |
| 6 | +from timeit import repeat |
| 7 | +import numpy as np |
| 8 | +import matplotlib as mpl |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +from matplotlib.testing.decorators import cleanup |
| 11 | + |
| 12 | +@cleanup |
| 13 | +def test_invisible_Line_rendering(): |
| 14 | + """ |
| 15 | + Github issue #1256 identified a bug in Line.draw method |
| 16 | + |
| 17 | + Despite visibility attribute set to False, the draw method was not |
| 18 | + returning early enough and some pre-rendering code was executed |
| 19 | + though not necessary. |
| 20 | +
|
| 21 | + Consequence was an excessive draw time for invisible Line instances |
| 22 | + holding a large number of points (Npts> 10**6) |
| 23 | + """ |
| 24 | + # Creates big x and y data: |
| 25 | + N = 10**7 |
| 26 | + x = np.linspace(0,1,N) |
| 27 | + y = np.random.normal(size=N) |
| 28 | + |
| 29 | + # Create a plot figure: |
| 30 | + fig = plt.figure() |
| 31 | + ax = plt.subplot(111) |
| 32 | + |
| 33 | + # Create a "big" Line instance: |
| 34 | + l = mpl.lines.Line2D(x,y) |
| 35 | + l.set_visible(False) |
| 36 | + # but don't add it to the Axis instance `ax` |
| 37 | + |
| 38 | + # [here Interactive panning and zooming is pretty responsive] |
| 39 | + # Time the canvas drawing: |
| 40 | + t_no_line = min(repeat(fig.canvas.draw, number=1, repeat=3)) |
| 41 | + # (gives about 25 ms) |
| 42 | + |
| 43 | + # Add the big invisible Line: |
| 44 | + ax.add_line(l) |
| 45 | + |
| 46 | + # [Now interactive panning and zooming is very slow] |
| 47 | + # Time the canvas drawing: |
| 48 | + t_unvisible_line = min(repeat(fig.canvas.draw, number=1, repeat=3)) |
| 49 | + # gives about 290 ms for N = 10**7 pts |
| 50 | + |
| 51 | + slowdown_factor = (t_unvisible_line/t_no_line) |
| 52 | + slowdown_threshold = 2 # trying to avoid false positive failures |
| 53 | + assert_true(slowdown_factor < slowdown_threshold) |
0 commit comments