This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
forked from AllenCellModeling/aicsimageio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
305 lines (248 loc) · 9.38 KB
/
benchmark.py
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import json
import logging
import platform
import sys
import time
import traceback
from datetime import datetime
from pathlib import Path
from typing import Callable, List
import czifile
import imageio
import numpy as np
import psutil
import tifffile
from dask_jobqueue import SLURMCluster
from distributed import Client
from quilt3 import Package
from tqdm import tqdm
import aicsimageio
###############################################################################
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)4s: %(module)s:%(lineno)4s %(asctime)s] %(message)s",
)
log = logging.getLogger(__name__)
###############################################################################
CLUSTER_CONFIGS = [
{"name": "small-local-cluster-replica", "per_worker_cores": 2, "workers": 4},
{"name": "large-local-cluster-replica", "per_worker_cores": 2, "workers": 16},
{"name": "small-worker-distributed-cluster", "per_worker_cores": 1, "workers": 32},
{
"name": "many-small-worker-distributed-cluster",
"per_worker_cores": 1,
"workers": 128,
},
{
"name": "many-standard-worker-distributed-cluster",
"per_worker_cores": 4,
"workers": 32,
},
]
###############################################################################
# Args
class Args(argparse.Namespace):
def __init__(self):
self.__parse()
def __parse(self):
# Setup parser
p = argparse.ArgumentParser(
prog="benchmark_aicsimageio",
description=(
"Run read time benchmarks for aicsimageio against other common image "
"readers. The benchmark dataset can be downloaded using the "
"download_test_resources script with the specific hash: "
"5e665ed66c1b373a84002227044c7a12a2ecc506b84a730442a5ed798428e26a"
),
)
# Arguments
p.add_argument(
"--save_path",
default="benchmark/results.json",
type=Path,
help="Path to save the generated benchmark JSON file.",
)
p.add_argument(
"--upload",
action="store_true",
help="Should the results be uploaded to Quilt.",
)
p.add_argument(
"--debug",
action="store_true",
help="Show traceback if the script were to fail.",
)
# Parse
p.parse_args(namespace=self)
###############################################################################
def _run_benchmark(
resources_dir: Path,
extensions: List[str],
non_aicsimageio_reader: List[Callable],
iterations: int = 3,
):
# Collect files matching the extensions provided
files = []
for ext in extensions:
files += list(resources_dir.glob(ext))
# Run reads for each file and store details in results
results = []
for file in files:
info_read = aicsimageio.AICSImage(file)
yx_planes = np.prod(info_read.size("STCZ"))
for reader in [aicsimageio.imread, non_aicsimageio_reader]:
reader_path = f"{reader.__module__}.{reader.__name__}"
for i in tqdm(range(iterations), desc=f"{reader_path}: {file.name}"):
start = time.perf_counter()
reader(str(file))
results.append(
{
"file_name": file.name,
"file_size_gb": file.stat().st_size / 10e8,
"reader": (
"aicsimageio" if "aicsimageio" in reader_path else "other"
),
"yx_planes": int(yx_planes),
"read_duration": time.perf_counter() - start,
}
)
return results
def _run_benchmark_suite(resources_dir: Path):
# Default reader / imageio imread tests
default_reader_single_image_results = _run_benchmark(
resources_dir=resources_dir,
extensions=["*.png", "*.jpg", "*.bmp"],
non_aicsimageio_reader=imageio.imread,
)
# Default reader / imageio mimread tests
default_reader_many_image_results = _run_benchmark(
resources_dir=resources_dir,
extensions=["*.gif"],
non_aicsimageio_reader=imageio.mimread,
)
# Tiff reader / tifffile imread tests
tiff_reader_results = _run_benchmark(
resources_dir=resources_dir,
extensions=["*.tiff"],
non_aicsimageio_reader=tifffile.imread,
)
# CZI reader / czifile imread tests
czi_reader_results = _run_benchmark(
resources_dir=resources_dir,
extensions=["*.czi"],
non_aicsimageio_reader=czifile.imread,
)
return [
*default_reader_single_image_results,
*default_reader_many_image_results,
*tiff_reader_results,
*czi_reader_results,
]
def run_benchmarks(args: Args):
# Results are stored as they are returned
all_results = {}
# Try running the benchmarks
try:
# Get benchmark resources dir
resources_dir = Path().parent.parent / "aicsimageio" / "tests" / "resources"
# Store machine config
_ = {
"platform": platform.system(),
"platform_version": platform.version(),
"architecture": platform.machine(),
"cpu_total_count": psutil.cpu_count(),
"cpu_current_utilization": psutil.cpu_percent(),
"memory_total_gb": psutil.virtual_memory().total / 10e8,
"memory_available_gb": psutil.virtual_memory().available / 10e8,
}
# Store python config
pyversion = sys.version_info
_ = {
"python_version": f"{pyversion.major}.{pyversion.minor}.{pyversion.micro}",
"aicsimageio": aicsimageio.__version__,
"czifile": czifile.__version__,
"imageio": imageio.__version__,
"tifffile": tifffile.__version__,
}
# Run tests
#######################################################################
log.info(f"Running tests: no cluster...")
log.info(f"=" * 80)
all_results["no-cluster"] = _run_benchmark_suite(resources_dir=resources_dir)
#######################################################################
for cluster_config in CLUSTER_CONFIGS:
total_cores = cluster_config["per_worker_cores"] * cluster_config["workers"]
log.info(
f"Running tests: {cluster_config['name']} "
f"(Total cores: {total_cores}) ..."
)
log.info(f"=" * 80)
# Create or get log dir
# Do not include ms
log_dir_name = datetime.now().isoformat().split(".")[0]
log_dir = Path(f".dask_logs/{log_dir_name}").expanduser()
# Log dir settings
log_dir.mkdir(parents=True, exist_ok=True)
# Calc per_worker_memory
per_worker_memory = cluster_config["per_worker_cores"] * 2
per_worker_memory = f"{per_worker_memory}GB"
# Create cluster
cluster = SLURMCluster(
cores=cluster_config["per_worker_cores"],
memory=per_worker_memory,
queue="aics_cpu_general",
walltime="10:00:00",
local_directory=str(log_dir),
log_directory=str(log_dir),
)
# Scale cluster
cluster.scale(cluster_config["workers"])
# Create client connection
client = Client(cluster)
# Wait for a minute for the cluster to fully spin up
time.sleep(60)
# Run benchmark
all_results[cluster_config["name"]] = _run_benchmark_suite(
resources_dir=resources_dir
)
client.shutdown()
cluster.close()
# Wait for a minute for the cluster to fully shutdown
time.sleep(60)
#######################################################################
log.info(f"Completed all tests")
log.info(f"=" * 80)
# Ensure save dir exists and save results
args.save_path.parent.mkdir(parents=True, exist_ok=True)
with open(args.save_path, "w") as write_out:
json.dump(all_results, write_out)
# Construct and push package
if args.upload:
p = Package()
p.set("results.json", args.save_path)
p.push(
"aicsimageio/benchmarks",
"s3://aics-modeling-packages-test-resources",
message=f"aicsimageio version: {aicsimageio.__version__}",
)
# Catch any exception
except Exception as e:
log.error("=============================================")
if args.debug:
log.error("\n\n" + traceback.format_exc())
log.error("=============================================")
log.error("\n\n" + str(e) + "\n")
log.error("=============================================")
sys.exit(1)
###############################################################################
# Runner
def main():
args = Args()
run_benchmarks(args)
###############################################################################
# Allow caller to directly run this module (usually in development scenarios)
if __name__ == "__main__":
main()