|
| 1 | +import time |
| 2 | +import numpy as np |
| 3 | +import plotly.graph_objs as go |
| 4 | +import plotly.io as pio |
| 5 | +import pytest |
| 6 | + |
| 7 | +np.random.seed(1) |
| 8 | +pio.renderers.default = "png" |
| 9 | + |
| 10 | + |
| 11 | +def test_performance_b64_scatter3d(): |
| 12 | + N = 50000 |
| 13 | + |
| 14 | + x = np.random.randn(N) |
| 15 | + y = np.random.randn(N).astype("float32") |
| 16 | + z = np.random.randint(size=N, low=0, high=256, dtype="uint8") |
| 17 | + c = np.random.randint(size=N, low=-10, high=10, dtype="int8") |
| 18 | + |
| 19 | + # Test the performance with lists |
| 20 | + x_list = x.tolist() |
| 21 | + y_list = y.tolist() |
| 22 | + z_list = z.tolist() |
| 23 | + c_list = c.tolist() |
| 24 | + list_start = time.time() |
| 25 | + fig = go.Figure( |
| 26 | + data=[ |
| 27 | + go.Scatter3d( |
| 28 | + x=x_list, |
| 29 | + y=y_list, |
| 30 | + z=z_list, |
| 31 | + marker=dict(color=c_list), |
| 32 | + mode="markers", |
| 33 | + opacity=0.2, |
| 34 | + ) |
| 35 | + ] |
| 36 | + ) |
| 37 | + fig.show(engine="kaleido") |
| 38 | + list_time_elapsed = time.time() - list_start |
| 39 | + |
| 40 | + # Test the performance with base64 arrays |
| 41 | + np_start = time.time() |
| 42 | + fig = go.Figure( |
| 43 | + data=[ |
| 44 | + go.Scatter3d( |
| 45 | + x=x, |
| 46 | + y=y, |
| 47 | + z=z, |
| 48 | + marker=dict(color=c), |
| 49 | + mode="markers", |
| 50 | + opacity=0.2, |
| 51 | + ) |
| 52 | + ] |
| 53 | + ) |
| 54 | + fig.show(engine="kaleido") |
| 55 | + |
| 56 | + np_time_elapsed = time.time() - np_start |
| 57 | + |
| 58 | + # np should be faster than lists |
| 59 | + assert (np_time_elapsed / list_time_elapsed) < 0.75 |
| 60 | + |
| 61 | + |
| 62 | +FLOAT_TEST_CASES = [ |
| 63 | + ("float32", 100000, 0.95), # dtype # difference threshold |
| 64 | + ("float64", 100000, 0.95), |
| 65 | +] |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("dtype, count, expected_size_difference", FLOAT_TEST_CASES) |
| 69 | +def test_performance_b64_float(dtype, count, expected_size_difference): |
| 70 | + np_arr_1 = np.random.random(count).astype(dtype) |
| 71 | + np_arr_2 = np.random.random(count).astype(dtype) |
| 72 | + list_1 = np_arr_1.tolist() |
| 73 | + list_2 = np_arr_2.tolist() |
| 74 | + |
| 75 | + # Test the performance of the base64 arrays |
| 76 | + np_start = time.time() |
| 77 | + fig = go.Figure(data=[go.Scattergl(x=np_arr_1, y=np_arr_2)]) |
| 78 | + fig.show(engine="kaleido") |
| 79 | + np_time_elapsed = time.time() - np_start |
| 80 | + |
| 81 | + # Test the performance of the normal lists |
| 82 | + list_start = time.time() |
| 83 | + fig = go.Figure(data=[go.Scattergl(x=list_1, y=list_2)]) |
| 84 | + fig.show(engine="kaleido") |
| 85 | + list_time_elapsed = time.time() - list_start |
| 86 | + |
| 87 | + # np should be faster than lists |
| 88 | + assert (np_time_elapsed / list_time_elapsed) < expected_size_difference |
| 89 | + |
| 90 | + |
| 91 | +INT_SIZE_PERFORMANCE_TEST_CASES = [ |
| 92 | + ("uint8", 256, 10500, 30000), |
| 93 | + ("uint32", 2**32, 10500, 100000), |
| 94 | +] |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize( |
| 98 | + "dtype, max_value, count, expected_size_difference", INT_SIZE_PERFORMANCE_TEST_CASES |
| 99 | +) |
| 100 | +def test_size_performance_b64_int(dtype, max_value, count, expected_size_difference): |
| 101 | + np_arr_1 = (np.random.random(count) * max_value).astype(dtype) |
| 102 | + np_arr_2 = (np.random.random(count) * max_value).astype(dtype) |
| 103 | + |
| 104 | + # Measure the size of figures with numpy arrays |
| 105 | + fig_np = go.Scatter(x=np_arr_1, y=np_arr_2) |
| 106 | + size_np = fig_np.to_json().__sizeof__() |
| 107 | + |
| 108 | + # Measure the size of the figure with normal python lists |
| 109 | + fig_list = go.Scatter(x=np_arr_1.tolist(), y=np_arr_2.tolist()) |
| 110 | + size_list = fig_list.to_json().__sizeof__() |
| 111 | + |
| 112 | + # np should be smaller than lists |
| 113 | + assert size_list - size_np > expected_size_difference |
0 commit comments