Skip to content

Commit

Permalink
LinuxTimeCM
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry1 committed Apr 14, 2023
1 parent 1b8a604 commit ccdd7a3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#https://peps.python.org/pep-0621/
[tool.poetry]
name = "valuefragments"
version = "0.2.10"
version = "0.2.11"
description = "Testing installation of Package"
authors = ["Ebeling, Dr. Bastian <bastian.ebeling@web.de>"]

Expand Down
1 change: 1 addition & 0 deletions src/valuefragments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
__all__: list[str] = [
"NoOutput",
"TimingCM",
"LinuxTimeCM",
"timing_wall",
"timing_resource",
"timing_psutil",
Expand Down
104 changes: 104 additions & 0 deletions src/valuefragments/contextmanagers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import sys
import time
from abc import ABC
from types import TracebackType
from typing import Any, Optional, TextIO, Type
Expand Down Expand Up @@ -43,6 +44,109 @@ def flush(self: NoOutput) -> None:
"""Flush attribute: Needed but does nothing."""


try:
import resource
except ImportError:
ic("resource is not available")
else:

class LinuxTimeCM:
"""
Use this as a context manager for getting timing details like with linux time.
at the moment it is needed to be instantiated with parenthesis as in
with LinuxTimeCM():
hopefully I can remove that for further simplification
"""

before: float | Literal[0]
childbefore: resource.struct_rusage
selfbefore: resource.struct_rusage
selfafter: resource.struct_rusage
childafter: resource.struct_rusage
after: float | Literal[0]

def __init__(self) -> None: # : TimingCM
"""Prepare (type) variables."""
ic("Prepared to run with LinuxTime -> __init__")

def __enter__(self: LinuxTimeCM) -> LinuxTimeCM:
"""Save startup timing information."""
self.before = time.monotonic()
self.childbefore = resource.getrusage(resource.RUSAGE_CHILDREN)
self.selfbefore = resource.getrusage(resource.RUSAGE_SELF)
ic("Prepared to run with LinuxTime -> __enter__")
return self

def __exit__(
self: LinuxTimeCM,
_exc_type: Optional[Type[BaseException]],
_exc_value: Optional[BaseException],
_exc_traceback: Optional[TracebackType],
) -> Optional[bool]:
"""Retrieve end timing information and print."""
# Check if any (loky) backend is still open and if, close

try:
# pylint: disable=import-outside-toplevel
from joblib.externals.loky import get_reusable_executor
except ModuleNotFoundError:
pass
else:
get_reusable_executor().shutdown()
self.selfafter = resource.getrusage(resource.RUSAGE_SELF)
self.childafter = resource.getrusage(resource.RUSAGE_CHILDREN)
self.after = time.monotonic()
if (
self.childbefore
and self.selfbefore
and self.selfafter
and self.childafter
and self.before
and self.after
):
WALLtime: float = self.after - self.before
USERtime: float = (
self.selfafter.ru_utime
- self.selfbefore.ru_utime
+ self.childafter.ru_utime
- self.childbefore.ru_utime
)
SYStime: float = (
self.selfafter.ru_stime
- self.selfbefore.ru_stime
+ self.childafter.ru_stime
- self.childbefore.ru_stime
)
print(
"user: ",
self.selfafter.ru_utime - self.selfbefore.ru_utime,
"+",
self.childafter.ru_utime - self.childbefore.ru_utime,
"=",
USERtime,
"[s]",
)
print(
"system",
self.selfafter.ru_stime - self.selfbefore.ru_stime,
"+",
self.childafter.ru_stime - self.childbefore.ru_stime,
"=",
SYStime,
"[s]",
)
print(
"real: ",
WALLtime,
"[s] beeing",
100 * (USERtime + SYStime) / WALLtime,
"% load",
)
ic("Ended to run with Timing -> __exit__")
return True


class TimingCM: # pyre-ignore[13]
"""
Use this as a context manager for getting timing details.
Expand Down

0 comments on commit ccdd7a3

Please sign in to comment.