Coverage for python / lsst / daf / butler / _butler_metrics.py: 62%
39 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-17 16:22 -0800
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-17 16:22 -0800
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28from __future__ import annotations
30from collections.abc import Callable, Iterator
31from contextlib import contextmanager
33from pydantic import BaseModel
35from lsst.utils.logging import LsstLoggers
36from lsst.utils.timer import time_this
39class ButlerMetrics(BaseModel):
40 """Metrics collected during Butler operations."""
42 time_in_put: float = 0.0
43 """Wall-clock time, in seconds, spent in put()."""
45 time_in_get: float = 0.0
46 """Wall-clock time, in seconds, spent in get()."""
48 n_get: int = 0
49 """Number of datasets retrieved with get()."""
51 n_put: int = 0
52 """Number of datasets stored with put()."""
54 def reset(self) -> None:
55 """Reset all metrics."""
56 self.time_in_put = 0.0
57 self.time_in_get = 0.0
58 self.n_get = 0
59 self.n_put = 0
61 def increment_get(self, duration: float) -> None:
62 """Increment time for get().
64 Parameters
65 ----------
66 duration : `float`
67 Duration to add to the get() statistics.
68 """
69 self.time_in_get += duration
70 self.n_get += 1
72 def increment_put(self, duration: float) -> None:
73 """Increment time for put().
75 Parameters
76 ----------
77 duration : `float`
78 Duration to add to the put() statistics.
79 """
80 self.time_in_put += duration
81 self.n_put += 1
83 @contextmanager
84 def _timer(
85 self, handler: Callable[[float], None], log: LsstLoggers | None = None, msg: str | None = None
86 ) -> Iterator[None]:
87 with time_this(log=log, msg=msg) as timer:
88 yield
89 handler(timer.duration)
91 @contextmanager
92 def instrument_get(self, log: LsstLoggers | None = None, msg: str | None = None) -> Iterator[None]:
93 """Run code and increment get statistics.
95 Parameters
96 ----------
97 log : `logging.Logger` or `None`
98 Logger to use for any timing information.
99 msg : `str` or `None`
100 Any message to be included in log output.
101 """
102 with self._timer(self.increment_get, log=log, msg=msg):
103 yield
105 @contextmanager
106 def instrument_put(self, log: LsstLoggers | None = None, msg: str | None = None) -> Iterator[None]:
107 """Run code and increment put statistics.
109 Parameters
110 ----------
111 log : `logging.Logger` or `None`
112 Logger to use for any timing information.
113 msg : `str` or `None`
114 Any message to be included in log output.
115 """
116 with self._timer(self.increment_put, log=log, msg=msg):
117 yield