Skip to content

Commit a666211

Browse files
committed
tests:Add single chunk write test for sharding
1 parent 1cad983 commit a666211

File tree

1 file changed

+13
-50
lines changed

1 file changed

+13
-50
lines changed

tests/benchmarks/test_indexing.py

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -221,67 +221,29 @@ def compute_morton_order() -> None:
221221
benchmark(compute_morton_order)
222222

223223

224-
# Benchmark for Morton order in write path
225-
# Morton order is used when encoding shards, so writes should show improvement
226224
@pytest.mark.parametrize("store", ["memory"], indirect=["store"])
227225
@pytest.mark.parametrize("shards", large_morton_shards, ids=str)
228-
def test_sharded_morton_write_full(
226+
def test_sharded_morton_write_single_chunk(
229227
store: Store,
230228
shards: tuple[int, ...],
231229
benchmark: BenchmarkFixture,
232230
) -> None:
233-
"""Benchmark writing a full shard with Morton order.
231+
"""Benchmark writing a single chunk to a large shard.
234232
235-
Morton order is used in the write/encode path of the sharding codec.
236-
This benchmark writes to a full shard with cache clearing to measure
237-
the Morton optimization impact on writes.
238-
"""
239-
import numpy as np
240-
241-
from zarr.core.indexing import _morton_order
242-
243-
shape = tuple(s * 2 for s in shards)
244-
chunks = (1,) * 3
245-
246-
data = create_array(
247-
store=store,
248-
shape=shape,
249-
dtype="uint8",
250-
chunks=chunks,
251-
shards=shards,
252-
compressors=None,
253-
filters=None,
254-
fill_value=0,
255-
)
256-
257-
# Pre-create write data
258-
write_data = np.ones(shards, dtype="uint8")
259-
indexer = (slice(shards[0]),) * 3
233+
This is the clearest end-to-end demonstration of Morton order optimization.
234+
Writing a single chunk to a shard with 32^3 = 32768 chunks requires
235+
computing the full Morton order, but minimizes I/O overhead.
260236
261-
def write_with_cache_clear() -> None:
262-
_morton_order.cache_clear()
263-
data[indexer] = write_data
264-
265-
benchmark(write_with_cache_clear)
266-
267-
268-
@pytest.mark.parametrize("store", ["memory"], indirect=["store"])
269-
@pytest.mark.parametrize("shards", morton_iter_shapes, ids=str)
270-
def test_sharded_morton_write(
271-
store: Store,
272-
shards: tuple[int, ...],
273-
benchmark: BenchmarkFixture,
274-
) -> None:
275-
"""Benchmark writing to sharded arrays with various shard sizes.
276-
277-
Tests Morton order impact on writes across different shard sizes.
237+
Expected improvement: ~160ms (matching Morton computation speedup of ~178ms).
238+
The Morton order cache is cleared before each iteration.
278239
"""
279240
import numpy as np
280241

281242
from zarr.core.indexing import _morton_order
282243

283-
shape = tuple(s * 2 for s in shards)
284-
chunks = (1,) * len(shards)
244+
# 1x1x1 chunks means chunks_per_shard equals shard shape
245+
shape = tuple(s * 2 for s in shards) # 2 shards per dimension
246+
chunks = (1,) * 3 # 1x1x1 chunks: chunks_per_shard = shards
285247

286248
data = create_array(
287249
store=store,
@@ -294,8 +256,9 @@ def test_sharded_morton_write(
294256
fill_value=0,
295257
)
296258

297-
write_data = np.ones(shards, dtype="uint8")
298-
indexer = tuple(slice(s) for s in shards)
259+
# Write data for a single chunk
260+
write_data = np.ones((1, 1, 1), dtype="uint8")
261+
indexer = (slice(1), slice(1), slice(1))
299262

300263
def write_with_cache_clear() -> None:
301264
_morton_order.cache_clear()

0 commit comments

Comments
 (0)