xtr-stopwatch 7.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xtr_stopwatch/__init__.py +51 -0
- xtr_stopwatch/_milliseconds.py +17 -0
- xtr_stopwatch/exception/__init__.py +17 -0
- xtr_stopwatch/exception/event_not_started_error.py +25 -0
- xtr_stopwatch/exception/no_started_section_error.py +25 -0
- xtr_stopwatch/exception/stopwatch_error.py +14 -0
- xtr_stopwatch/exception/unknown_event_error.py +24 -0
- xtr_stopwatch/exception/unknown_section_error.py +30 -0
- xtr_stopwatch/py.typed +0 -0
- xtr_stopwatch/section.py +131 -0
- xtr_stopwatch/stopwatch.py +202 -0
- xtr_stopwatch/stopwatch_event.py +158 -0
- xtr_stopwatch/stopwatch_period.py +48 -0
- xtr_stopwatch-7.0.0.dist-info/METADATA +181 -0
- xtr_stopwatch-7.0.0.dist-info/RECORD +17 -0
- xtr_stopwatch-7.0.0.dist-info/WHEEL +4 -0
- xtr_stopwatch-7.0.0.dist-info/licenses/LICENSE +22 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Times what code does: named events, grouped by category, nested in sections.
|
|
2
|
+
|
|
3
|
+
A profile wants to know where a request spent its time — which listener,
|
|
4
|
+
which query, which template — and on one axis, so the pieces can be drawn
|
|
5
|
+
as a timeline. :class:`~xtr_stopwatch.stopwatch.Stopwatch` records exactly
|
|
6
|
+
that: each event is a name, a category and the periods it ran for, and every
|
|
7
|
+
event of a section is measured from the section's own origin.
|
|
8
|
+
|
|
9
|
+
What is timed belongs to the calling context, so concurrent requests served
|
|
10
|
+
by one process each see only their own events.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
16
|
+
|
|
17
|
+
from .exception import (
|
|
18
|
+
EventNotStartedError,
|
|
19
|
+
NoStartedSectionError,
|
|
20
|
+
StopwatchError,
|
|
21
|
+
UnknownEventError,
|
|
22
|
+
UnknownSectionError,
|
|
23
|
+
)
|
|
24
|
+
from .section import Section
|
|
25
|
+
from .stopwatch import ROOT, SECTION_CATEGORY, SECTION_CHILD_EVENT, SECTION_EVENT, Stopwatch
|
|
26
|
+
from .stopwatch_event import StopwatchEvent
|
|
27
|
+
from .stopwatch_period import StopwatchPeriod
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
__version__ = version("xtr-stopwatch")
|
|
31
|
+
except PackageNotFoundError: # pragma: no cover
|
|
32
|
+
# Running from a source tree or a vendored copy, with no installed
|
|
33
|
+
# metadata to read. Having no version is better than refusing to import.
|
|
34
|
+
__version__ = "0+unknown"
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
"ROOT",
|
|
38
|
+
"SECTION_CATEGORY",
|
|
39
|
+
"SECTION_CHILD_EVENT",
|
|
40
|
+
"SECTION_EVENT",
|
|
41
|
+
"EventNotStartedError",
|
|
42
|
+
"NoStartedSectionError",
|
|
43
|
+
"Section",
|
|
44
|
+
"Stopwatch",
|
|
45
|
+
"StopwatchError",
|
|
46
|
+
"StopwatchEvent",
|
|
47
|
+
"StopwatchPeriod",
|
|
48
|
+
"UnknownEventError",
|
|
49
|
+
"UnknownSectionError",
|
|
50
|
+
"__version__",
|
|
51
|
+
]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""The one reading every timing in this library is taken with."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, Final
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from xtr_clock import ClockInterface
|
|
9
|
+
|
|
10
|
+
__all__ = ["now_ms"]
|
|
11
|
+
|
|
12
|
+
_MILLISECONDS_PER_SECOND: Final = 1000.0
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def now_ms(clock: ClockInterface) -> float:
|
|
16
|
+
"""Return ``clock``'s current instant, in milliseconds since the epoch."""
|
|
17
|
+
return clock.now().timestamp() * _MILLISECONDS_PER_SECOND
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Everything this library raises, under :class:`StopwatchError`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .event_not_started_error import EventNotStartedError
|
|
6
|
+
from .no_started_section_error import NoStartedSectionError
|
|
7
|
+
from .stopwatch_error import StopwatchError
|
|
8
|
+
from .unknown_event_error import UnknownEventError
|
|
9
|
+
from .unknown_section_error import UnknownSectionError
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"EventNotStartedError",
|
|
13
|
+
"NoStartedSectionError",
|
|
14
|
+
"StopwatchError",
|
|
15
|
+
"UnknownEventError",
|
|
16
|
+
"UnknownSectionError",
|
|
17
|
+
]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""An event was stopped that is not running."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .stopwatch_error import StopwatchError
|
|
6
|
+
|
|
7
|
+
__all__ = ["EventNotStartedError"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class EventNotStartedError(StopwatchError):
|
|
11
|
+
"""An event was stopped, or lapped, while it was not running.
|
|
12
|
+
|
|
13
|
+
A stop with no start to pair it with has no period to close, so it is
|
|
14
|
+
refused rather than recorded as nothing.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
name: The event that was not running.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
name: str
|
|
21
|
+
|
|
22
|
+
def __init__(self, name: str) -> None:
|
|
23
|
+
"""Record the event that was not running."""
|
|
24
|
+
self.name = name
|
|
25
|
+
super().__init__(f'event "{name}" is not started')
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""A section was stopped while none was open."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .stopwatch_error import StopwatchError
|
|
6
|
+
|
|
7
|
+
__all__ = ["NoStartedSectionError"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class NoStartedSectionError(StopwatchError):
|
|
11
|
+
"""A section was stopped while none was open.
|
|
12
|
+
|
|
13
|
+
The root section is always open and is never stopped, so stopping with
|
|
14
|
+
only the root in place has nothing to stop.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
section_id: The id the section would have been stored under.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
section_id: str
|
|
21
|
+
|
|
22
|
+
def __init__(self, section_id: str) -> None:
|
|
23
|
+
"""Record the id the stop was asked for."""
|
|
24
|
+
self.section_id = section_id
|
|
25
|
+
super().__init__(f'there is no started section to stop as "{section_id}"')
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""The root every error in this library derives from."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = ["StopwatchError"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class StopwatchError(Exception):
|
|
9
|
+
"""Base class for every error raised by this library.
|
|
10
|
+
|
|
11
|
+
Catch this to handle anything timing can go wrong with; catch a subclass to
|
|
12
|
+
handle one cause. Every subclass carries what went wrong as typed attributes
|
|
13
|
+
and composes its own message from them.
|
|
14
|
+
"""
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""An event was asked for that the current section never started."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .stopwatch_error import StopwatchError
|
|
6
|
+
|
|
7
|
+
__all__ = ["UnknownEventError"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class UnknownEventError(StopwatchError, LookupError):
|
|
11
|
+
"""An event was asked for that the current section never started.
|
|
12
|
+
|
|
13
|
+
Also a :class:`LookupError`, as a missing key is.
|
|
14
|
+
|
|
15
|
+
Attributes:
|
|
16
|
+
name: The event that is not known.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
name: str
|
|
20
|
+
|
|
21
|
+
def __init__(self, name: str) -> None:
|
|
22
|
+
"""Record the event that is not known."""
|
|
23
|
+
self.name = name
|
|
24
|
+
super().__init__(f'event "{name}" is not known')
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""A section was reopened that the current one does not hold."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .stopwatch_error import StopwatchError
|
|
6
|
+
|
|
7
|
+
__all__ = ["UnknownSectionError"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class UnknownSectionError(StopwatchError, LookupError):
|
|
11
|
+
"""A section was reopened by an id the current section holds no child under.
|
|
12
|
+
|
|
13
|
+
Reopening adds periods to a section that was stopped earlier, and that is
|
|
14
|
+
only meaningful at the level it lives at: a section started elsewhere is
|
|
15
|
+
refused rather than silently begun afresh here.
|
|
16
|
+
|
|
17
|
+
Also a :class:`LookupError`, as a missing key is.
|
|
18
|
+
|
|
19
|
+
Attributes:
|
|
20
|
+
section_id: The id that is not a child of the current section.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
section_id: str
|
|
24
|
+
|
|
25
|
+
def __init__(self, section_id: str) -> None:
|
|
26
|
+
"""Record the id that was asked for."""
|
|
27
|
+
self.section_id = section_id
|
|
28
|
+
super().__init__(
|
|
29
|
+
f'section "{section_id}" was started at another level and cannot be opened here'
|
|
30
|
+
)
|
xtr_stopwatch/py.typed
ADDED
|
File without changes
|
xtr_stopwatch/section.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""A group of events sharing one origin, nested in the section that opened it."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, Self, final, override
|
|
6
|
+
|
|
7
|
+
from xtr_clock import MonotonicClock
|
|
8
|
+
|
|
9
|
+
from ._milliseconds import now_ms
|
|
10
|
+
from .exception import EventNotStartedError, UnknownEventError
|
|
11
|
+
from .stopwatch_event import StopwatchEvent
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from xtr_clock import ClockInterface
|
|
15
|
+
|
|
16
|
+
__all__ = ["Section"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@final
|
|
20
|
+
class Section:
|
|
21
|
+
"""Holds the events timed while it was the current section.
|
|
22
|
+
|
|
23
|
+
A section measures every event it holds from one origin — the instant it
|
|
24
|
+
was opened — so its events line up on one timeline. The root section has
|
|
25
|
+
no origin of its own; each of its events is measured from the instant it
|
|
26
|
+
first started.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
__slots__ = ("_children", "_clock", "_events", "_id", "_origin")
|
|
30
|
+
|
|
31
|
+
def __init__(self, origin: float | None = None, clock: ClockInterface | None = None) -> None:
|
|
32
|
+
"""Make a section measuring its events from ``origin``.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
origin: The instant the section began, in milliseconds since the
|
|
36
|
+
epoch; ``None`` for the root.
|
|
37
|
+
clock: What time is read from. ``None`` uses a
|
|
38
|
+
:class:`~xtr_clock.MonotonicClock`.
|
|
39
|
+
"""
|
|
40
|
+
self._origin = origin
|
|
41
|
+
self._clock: ClockInterface = clock if clock is not None else MonotonicClock()
|
|
42
|
+
self._id: str | None = None
|
|
43
|
+
self._children: list[Section] = []
|
|
44
|
+
self._events: dict[str, StopwatchEvent] = {}
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def id(self) -> str | None:
|
|
48
|
+
"""The id the section was stopped under, or ``None`` while it runs."""
|
|
49
|
+
return self._id
|
|
50
|
+
|
|
51
|
+
def set_id(self, section_id: str) -> Self:
|
|
52
|
+
"""Name the section ``section_id``."""
|
|
53
|
+
self._id = section_id
|
|
54
|
+
return self
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def children(self) -> tuple[Section, ...]:
|
|
58
|
+
"""The sections opened inside this one, in the order they opened."""
|
|
59
|
+
return tuple(self._children)
|
|
60
|
+
|
|
61
|
+
def get(self, section_id: str) -> Section | None:
|
|
62
|
+
"""Return the child named ``section_id``, or ``None``."""
|
|
63
|
+
for child in self._children:
|
|
64
|
+
if child.id == section_id:
|
|
65
|
+
return child
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
def open(self, section_id: str | None) -> Section:
|
|
69
|
+
"""Return the child named ``section_id``, opening a new one when there is none."""
|
|
70
|
+
child = self.get(section_id) if section_id is not None else None
|
|
71
|
+
if child is None:
|
|
72
|
+
child = Section(now_ms(self._clock), self._clock)
|
|
73
|
+
self._children.append(child)
|
|
74
|
+
return child
|
|
75
|
+
|
|
76
|
+
def start_event(self, name: str, category: str | None) -> StopwatchEvent:
|
|
77
|
+
"""Start ``name`` here, making the event the first time it is started."""
|
|
78
|
+
event = self._events.get(name)
|
|
79
|
+
if event is None:
|
|
80
|
+
origin = self._origin if self._origin is not None else now_ms(self._clock)
|
|
81
|
+
event = StopwatchEvent(origin, category, name, self._clock)
|
|
82
|
+
self._events[name] = event
|
|
83
|
+
return event.start()
|
|
84
|
+
|
|
85
|
+
def is_event_started(self, name: str) -> bool:
|
|
86
|
+
"""Tell whether ``name`` has an open period here."""
|
|
87
|
+
event = self._events.get(name)
|
|
88
|
+
return event is not None and event.is_started()
|
|
89
|
+
|
|
90
|
+
def stop_event(self, name: str) -> StopwatchEvent:
|
|
91
|
+
"""Stop ``name`` here.
|
|
92
|
+
|
|
93
|
+
Raises:
|
|
94
|
+
EventNotStartedError: When this section never started ``name``,
|
|
95
|
+
or it is not running.
|
|
96
|
+
"""
|
|
97
|
+
event = self._events.get(name)
|
|
98
|
+
if event is None:
|
|
99
|
+
raise EventNotStartedError(name)
|
|
100
|
+
return event.stop()
|
|
101
|
+
|
|
102
|
+
def lap_event(self, name: str) -> StopwatchEvent:
|
|
103
|
+
"""Stop ``name`` and start it again at once.
|
|
104
|
+
|
|
105
|
+
Raises:
|
|
106
|
+
EventNotStartedError: When ``name`` is not running here.
|
|
107
|
+
"""
|
|
108
|
+
return self.stop_event(name).start()
|
|
109
|
+
|
|
110
|
+
def get_event(self, name: str) -> StopwatchEvent:
|
|
111
|
+
"""Return the event ``name``.
|
|
112
|
+
|
|
113
|
+
Raises:
|
|
114
|
+
UnknownEventError: When this section never started ``name``.
|
|
115
|
+
"""
|
|
116
|
+
event = self._events.get(name)
|
|
117
|
+
if event is None:
|
|
118
|
+
raise UnknownEventError(name)
|
|
119
|
+
return event
|
|
120
|
+
|
|
121
|
+
def get_events(self) -> dict[str, StopwatchEvent]:
|
|
122
|
+
"""Return every event of this section by name, in the order each first started."""
|
|
123
|
+
return dict(self._events)
|
|
124
|
+
|
|
125
|
+
@override
|
|
126
|
+
def __repr__(self) -> str:
|
|
127
|
+
"""Return the section's id and how many events and children it holds."""
|
|
128
|
+
return (
|
|
129
|
+
f"{type(self).__name__}(id={self._id!r}, events={len(self._events)}, "
|
|
130
|
+
f"children={len(self._children)})"
|
|
131
|
+
)
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Times named events in nested sections, kept apart for every unit of work."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from contextvars import ContextVar
|
|
6
|
+
from typing import TYPE_CHECKING, Final, final, override
|
|
7
|
+
|
|
8
|
+
from xtr_clock import MonotonicClock
|
|
9
|
+
|
|
10
|
+
from .exception import NoStartedSectionError, UnknownSectionError
|
|
11
|
+
from .section import Section
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from xtr_clock import ClockInterface
|
|
15
|
+
|
|
16
|
+
from .stopwatch_event import StopwatchEvent
|
|
17
|
+
|
|
18
|
+
__all__ = ["ROOT", "SECTION_CATEGORY", "SECTION_CHILD_EVENT", "SECTION_EVENT", "Stopwatch"]
|
|
19
|
+
|
|
20
|
+
ROOT: Final = "__root__"
|
|
21
|
+
"""The id the root section is kept under."""
|
|
22
|
+
|
|
23
|
+
SECTION_EVENT: Final = "__section__"
|
|
24
|
+
"""The event spanning a section, started in it as it opens and stopped as it closes."""
|
|
25
|
+
|
|
26
|
+
SECTION_CHILD_EVENT: Final = "__section__.child"
|
|
27
|
+
"""The event spanning a child section, kept in the section that opened it."""
|
|
28
|
+
|
|
29
|
+
SECTION_CATEGORY: Final = "section"
|
|
30
|
+
"""The category of the event spanning a child section."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@final
|
|
34
|
+
class _Activity:
|
|
35
|
+
"""What one unit of work timed: its open sections and the ones it stopped.
|
|
36
|
+
|
|
37
|
+
Mutated in place, so code run in a copy of the context that made it — a
|
|
38
|
+
synchronous function a worker thread runs — times into the same sections.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
__slots__ = ("sections", "stack")
|
|
42
|
+
|
|
43
|
+
def __init__(self, clock: ClockInterface) -> None:
|
|
44
|
+
root = Section(None, clock)
|
|
45
|
+
self.stack: list[Section] = [root]
|
|
46
|
+
self.sections: dict[str, Section] = {ROOT: root}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@final
|
|
50
|
+
class Stopwatch:
|
|
51
|
+
"""Times what code does, as named events grouped into sections.
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
stopwatch = Stopwatch()
|
|
55
|
+
stopwatch.open_section()
|
|
56
|
+
stopwatch.start("load", "orm")
|
|
57
|
+
...
|
|
58
|
+
stopwatch.stop("load")
|
|
59
|
+
stopwatch.stop_section("request-42")
|
|
60
|
+
stopwatch.get_section_events("request-42")["load"].get_duration()
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
An event is started and stopped by name in the current section; a
|
|
64
|
+
section opened inside another nests under it, and is stored under the id
|
|
65
|
+
it is stopped with. Opening a section starts :data:`SECTION_EVENT` in it
|
|
66
|
+
and :data:`SECTION_CHILD_EVENT` in its parent, both stopped as it closes,
|
|
67
|
+
so a section's own duration is one of its events.
|
|
68
|
+
|
|
69
|
+
What is timed belongs to the calling context: every concurrent unit of
|
|
70
|
+
work — every request a server handles at once — sees only its own
|
|
71
|
+
sections. :meth:`reset` starts the calling context afresh, which is how a
|
|
72
|
+
unit of work begins with nothing timed.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
__slots__ = ("_activity", "_clock", "detached_variable")
|
|
76
|
+
|
|
77
|
+
def __init__(self, clock: ClockInterface | None = None) -> None:
|
|
78
|
+
"""Make a stopwatch reading ``clock``.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
clock: What time is read from. ``None`` uses a
|
|
82
|
+
:class:`~xtr_clock.MonotonicClock`, because a duration must not
|
|
83
|
+
follow a wall-clock correction.
|
|
84
|
+
"""
|
|
85
|
+
self._clock: ClockInterface = clock if clock is not None else MonotonicClock()
|
|
86
|
+
self._activity: ContextVar[_Activity | None] = ContextVar(
|
|
87
|
+
"stopwatch_activity", default=None
|
|
88
|
+
)
|
|
89
|
+
self.detached_variable: tuple[ContextVar[_Activity | None], None] = (self._activity, None)
|
|
90
|
+
"""The variable a new connection must start without, and the value it starts from.
|
|
91
|
+
|
|
92
|
+
A server may start a connection from a copy of another's context taken
|
|
93
|
+
mid-request; whoever builds the stopwatch for a container registers
|
|
94
|
+
this, so that request's timing never reaches the new one.
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
def open_section(self, section_id: str | None = None) -> None:
|
|
98
|
+
"""Open a section inside the current one, or reopen the child named ``section_id``.
|
|
99
|
+
|
|
100
|
+
Raises:
|
|
101
|
+
UnknownSectionError: When ``section_id`` is given and the current
|
|
102
|
+
section holds no child under it.
|
|
103
|
+
"""
|
|
104
|
+
activity = self._current()
|
|
105
|
+
current = activity.stack[-1]
|
|
106
|
+
if section_id is not None and current.get(section_id) is None:
|
|
107
|
+
raise UnknownSectionError(section_id)
|
|
108
|
+
_ = current.start_event(SECTION_CHILD_EVENT, SECTION_CATEGORY)
|
|
109
|
+
activity.stack.append(current.open(section_id))
|
|
110
|
+
_ = activity.stack[-1].start_event(SECTION_EVENT, None)
|
|
111
|
+
|
|
112
|
+
def stop_section(self, section_id: str) -> None:
|
|
113
|
+
"""Close the current section, storing it under ``section_id``.
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
NoStartedSectionError: When only the root section is open.
|
|
117
|
+
"""
|
|
118
|
+
activity = self._current()
|
|
119
|
+
if len(activity.stack) == 1:
|
|
120
|
+
raise NoStartedSectionError(section_id)
|
|
121
|
+
_ = activity.stack[-1].stop_event(SECTION_EVENT)
|
|
122
|
+
section = activity.stack.pop().set_id(section_id)
|
|
123
|
+
activity.sections[section_id] = section
|
|
124
|
+
_ = activity.stack[-1].stop_event(SECTION_CHILD_EVENT)
|
|
125
|
+
|
|
126
|
+
def start(self, name: str, category: str | None = None) -> StopwatchEvent:
|
|
127
|
+
"""Start the event ``name`` in the current section and return it.
|
|
128
|
+
|
|
129
|
+
Keep the returned event to stop it: it stays the same object even if
|
|
130
|
+
the context is reset while it runs.
|
|
131
|
+
"""
|
|
132
|
+
return self._current().stack[-1].start_event(name, category)
|
|
133
|
+
|
|
134
|
+
def stop(self, name: str) -> StopwatchEvent:
|
|
135
|
+
"""Stop the event ``name`` in the current section and return it.
|
|
136
|
+
|
|
137
|
+
Raises:
|
|
138
|
+
EventNotStartedError: When ``name`` is not running there.
|
|
139
|
+
"""
|
|
140
|
+
return self._current().stack[-1].stop_event(name)
|
|
141
|
+
|
|
142
|
+
def lap(self, name: str) -> StopwatchEvent:
|
|
143
|
+
"""Stop the event ``name`` and start it again at once.
|
|
144
|
+
|
|
145
|
+
Raises:
|
|
146
|
+
EventNotStartedError: When ``name`` is not running in the current
|
|
147
|
+
section.
|
|
148
|
+
"""
|
|
149
|
+
return self._current().stack[-1].lap_event(name)
|
|
150
|
+
|
|
151
|
+
def is_started(self, name: str) -> bool:
|
|
152
|
+
"""Tell whether the event ``name`` is running in the current section."""
|
|
153
|
+
return self._current().stack[-1].is_event_started(name)
|
|
154
|
+
|
|
155
|
+
def has_open_section(self) -> bool:
|
|
156
|
+
"""Tell whether the calling context has a section open, without starting anything.
|
|
157
|
+
|
|
158
|
+
A unit of work timing itself opens a section; code that times what it
|
|
159
|
+
does on a shared stopwatch asks this first, so a context nobody times
|
|
160
|
+
in — a worker's own loop, a long command — never accumulates events.
|
|
161
|
+
"""
|
|
162
|
+
activity = self._activity.get()
|
|
163
|
+
return activity is not None and len(activity.stack) > 1
|
|
164
|
+
|
|
165
|
+
def get_event(self, name: str) -> StopwatchEvent:
|
|
166
|
+
"""Return the event ``name`` of the current section.
|
|
167
|
+
|
|
168
|
+
Raises:
|
|
169
|
+
UnknownEventError: When the current section never started ``name``.
|
|
170
|
+
"""
|
|
171
|
+
return self._current().stack[-1].get_event(name)
|
|
172
|
+
|
|
173
|
+
def get_section_events(self, section_id: str) -> dict[str, StopwatchEvent]:
|
|
174
|
+
"""Return the events of the section stored as ``section_id``; empty when there is none."""
|
|
175
|
+
section = self._current().sections.get(section_id)
|
|
176
|
+
return section.get_events() if section is not None else {}
|
|
177
|
+
|
|
178
|
+
def get_sections(self) -> dict[str, Section]:
|
|
179
|
+
"""Return every stopped section by id, the root included."""
|
|
180
|
+
return dict(self._current().sections)
|
|
181
|
+
|
|
182
|
+
def reset(self) -> None:
|
|
183
|
+
"""Forget everything the calling context timed, and start it afresh.
|
|
184
|
+
|
|
185
|
+
Other contexts are left as they are, and an event already handed out
|
|
186
|
+
stays the object it was, so stopping it still closes its period.
|
|
187
|
+
"""
|
|
188
|
+
_ = self._activity.set(_Activity(self._clock))
|
|
189
|
+
|
|
190
|
+
def _current(self) -> _Activity:
|
|
191
|
+
activity = self._activity.get()
|
|
192
|
+
if activity is None:
|
|
193
|
+
activity = _Activity(self._clock)
|
|
194
|
+
_ = self._activity.set(activity)
|
|
195
|
+
return activity
|
|
196
|
+
|
|
197
|
+
@override
|
|
198
|
+
def __repr__(self) -> str:
|
|
199
|
+
"""Return how deep the calling context's sections are open."""
|
|
200
|
+
activity = self._activity.get()
|
|
201
|
+
depth = len(activity.stack) - 1 if activity is not None else 0
|
|
202
|
+
return f"{type(self).__name__}(open_sections={depth})"
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""A named thing being timed, made of every period it ran for."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, Final, Self, final, override
|
|
6
|
+
|
|
7
|
+
from xtr_clock import MonotonicClock
|
|
8
|
+
|
|
9
|
+
from ._milliseconds import now_ms
|
|
10
|
+
from .exception import EventNotStartedError
|
|
11
|
+
from .stopwatch_period import StopwatchPeriod
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from xtr_clock import ClockInterface
|
|
15
|
+
|
|
16
|
+
__all__ = ["StopwatchEvent"]
|
|
17
|
+
|
|
18
|
+
_DEFAULT_CATEGORY: Final = "default"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@final
|
|
22
|
+
class StopwatchEvent:
|
|
23
|
+
"""Times one named thing, however many times it runs.
|
|
24
|
+
|
|
25
|
+
Each :meth:`start` opens a period and the matching :meth:`stop` closes
|
|
26
|
+
it, so a listener called three times is one event with three periods
|
|
27
|
+
rather than three events. Starts nest: a start while already running opens
|
|
28
|
+
a second period, and stops close them last-opened first.
|
|
29
|
+
|
|
30
|
+
Every reading is in milliseconds from :attr:`origin` — the instant the
|
|
31
|
+
event's section began — so the events of a section share one axis and can
|
|
32
|
+
be drawn on one timeline.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
__slots__ = ("_category", "_clock", "_name", "_origin", "_periods", "_started")
|
|
36
|
+
|
|
37
|
+
def __init__(
|
|
38
|
+
self,
|
|
39
|
+
origin: float,
|
|
40
|
+
category: str | None = None,
|
|
41
|
+
name: str | None = None,
|
|
42
|
+
clock: ClockInterface | None = None,
|
|
43
|
+
) -> None:
|
|
44
|
+
"""Make an event measured from ``origin``.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
origin: The instant readings are measured from, in milliseconds
|
|
48
|
+
since the epoch.
|
|
49
|
+
category: What kind of work this is, grouping events on a
|
|
50
|
+
timeline; ``"default"`` when not given.
|
|
51
|
+
name: What the event is called, for errors and for plain data.
|
|
52
|
+
clock: What time is read from. ``None`` uses a
|
|
53
|
+
:class:`~xtr_clock.MonotonicClock`, because a duration must not
|
|
54
|
+
follow a wall-clock correction.
|
|
55
|
+
"""
|
|
56
|
+
self._origin = origin
|
|
57
|
+
self._category = category or _DEFAULT_CATEGORY
|
|
58
|
+
self._name = name or "unknown"
|
|
59
|
+
self._clock: ClockInterface = clock if clock is not None else MonotonicClock()
|
|
60
|
+
self._started: list[float] = []
|
|
61
|
+
self._periods: list[StopwatchPeriod] = []
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def name(self) -> str:
|
|
65
|
+
"""What the event is called."""
|
|
66
|
+
return self._name
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def category(self) -> str:
|
|
70
|
+
"""What kind of work the event times."""
|
|
71
|
+
return self._category
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def origin(self) -> float:
|
|
75
|
+
"""The instant readings are measured from, in milliseconds since the epoch."""
|
|
76
|
+
return self._origin
|
|
77
|
+
|
|
78
|
+
def start(self) -> Self:
|
|
79
|
+
"""Open a period now."""
|
|
80
|
+
self._started.append(self._now())
|
|
81
|
+
return self
|
|
82
|
+
|
|
83
|
+
def stop(self) -> Self:
|
|
84
|
+
"""Close the period opened last.
|
|
85
|
+
|
|
86
|
+
Raises:
|
|
87
|
+
EventNotStartedError: When no period is open.
|
|
88
|
+
"""
|
|
89
|
+
if not self._started:
|
|
90
|
+
raise EventNotStartedError(self._name)
|
|
91
|
+
self._periods.append(StopwatchPeriod(self._started.pop(), self._now()))
|
|
92
|
+
return self
|
|
93
|
+
|
|
94
|
+
def lap(self) -> Self:
|
|
95
|
+
"""Close the open period and open the next one at the same instant.
|
|
96
|
+
|
|
97
|
+
Raises:
|
|
98
|
+
EventNotStartedError: When no period is open.
|
|
99
|
+
"""
|
|
100
|
+
return self.stop().start()
|
|
101
|
+
|
|
102
|
+
def is_started(self) -> bool:
|
|
103
|
+
"""Tell whether a period is open."""
|
|
104
|
+
return bool(self._started)
|
|
105
|
+
|
|
106
|
+
def ensure_stopped(self) -> None:
|
|
107
|
+
"""Close every open period."""
|
|
108
|
+
while self._started:
|
|
109
|
+
_ = self.stop()
|
|
110
|
+
|
|
111
|
+
def get_periods(self) -> tuple[StopwatchPeriod, ...]:
|
|
112
|
+
"""Return every closed period, in the order they closed."""
|
|
113
|
+
return tuple(self._periods)
|
|
114
|
+
|
|
115
|
+
def get_start_time(self) -> float:
|
|
116
|
+
"""Return when the event first began, in milliseconds from the origin.
|
|
117
|
+
|
|
118
|
+
The first closed period's start, or the first open one's when none has
|
|
119
|
+
closed yet; ``0.0`` for an event that never started.
|
|
120
|
+
"""
|
|
121
|
+
if self._periods:
|
|
122
|
+
return self._periods[0].start
|
|
123
|
+
return self._started[0] if self._started else 0.0
|
|
124
|
+
|
|
125
|
+
def get_end_time(self) -> float:
|
|
126
|
+
"""Return when the last closed period ended, in milliseconds from the origin."""
|
|
127
|
+
return self._periods[-1].end if self._periods else 0.0
|
|
128
|
+
|
|
129
|
+
def get_duration(self) -> float:
|
|
130
|
+
"""Return the time spent in every period, open ones up to now, in milliseconds."""
|
|
131
|
+
closed = sum(period.duration for period in self._periods)
|
|
132
|
+
if not self._started:
|
|
133
|
+
return closed
|
|
134
|
+
now = self._now()
|
|
135
|
+
return closed + sum(now - start for start in self._started)
|
|
136
|
+
|
|
137
|
+
def to_dict(self) -> dict[str, object]:
|
|
138
|
+
"""Return the event as plain data, readable once serialised to JSON."""
|
|
139
|
+
return {
|
|
140
|
+
"name": self._name,
|
|
141
|
+
"category": self._category,
|
|
142
|
+
"origin": self._origin,
|
|
143
|
+
"start": self.get_start_time(),
|
|
144
|
+
"end": self.get_end_time(),
|
|
145
|
+
"duration": self.get_duration(),
|
|
146
|
+
"periods": [period.to_dict() for period in self._periods],
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
def _now(self) -> float:
|
|
150
|
+
return now_ms(self._clock) - self._origin
|
|
151
|
+
|
|
152
|
+
@override
|
|
153
|
+
def __repr__(self) -> str:
|
|
154
|
+
"""Return the event's name, category and duration so far."""
|
|
155
|
+
return (
|
|
156
|
+
f"{type(self).__name__}({self._name!r}, category={self._category!r}, "
|
|
157
|
+
f"duration={self.get_duration():.3f})"
|
|
158
|
+
)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""One stretch of time an event ran for."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import final, override
|
|
6
|
+
|
|
7
|
+
__all__ = ["StopwatchPeriod"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@final
|
|
11
|
+
class StopwatchPeriod:
|
|
12
|
+
"""One start and the stop that closed it.
|
|
13
|
+
|
|
14
|
+
Both ends are milliseconds from the origin of the event the period belongs
|
|
15
|
+
to, so periods of one event — and of every event of one section — line up
|
|
16
|
+
on the same axis.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
__slots__ = ("_end", "_start")
|
|
20
|
+
|
|
21
|
+
def __init__(self, start: float, end: float) -> None:
|
|
22
|
+
"""Record a period from ``start`` to ``end``, both in milliseconds from the origin."""
|
|
23
|
+
self._start = start
|
|
24
|
+
self._end = end
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def start(self) -> float:
|
|
28
|
+
"""When the period began, in milliseconds from the origin."""
|
|
29
|
+
return self._start
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def end(self) -> float:
|
|
33
|
+
"""When the period ended, in milliseconds from the origin."""
|
|
34
|
+
return self._end
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def duration(self) -> float:
|
|
38
|
+
"""How long the period lasted, in milliseconds."""
|
|
39
|
+
return self._end - self._start
|
|
40
|
+
|
|
41
|
+
def to_dict(self) -> dict[str, float]:
|
|
42
|
+
"""Return the period as plain data: ``start``, ``end`` and ``duration``."""
|
|
43
|
+
return {"start": self._start, "end": self._end, "duration": self.duration}
|
|
44
|
+
|
|
45
|
+
@override
|
|
46
|
+
def __repr__(self) -> str:
|
|
47
|
+
"""Return the period's span, which is all there is to see."""
|
|
48
|
+
return f"{type(self).__name__}(start={self._start:.3f}, end={self._end:.3f})"
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xtr-stopwatch
|
|
3
|
+
Version: 7.0.0
|
|
4
|
+
Summary: Times what code does, in named events grouped by category and nested in sections, kept apart for every concurrent unit of work.
|
|
5
|
+
Keywords: stopwatch,timing,profiling,performance,timeline
|
|
6
|
+
Author: Xterr
|
|
7
|
+
Author-email: Xterr <me@xterr.dev>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Topic :: Software Development :: Debuggers
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Dist: xtr-clock>=7.0,<8
|
|
16
|
+
Requires-Python: >=3.14.4
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
<div align="center">
|
|
20
|
+
|
|
21
|
+
# xtr-stopwatch
|
|
22
|
+
|
|
23
|
+
**Where the time went: named events, grouped by category, nested in sections — one set per unit of work.**
|
|
24
|
+
|
|
25
|
+
<img alt="python 3.14+" src="https://img.shields.io/badge/python-%E2%89%A5%203.14-3776AB?logo=python&logoColor=white">
|
|
26
|
+
<img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
|
|
27
|
+
<img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
|
|
28
|
+
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Why?
|
|
34
|
+
|
|
35
|
+
"This request took 400 ms" is a start; "the listeners took 30, the queries 300, and the template
|
|
36
|
+
the rest" is an answer. Getting there takes every piece of work timed under a name, a kind, and
|
|
37
|
+
one shared starting point, so the pieces can be laid side by side on a timeline.
|
|
38
|
+
|
|
39
|
+
- ⏱️ **Events by name.** Start and stop `"load"`; run it three times and it is one event with
|
|
40
|
+
three periods.
|
|
41
|
+
- 🏷️ **Categories.** Each event says what kind of work it is — `"orm"`, `"event_listener"`,
|
|
42
|
+
`"template"` — so a timeline can colour and filter by kind.
|
|
43
|
+
- 🪆 **Sections.** Open one per request: every event in it is measured from the section's own
|
|
44
|
+
origin, and the section is stored under an id once it closes.
|
|
45
|
+
- 🔀 **Concurrent units of work stay apart.** What is timed belongs to the calling context, so
|
|
46
|
+
two requests served at once each see only their own events.
|
|
47
|
+
- 🕰️ **A clock you can freeze.** Readings come from an
|
|
48
|
+
[xtr-clock](../xtr-clock) clock — monotonic by default, a `MockClock` in a test.
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
uv add xtr-stopwatch
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Requires Python 3.14.4+.
|
|
57
|
+
|
|
58
|
+
## Quick start
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from xtr_stopwatch import Stopwatch
|
|
62
|
+
|
|
63
|
+
stopwatch = Stopwatch()
|
|
64
|
+
|
|
65
|
+
stopwatch.open_section()
|
|
66
|
+
event = stopwatch.start("load-books", "orm")
|
|
67
|
+
... # the work
|
|
68
|
+
event.stop()
|
|
69
|
+
stopwatch.stop_section("request-42")
|
|
70
|
+
|
|
71
|
+
for name, event in stopwatch.get_section_events("request-42").items():
|
|
72
|
+
print(name, event.category, f"{event.get_duration():.1f} ms")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Events
|
|
76
|
+
|
|
77
|
+
`start(name, category=None)` opens a period of the event `name` in the current section and
|
|
78
|
+
returns the event; `stop(name)` closes the period opened last. Starting a running event again
|
|
79
|
+
opens a second, nested period. `lap(name)` closes one period and opens the next at the same
|
|
80
|
+
instant.
|
|
81
|
+
|
|
82
|
+
Keep the event `start` returns and stop *it* when the code that started it is the code that
|
|
83
|
+
ends it: the object stays the same even if the context is [reset](#units-of-work) in between,
|
|
84
|
+
so its period still closes.
|
|
85
|
+
|
|
86
|
+
| `StopwatchEvent` | Answers |
|
|
87
|
+
|---|---|
|
|
88
|
+
| `name`, `category`, `origin` | what it is called, what kind of work it is (`"default"` when none was given), the instant its readings count from — milliseconds since the epoch |
|
|
89
|
+
| `get_periods()` | every closed period: `start`, `end`, `duration`, in milliseconds from `origin` |
|
|
90
|
+
| `get_start_time()`, `get_end_time()` | when it first began, when its last closed period ended |
|
|
91
|
+
| `get_duration()` | the time spent in every period, open ones counted up to now |
|
|
92
|
+
| `is_started()`, `ensure_stopped()` | whether a period is open; close every open one |
|
|
93
|
+
| `to_dict()` | all of the above as plain data, for JSON |
|
|
94
|
+
|
|
95
|
+
## Sections
|
|
96
|
+
|
|
97
|
+
`open_section()` opens a section inside the current one; `stop_section(section_id)` closes the
|
|
98
|
+
current section and stores it under `section_id`, after which
|
|
99
|
+
`get_section_events(section_id)` returns its events by name. The events of a section all count
|
|
100
|
+
from the instant it opened, which is what puts them on one axis.
|
|
101
|
+
|
|
102
|
+
Opening a section starts `SECTION_EVENT` (`"__section__"`) in it and `SECTION_CHILD_EVENT`
|
|
103
|
+
(`"__section__.child"`, category `"section"`) in its parent, and closing it stops both — the
|
|
104
|
+
section's own duration is one of its events, and the parent records where the child sat in its
|
|
105
|
+
own time. `open_section(section_id)` reopens a stopped child of the current section, adding a
|
|
106
|
+
period to it.
|
|
107
|
+
|
|
108
|
+
Outside any section, events go to the root section, stored as `ROOT` (`"__root__"`), whose
|
|
109
|
+
events each count from the instant they first started.
|
|
110
|
+
|
|
111
|
+
`has_open_section()` tells whether the calling context has a section open, without starting
|
|
112
|
+
anything. Code that times what it does on a stopwatch it shares — every listener a dispatcher
|
|
113
|
+
runs — asks it first, so a context no unit of work times in, a worker's own loop, never
|
|
114
|
+
accumulates events.
|
|
115
|
+
|
|
116
|
+
## Units of work
|
|
117
|
+
|
|
118
|
+
Everything a stopwatch times belongs to the calling context — the same `contextvars` mechanism
|
|
119
|
+
that keeps a server's concurrent requests apart — so
|
|
120
|
+
one stopwatch shared by a whole application gives every request only its own sections. Code
|
|
121
|
+
run in a copy of that context, such as a synchronous function a worker thread runs, times into
|
|
122
|
+
the same sections.
|
|
123
|
+
|
|
124
|
+
`reset()` starts the calling context afresh, with nothing timed. A unit of work calls it as it
|
|
125
|
+
begins; other contexts are untouched.
|
|
126
|
+
|
|
127
|
+
## Testing
|
|
128
|
+
|
|
129
|
+
Freeze time with [xtr-clock](../xtr-clock)'s `MockClock` and move it with `sleep`:
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
from xtr_clock import MockClock
|
|
133
|
+
from xtr_stopwatch import Stopwatch
|
|
134
|
+
|
|
135
|
+
clock = MockClock("2026-01-01 00:00:00")
|
|
136
|
+
stopwatch = Stopwatch(clock)
|
|
137
|
+
|
|
138
|
+
stopwatch.start("load")
|
|
139
|
+
clock.sleep(0.004)
|
|
140
|
+
assert stopwatch.stop("load").get_duration() == 4.0
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Errors
|
|
144
|
+
|
|
145
|
+
Everything this library raises derives from `StopwatchError`.
|
|
146
|
+
|
|
147
|
+
| Error | Raised when |
|
|
148
|
+
|---|---|
|
|
149
|
+
| `EventNotStartedError` | an event is stopped or lapped while it is not running |
|
|
150
|
+
| `UnknownEventError` | `get_event` names an event the current section never started; also a `LookupError` |
|
|
151
|
+
| `NoStartedSectionError` | `stop_section` is called with only the root open |
|
|
152
|
+
| `UnknownSectionError` | `open_section(section_id)` names no child of the current section; also a `LookupError` |
|
|
153
|
+
|
|
154
|
+
## Layout
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
xtr_stopwatch/
|
|
158
|
+
├── stopwatch.py Stopwatch, the sections of the calling context
|
|
159
|
+
├── section.py Section, the events sharing one origin
|
|
160
|
+
├── stopwatch_event.py StopwatchEvent, one named thing and its periods
|
|
161
|
+
├── stopwatch_period.py StopwatchPeriod, one start and its stop
|
|
162
|
+
└── exception/ StopwatchError, the root of everything this library raises
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Development
|
|
166
|
+
|
|
167
|
+
Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
|
|
168
|
+
`packages/xtr-stopwatch`; run the commands below from there.
|
|
169
|
+
|
|
170
|
+
```sh
|
|
171
|
+
uv sync
|
|
172
|
+
uv run ruff check
|
|
173
|
+
uv run ruff format --check
|
|
174
|
+
uv run basedpyright
|
|
175
|
+
uv run ty check
|
|
176
|
+
uv run pytest
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
xtr_stopwatch/__init__.py,sha256=aRDm4qQ-jg_TFmmkQ4l6826DLNFXO52jsRd0j7mOkrQ,1635
|
|
2
|
+
xtr_stopwatch/_milliseconds.py,sha256=iAxUlevuT6lf6jug3Hi2GFqrkQbRPz_ZeawhYc4N8i0,454
|
|
3
|
+
xtr_stopwatch/exception/__init__.py,sha256=hztT1FVZqfRvQ_wTVCCHwz_34vFfSH1kvE0RCUkMA0s,520
|
|
4
|
+
xtr_stopwatch/exception/event_not_started_error.py,sha256=LxcXg2Uep7XcamLg-AexTeOf08BaeBJvoFlXt2Qr2k4,664
|
|
5
|
+
xtr_stopwatch/exception/no_started_section_error.py,sha256=Ax-EUIw13HPNco0DglAK0GyLtoHR744FmJf5QmNRNFg,725
|
|
6
|
+
xtr_stopwatch/exception/stopwatch_error.py,sha256=shT1cMW9Kptq_EGGM0JtgfgtlXY3IigYoRmrHmQx9Wo,430
|
|
7
|
+
xtr_stopwatch/exception/unknown_event_error.py,sha256=B1xxEHq54TgvWIcMpQwlEwrxR49d-wrcnlg-PphOl4I,623
|
|
8
|
+
xtr_stopwatch/exception/unknown_section_error.py,sha256=dQDJes89Fz3PV0_AwDFwnfmKpXtSb_yO4bGZq2GemK8,964
|
|
9
|
+
xtr_stopwatch/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
|
+
xtr_stopwatch/section.py,sha256=IO751NThel5K53duvizImWDxoloetNubUqo7S5NtuPI,4687
|
|
11
|
+
xtr_stopwatch/stopwatch.py,sha256=zlgD_fk5AbvJTTJkKFjzzKR7DfC4x6jxGi8OAsUc708,7970
|
|
12
|
+
xtr_stopwatch/stopwatch_event.py,sha256=9T12qCaqzGSL8IbesknWCnSEUe7h7Vp4Sri3QZeVM28,5423
|
|
13
|
+
xtr_stopwatch/stopwatch_period.py,sha256=TH8PT3pBA40fNzuFeq1XWfRkssANGf9u5dmZ7WDdm58,1498
|
|
14
|
+
xtr_stopwatch-7.0.0.dist-info/licenses/LICENSE,sha256=N6mW05zc_GYFRdNovzY4zFuWyBKavZLcouPmDOBHK5o,1106
|
|
15
|
+
xtr_stopwatch-7.0.0.dist-info/WHEEL,sha256=G_-rrEibZr7OEM7zdH1dSO6s-VdjfnS5PA4CLAdI9Vw,80
|
|
16
|
+
xtr_stopwatch-7.0.0.dist-info/METADATA,sha256=H1CqsNNQR2JcsBvkc6oEZ4KtC0hMcC3NjjpWE2q44hs,7013
|
|
17
|
+
xtr_stopwatch-7.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 xterr
|
|
4
|
+
Copyright (c) 2004-present Fabien Potencier
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|