-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtest_benchmark_writes.py
More file actions
273 lines (240 loc) · 8.09 KB
/
test_benchmark_writes.py
File metadata and controls
273 lines (240 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python3
# pytest -s test_benchmark_writes.py::test_write_chunks
import numpy as np
import pytest
import zarr
from benchmarks import lib
from benchmarks.helpers import get_splitting_config, repo_config_with
from benchmarks.tasks import Executor, write
from icechunk import (
Repository,
RepositoryConfig,
Session,
VirtualChunkSpec,
local_filesystem_storage,
)
NUM_CHUNK_REFS = 10_000
NUM_VIRTUAL_CHUNK_REFS = 100_000
pytestmark = pytest.mark.write_benchmark
@pytest.fixture(
params=[
pytest.param(None, id="no-splitting"),
pytest.param(10000, id="split-size-10_000"),
]
)
def splitting(request):
if request.param is None:
return None
else:
try:
return get_splitting_config(split_size=request.param)
except ImportError:
pytest.skip("splitting not supported on this version")
# FIXME: figure out a reasonable default
@pytest.mark.parametrize(
"executor",
[
Executor.threads,
pytest.param(
Executor.processes, marks=pytest.mark.skipif(True, reason="hangs on Coiled")
),
],
)
def test_write_chunks_with_tasks(synth_write_dataset, benchmark, executor):
"""
Writes chunks orchestrated using 'bare' tasks and executed using
either a ThreadPoolExecutor or ProcessPoolExecutor.
Importantly this benchmarks captures timings PER write task, summarizes them,
and then records them in the .json file.
"""
timer = benchmark.pedantic(
# TODO: parametrize over some of these
write,
kwargs=dict(
storage=synth_write_dataset.storage,
num_arrays=synth_write_dataset.num_arrays,
shape=synth_write_dataset.shape,
chunks=synth_write_dataset.chunks,
task_nchunks=1,
executor=executor,
workers=4,
threads_per_worker=None,
),
iterations=1,
rounds=1,
)
diags = timer.as_dict()
diags["write_task_raw"] = diags["write_task"]
diags["write_task"] = lib.stats(np.array(diags["write_task"]))
benchmark.extra_info["data"] = diags
def test_write_simple_1d(benchmark, simple_write_dataset):
"""Simple write benchmarks. Shows 2-3X slower on GCS compared to S3"""
dataset = simple_write_dataset
repo = dataset.create()
session = repo.writable_session(branch="main")
store = session.store
group = zarr.open_group(store, zarr_format=3)
group.create_array(
"array",
shape=dataset.shape,
chunks=dataset.chunks,
dtype="int8",
fill_value=0,
compressors=None,
)
session.commit("initialize")
def write_task():
session = repo.writable_session(branch="main")
array = zarr.open_array(session.store, path="array")
array[:] = 42
session.commit("foo")
benchmark(write_task)
@pytest.mark.benchmark(group="refs-write")
@pytest.mark.parametrize("commit", [True, False])
@pytest.mark.parametrize(
"repo_config",
[
pytest.param(repo_config_with(), id="default-inlined"),
pytest.param(repo_config_with(inline_chunk_threshold_bytes=0), id="not-inlined"),
],
)
def test_write_many_chunk_refs(
commit: bool, benchmark, repo_config: RepositoryConfig, tmpdir
) -> None:
"""Benchmarking the writing of many chunk refs, inlined and not; committed and not;"""
# 1. benchmark only the writes with no commit
# 2. one with commit
def write_chunk_refs(repo) -> None:
session = repo.writable_session("main")
array = zarr.open_array(path="array", store=session.store)
# TODO: configurable?
with zarr.config.set({"async.concurrency": 64}):
array[:] = -1
if commit:
session.commit("written!")
assert repo_config is not None
repo = Repository.create(storage=local_filesystem_storage(tmpdir), config=repo_config)
session = repo.writable_session("main")
group = zarr.group(session.store)
kwargs = dict(
name="array",
shape=(NUM_CHUNK_REFS,),
chunks=(1,),
dtype=np.int8,
dimension_names=("t",),
)
try:
group.create_array(**kwargs)
except AttributeError:
group.array(**kwargs)
session.commit("initialized")
benchmark(write_chunk_refs, repo)
@pytest.mark.benchmark(group="refs-write")
def test_set_many_virtual_chunk_refs(benchmark, repo) -> None:
"""Benchmark the setting of many virtual chunk refs."""
session = repo.writable_session("main")
store = session.store
group = zarr.group(store)
kwargs = dict(
name="array",
shape=(NUM_VIRTUAL_CHUNK_REFS,),
chunks=(1,),
dtype=np.int8,
dimension_names=("t",),
overwrite=True,
)
try:
group.create_array(**kwargs)
except AttributeError:
group.array(**kwargs)
session.commit("initialized")
@benchmark
def write():
# always create a new session so we have a clean changelog
session = repo.writable_session("main")
store = session.store
for i in range(NUM_VIRTUAL_CHUNK_REFS):
store.set_virtual_ref(
f"array/c/{i}", location=f"s3://foo/bar/{i}.nc", offset=0, length=1
)
@pytest.mark.benchmark(group="refs-write")
def test_write_split_manifest_refs_full_rewrite(
benchmark, splitting, large_write_dataset
) -> None:
dataset = large_write_dataset
config = repo_config_with(splitting=splitting)
assert config is not None
if hasattr(config.manifest, "splitting"):
assert config.manifest.splitting == splitting
repo = dataset.create(config=config)
session = repo.writable_session(branch="main")
store = session.store
group = zarr.open_group(store, zarr_format=3)
group.create_array(
"array",
shape=dataset.shape,
chunks=dataset.chunks,
dtype="int8",
fill_value=0,
compressors=None,
)
session.commit("initialize")
def write_refs() -> Session:
session = repo.writable_session(branch="main")
num_chunks = dataset.shape[0] // dataset.chunks[0]
chunks = [
VirtualChunkSpec(
index=[i], location=f"s3://foo/bar/{i}.nc", offset=0, length=1
)
for i in range(num_chunks)
]
session.store.set_virtual_refs("array", chunks)
# (args, kwargs)
return ((session,), {})
def commit(session_from_setup):
session_from_setup.commit("wrote refs")
benchmark.pedantic(commit, setup=write_refs, iterations=1, rounds=10)
@pytest.mark.benchmark(group="refs-write")
def test_write_split_manifest_refs_append(
benchmark, splitting, large_write_dataset
) -> None:
dataset = large_write_dataset
config = repo_config_with(splitting=splitting)
assert config is not None
if hasattr(config.manifest, "splitting"):
assert config.manifest.splitting == splitting
repo = dataset.create(config=config)
session = repo.writable_session(branch="main")
store = session.store
group = zarr.open_group(store, zarr_format=3)
group.create_array(
"array",
shape=dataset.shape,
chunks=dataset.chunks,
dtype="int8",
fill_value=0,
compressors=None,
)
session.commit("initialize")
# yuck, but I'm abusing `rounds` to do a loop and time _only_ the commit.
global counter
counter = 0
rounds = 10
num_chunks = dataset.shape[0] // dataset.chunks[0]
batch_size = num_chunks // rounds
def write_refs() -> Session:
global counter
session = repo.writable_session(branch="main")
chunks = [
VirtualChunkSpec(
index=[i], location=f"s3://foo/bar/{i}.nc", offset=0, length=1
)
for i in range(counter * batch_size, counter * batch_size + batch_size)
]
counter += 1
session.store.set_virtual_refs("array", chunks)
# (args, kwargs)
return ((session,), {})
def commit(session_from_setup):
session_from_setup.commit("wrote refs")
benchmark.pedantic(commit, setup=write_refs, iterations=1, rounds=rounds)