11# Copyright © 2024 Norman Fomferra
22# Permissions are hereby granted under the terms of the MIT License:
33# https://opensource.org/licenses/MIT.
4-
5- from typing import Iterable , Any
4+ from cProfile import Profile
5+ from io import StringIO
6+ from pstats import Stats
7+ from typing import Iterable , Any , Literal
68
79from .config import ConfigItem
810from .config import ConfigLike
1517from .slice import SliceSource
1618from .slice import to_slice_factories
1719from .slice import to_slice_factory
20+ from .log import logger
1821
1922
2023__all__ = [
3134 "to_slice_factory" ,
3235]
3336
37+ _TOTAL_TIME : Literal ["tottime" ] = "tottime"
38+
3439
3540def zappend (
36- slices : Iterable [SliceObj | SliceFactory ], config : ConfigLike = None , ** kwargs : Any
41+ slices : Iterable [SliceObj | SliceFactory ],
42+ config : ConfigLike = None ,
43+ profiling = None ,
44+ ** kwargs : Any ,
3745):
3846 """
3947 Robustly create or update a Zarr dataset from dataset slices.
@@ -57,8 +65,25 @@ def zappend(
5765 May be a file path or URI, a ``dict``, ``None``, or a sequence of
5866 the aforementioned. If a sequence is used, subsequent configurations
5967 are incremental to the previous ones.
68+ profiling: Path for profiling output.
69+ Switches on profiling
6070 kwargs: Additional configuration parameters.
6171 Can be used to pass or override configuration values in *config*.
6272 """
6373 processor = Processor (config , ** kwargs )
74+
75+ if profiling :
76+ profile = Profile ()
77+ logger .info (f"profiling ..." )
78+ profile .enable ()
79+
6480 processor .process_slices (slices )
81+
82+ if profiling :
83+ profile .disable ()
84+ results = StringIO ()
85+ stats = Stats (profile , stream = results )
86+ stats .sort_stats (_TOTAL_TIME ).print_stats ()
87+ with open (profiling , "w" ) as f :
88+ f .write (results .getvalue ())
89+ logger .info (f"profiling output written to { profiling } " )
0 commit comments