|
| 1 | +""" |
| 2 | +Benchmarks for end-to-end read/write performance of Zarr |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +from tests.benchmarks.common import Layout |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from pytest_benchmark.fixture import BenchmarkFixture |
| 13 | + |
| 14 | + from zarr.abc.store import Store |
| 15 | + from zarr.core.common import NamedConfig |
| 16 | +from operator import getitem, setitem |
| 17 | +from typing import Any, Literal |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from zarr import create_array |
| 22 | + |
| 23 | +CompressorName = Literal["gzip"] | None |
| 24 | + |
| 25 | +compressors: dict[CompressorName, NamedConfig[Any, Any] | None] = { |
| 26 | + None: None, |
| 27 | + "gzip": {"name": "gzip", "configuration": {"level": 1}}, |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +layouts: tuple[Layout, ...] = ( |
| 32 | + # No shards, just 1000 chunks |
| 33 | + Layout(shape=(1_000_000,), chunks=(1000,), shards=None), |
| 34 | + # 1:1 chunk:shard shape, should measure overhead of sharding |
| 35 | + Layout(shape=(1_000_000,), chunks=(1000,), shards=(1000,)), |
| 36 | + # One shard with all the chunks, should measure overhead of handling inner shard chunks |
| 37 | + Layout(shape=(1_000_000,), chunks=(100,), shards=(10000 * 100,)), |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("compression_name", [None, "gzip"]) |
| 42 | +@pytest.mark.parametrize("layout", layouts, ids=str) |
| 43 | +@pytest.mark.parametrize("store", ["memory", "local"], indirect=["store"]) |
| 44 | +def test_write_array( |
| 45 | + store: Store, layout: Layout, compression_name: CompressorName, benchmark: BenchmarkFixture |
| 46 | +) -> None: |
| 47 | + """ |
| 48 | + Test the time required to fill an array with a single value |
| 49 | + """ |
| 50 | + arr = create_array( |
| 51 | + store, |
| 52 | + dtype="uint8", |
| 53 | + shape=layout.shape, |
| 54 | + chunks=layout.chunks, |
| 55 | + shards=layout.shards, |
| 56 | + compressors=compressors[compression_name], # type: ignore[arg-type] |
| 57 | + fill_value=0, |
| 58 | + ) |
| 59 | + |
| 60 | + benchmark(setitem, arr, Ellipsis, 1) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize("compression_name", [None, "gzip"]) |
| 64 | +@pytest.mark.parametrize("layout", layouts, ids=str) |
| 65 | +@pytest.mark.parametrize("store", ["memory", "local"], indirect=["store"]) |
| 66 | +def test_read_array( |
| 67 | + store: Store, layout: Layout, compression_name: CompressorName, benchmark: BenchmarkFixture |
| 68 | +) -> None: |
| 69 | + """ |
| 70 | + Test the time required to fill an array with a single value |
| 71 | + """ |
| 72 | + arr = create_array( |
| 73 | + store, |
| 74 | + dtype="uint8", |
| 75 | + shape=layout.shape, |
| 76 | + chunks=layout.chunks, |
| 77 | + shards=layout.shards, |
| 78 | + compressors=compressors[compression_name], # type: ignore[arg-type] |
| 79 | + fill_value=0, |
| 80 | + ) |
| 81 | + arr[:] = 1 |
| 82 | + benchmark(getitem, arr, Ellipsis) |
0 commit comments