-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
measure time for different steps in bootstrapping and build-sequence
Signed-off-by: Shubh Bapna <[email protected]>
- Loading branch information
1 parent
0fdcd46
commit 61ff0a4
Showing
6 changed files
with
93 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import functools | ||
import logging | ||
import time | ||
import typing | ||
from collections import defaultdict | ||
from datetime import timedelta | ||
|
||
from packaging.requirements import Requirement | ||
from packaging.version import Version | ||
|
||
_time_store: dict[str, dict[str, float]] = defaultdict(dict[str, float]) | ||
_time_description_store: dict[str, str] = {} | ||
|
||
|
||
def timeit(description: str): | ||
def timeit_decorator(func: typing.Callable) -> typing.Callable: | ||
_time_description_store[func.__name__] = description | ||
|
||
@functools.wraps(func) | ||
def wrapper_timeit( | ||
*, req: Requirement | None = None, **kwargs: typing.Any | ||
) -> typing.Any: | ||
start = time.perf_counter() | ||
ret = func(req=req, **kwargs) | ||
end = time.perf_counter() | ||
# get the logger for the module from which this function was called | ||
logger = logging.getLogger(func.__module__) | ||
version = ( | ||
kwargs.get("version") | ||
or kwargs.get("dist_version") | ||
or kwargs.get("resolved_version") | ||
) | ||
if not version: | ||
version = _extract_version_from_return(ret) | ||
|
||
runtime = end - start | ||
|
||
if req: | ||
logger.debug( | ||
f"{req.name}: took {timedelta(seconds=runtime)} to {description}" | ||
) | ||
else: | ||
logger.debug(f"took {timedelta(seconds=runtime)} to {description}") | ||
|
||
if req and version: | ||
# store total time spent calling that function for a particular version of that req | ||
_time_store[f"{req.name}=={version}"][func.__name__] = ( | ||
_time_store[f"{req.name}=={version}"].get(func.__name__, 0) | ||
+ runtime | ||
) | ||
|
||
return ret | ||
|
||
return wrapper_timeit | ||
|
||
return timeit_decorator | ||
|
||
|
||
def summarize(prefix: str) -> None: | ||
logger = logging.getLogger(__name__) | ||
for req in sorted(_time_store.keys()): | ||
total_time = sum(_time_store[req].values()) | ||
log = f"{prefix} {req} took {timedelta(seconds=total_time)} total" | ||
for fn_name, time_taken in _time_store[req].items(): | ||
log += f", {timedelta(seconds=time_taken)} to {_time_description_store[fn_name]}" | ||
logger.info(log) | ||
|
||
|
||
def _extract_version_from_return(ret: typing.Any) -> Version | None: | ||
try: | ||
for r in ret: | ||
if isinstance(r, Version): | ||
return r | ||
except Exception: | ||
if isinstance(ret, Version): | ||
return ret | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters