Skip to content

Commit

Permalink
Add TickTock method for adding external measurements
Browse files Browse the repository at this point in the history
Resolves #118
  • Loading branch information
asgerius committed Dec 25, 2022
1 parent cff259c commit 2c3de50
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Moved `is_windows` to static `OS` attribute, which also contains `is_mac` and `is_linux`
- Added `HardwareInfo`, which contains a bunch of system hardware information
- `Figure` now automatically creates necessary subdirectories
- Added `add_external_measurements` method to `TickTock`

### Bug fixes

Expand Down
17 changes: 15 additions & 2 deletions pelutils/ticktock.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from copy import deepcopy
from time import perf_counter
from typing import Generator, Iterable
from typing import Generator, Optional

from pelutils import thousands_seperators
from pelutils.format import Table
Expand Down Expand Up @@ -189,7 +189,7 @@ def end_profile(self, name: str=None) -> float:
if name is not None and name != self._profile_stack[-1].name:
raise NameError(f"Expected to pop profile '{self._profile_stack[-1].name}', received '{name}'")
nhits = self._nhits.pop()
self._profile_stack[-1].hits.extend([dt/nhits] * nhits)
self._profile_stack[-1]._hits += [dt/nhits] * nhits
self._profile_stack.pop()
return dt

Expand All @@ -199,6 +199,19 @@ def reset(self):
raise TickTockException("Cannot reset TickTock while profiling is active")
self.__init__()

def add_external_measurements(self, name: Optional[str], time: float, *, hits=1):
""" Allows adding data to a (new) profile with given time spread over given hits.
If name is a string, it will act like .profile(name). If it is none, the current
active profile will be used. """
measurements = [time / hits] * hits
if name is not None:
self.profile(name, hits=hits)
profile = self._profile_stack[-1]
self.end_profile()
profile._hits[-hits:] = measurements
else:
self._profile_stack[-1]._hits += measurements

def fuse(self, tt: TickTock):
""" Fuses a TickTock instance into self """
if len(self._profile_stack) or len(tt._profile_stack):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_ticktock.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ def test_profiles_with_same_name():
assert profiles[2].depth == 1
assert len(profiles[2]._hits) == 4

def test_add_external_measurements():
tt = TickTock()
with tt.profile("a"):
tt.add_external_measurements("b", 5, hits=2)
with tt.profile("b"):
tt.add_external_measurements(None, 3, hits=4)
tt.add_external_measurements(None, 5)
tt.add_external_measurements("a", 5)

for profile in tt.profiles:
if profile.name == "a":
assert len(profile.hits) == 3
elif profile.name == "b":
assert len(profile.hits) == 7

def test_print(capfd: pytest.CaptureFixture):
tt = TickTock()
with tt.profile("a"):
Expand Down

0 comments on commit 2c3de50

Please sign in to comment.