Skip to content

Commit 8a051a3

Browse files
committed
Update tests to remove conversion to base64 before passing numpy arrays
1 parent 024f3c1 commit 8a051a3

File tree

2 files changed

+42
-55
lines changed

2 files changed

+42
-55
lines changed
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import time
22
import numpy as np
33
import plotly.graph_objs as go
4-
from plotly.tests.b64 import b64
54

65
np.random.seed(1)
76

87

9-
def test_performance_scatter3d():
8+
def test_performance_b64_scatter3d():
109
N = 10000
1110

1211
x = np.random.randn(N)
@@ -31,50 +30,50 @@ def test_performance_scatter3d():
3130
list_time_elapsed = time.time() - list_start
3231

3332
# Test the performance with base64 arrays
34-
b64_start = time.time()
33+
np_start = time.time()
3534
fig = go.Scatter3d(
36-
x=b64(x),
37-
y=b64(y),
38-
z=b64(z),
39-
marker=dict(color=b64(c)),
35+
x=x,
36+
y=y,
37+
z=z,
38+
marker=dict(color=c),
4039
mode="markers",
4140
opacity=0.2,
4241
)
43-
b64_time_elapsed = time.time() - b64_start
42+
np_time_elapsed = time.time() - np_start
4443

45-
# b64 should be faster than raw
46-
assert (b64_time_elapsed / list_time_elapsed) < 0.75
44+
# np should be faster than raw
45+
assert (np_time_elapsed / list_time_elapsed) < 0.75
4746

4847

4948
def test_performance_b64_float64():
5049
np_arr_1 = np.random.random(10000)
5150
np_arr_2 = np.random.random(10000)
5251

5352
# Test the performance of the base64 arrays
54-
b64_start = time.time()
55-
fig = go.Scatter(x=b64(np_arr_1), y=b64(np_arr_2))
56-
b64_time_elapsed = time.time() - b64_start
53+
np_start = time.time()
54+
fig = go.Scatter(x=np_arr_1, y=np_arr_2)
55+
np_time_elapsed = time.time() - np_start
5756

5857
# Test the performance of the raw arrays
5958
list_start = time.time()
6059
fig = go.Scatter(x=np_arr_1.tolist(), y=np_arr_2.tolist())
6160
list_time_elapsed = time.time() - list_start
6261

63-
# b64 should be faster than raw
64-
assert (b64_time_elapsed / list_time_elapsed) < 0.75
62+
# np should be faster than raw
63+
assert (np_time_elapsed / list_time_elapsed) < 0.75
6564

6665

6766
def test_size_performance_b64_uint8():
6867
np_arr_1 = (np.random.random(100000) * 256).astype("uint8")
6968
np_arr_2 = (np.random.random(100000) * 256).astype("uint8")
7069

71-
# Measure the size of figures with b64 arrays
72-
fig_b64 = go.Scatter(x=b64(np_arr_1), y=b64(np_arr_2))
73-
size_b64 = fig_b64.to_json().__sizeof__()
70+
# Measure the size of figures with numpy arrays
71+
fig_np = go.Scatter(x=np_arr_1, y=np_arr_2)
72+
size_np = fig_np.to_json().__sizeof__()
7473

7574
# Measure the size of the figure with normal python lists
7675
fig_list = go.Scatter(x=np_arr_1.tolist(), y=np_arr_2.tolist())
7776
size_list = fig_list.to_json().__sizeof__()
7877

79-
# b64 should be smaller than raw
80-
assert size_b64 / size_list < 0.75
78+
# np should be smaller than raw
79+
assert size_list - size_np > 1000

packages/python/plotly/plotly/tests/test_optional/test_px/test_px_input.py

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -141,61 +141,49 @@ def test_name_heuristics():
141141

142142

143143
def test_performance_b64():
144-
rand_arr_1 = np.random.random(1000000)
145-
rand_arr_2 = np.random.random(1000000)
146-
raw_arr_1 = rand_arr_1.tolist()
147-
raw_arr_2 = rand_arr_2.tolist()
148-
b64_arr_1 = b64(rand_arr_1)
149-
b64_arr_2 = b64(rand_arr_2)
144+
np_arr_1 = np.random.random(1000000)
145+
np_arr_2 = np.random.random(1000000)
150146

151147
# Test the performance of the base64 arrays
152-
b64_start = time.time()
153-
fig = px.scatter(x=b64_arr_1, y=b64_arr_2, width=800, height=800)
148+
np_arr_start = time.time()
149+
fig = px.scatter(x=np_arr_1, y=np_arr_2, width=800, height=800)
154150
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
155-
b64_time_elapsed = time.time() - b64_start
151+
np_arr_time_elapsed = time.time() - np_arr_start
156152

157153
# Test the performance of the raw arrays
158-
raw_start = time.time()
159-
fig = px.scatter(x=raw_arr_1, y=raw_arr_2, width=800, height=800)
154+
list_start = time.time()
155+
fig = px.scatter(x=np_arr_1.tolist(), y=np_arr_2.tolist(), width=800, height=800)
160156
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
161-
raw_time_elapsed = time.time() - raw_start
157+
list_time_elapsed = time.time() - list_start
162158

163159
# b64 should be faster than raw
164-
assert (b64_time_elapsed / raw_time_elapsed) < 0.7
160+
assert (np_arr_time_elapsed / list_time_elapsed) < 0.7
165161

166162

167163
def test_size_performance_b64_uint8():
168-
rand_arr_1 = np.random.randint(0, high=254, size=100000, dtype="uint8")
169-
rand_arr_2 = np.random.randint(0, high=254, size=100000, dtype="uint8")
170-
raw_arr_1 = rand_arr_1.tolist()
171-
raw_arr_2 = rand_arr_2.tolist()
172-
b64_arr_1 = b64(rand_arr_1)
173-
b64_arr_2 = b64(rand_arr_2)
164+
np_arr_1 = np.random.randint(0, high=254, size=100000, dtype="uint8")
165+
np_arr_2 = np.random.randint(0, high=254, size=100000, dtype="uint8")
174166

175167
# Compare the size of figures with b64 arrays and raw arrays
176-
fig_b64 = px.scatter(x=b64_arr_1, y=b64_arr_2)
177-
size_b64 = fig_b64.to_json().__sizeof__()
178-
fig_raw = px.scatter(x=raw_arr_1, y=raw_arr_2)
179-
size_raw = fig_raw.to_json().__sizeof__()
168+
fig_np_arr = px.scatter(x=np_arr_1, y=np_arr_2)
169+
size_np_arr = fig_np_arr.to_json().__sizeof__()
170+
fig_list = px.scatter(x=np_arr_1.tolist(), y=np_arr_2.tolist())
171+
size_list = fig_list.to_json().__sizeof__()
180172

181-
assert size_b64 / size_raw < 0.85
173+
assert size_list - size_np_arr > 250000
182174

183175

184176
def test_size_performance_b64_float32():
185-
rand_arr_1 = np.random.random(100000).astype("float32")
186-
rand_arr_2 = np.random.random(100000).astype("float32")
187-
raw_arr_1 = rand_arr_1.tolist()
188-
raw_arr_2 = rand_arr_2.tolist()
189-
b64_arr_1 = b64(rand_arr_1)
190-
b64_arr_2 = b64(rand_arr_2)
177+
np_arr_1 = np.random.random(100000).astype("float32")
178+
np_arr_2 = np.random.random(100000).astype("float32")
191179

192180
# Compare the size of figures with b64 arrays and raw arrays
193-
fig_b64 = px.scatter(x=b64_arr_1, y=b64_arr_2)
194-
size_b64 = fig_b64.to_json().__sizeof__()
195-
fig_raw = px.scatter(x=raw_arr_1, y=raw_arr_2)
196-
size_raw = fig_raw.to_json().__sizeof__()
181+
fig_np_arr = px.scatter(x=np_arr_1, y=np_arr_2)
182+
size_np_arr = fig_np_arr.to_json().__sizeof__()
183+
fig_list = px.scatter(x=np_arr_1.tolist(), y=np_arr_2.tolist())
184+
size_list = fig_list.to_json().__sizeof__()
197185

198-
assert size_b64 / size_raw < 0.85
186+
assert size_list - size_np_arr > 250000
199187

200188

201189
def test_repeated_name():

0 commit comments

Comments
 (0)