-
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
70e212e
commit 24162c8
Showing
7 changed files
with
103 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
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,80 @@ | ||
import functools | ||
import logging | ||
import time | ||
import typing | ||
from datetime import timedelta | ||
|
||
from packaging.requirements import Requirement | ||
from packaging.version import Version | ||
|
||
from . import context | ||
|
||
|
||
def timeit(description: str) -> typing.Callable: | ||
def timeit_decorator(func: typing.Callable) -> typing.Callable: | ||
@functools.wraps(func) | ||
def wrapper_timeit( | ||
*, | ||
ctx: context.WorkContext, | ||
req: Requirement | None = None, | ||
**kwargs: typing.Any, | ||
) -> typing.Any: | ||
ctx.time_description_store[func.__name__] = description | ||
|
||
start = time.perf_counter() | ||
ret = func(ctx=ctx, 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}: {func.__name__} took {timedelta(seconds=runtime)} to {description}" | ||
) | ||
else: | ||
logger.debug( | ||
f"{func.__name__} took {timedelta(seconds=runtime)} to {description}" | ||
) | ||
|
||
if req and version: | ||
# store total time spent calling that function for a particular version of that req | ||
ctx.time_store[f"{req.name}=={version}"][func.__name__] = ( | ||
ctx.time_store[f"{req.name}=={version}"].get(func.__name__, 0) | ||
+ runtime | ||
) | ||
|
||
return ret | ||
|
||
return wrapper_timeit | ||
|
||
return timeit_decorator | ||
|
||
|
||
def summarize(ctx: context.WorkContext, prefix: str) -> None: | ||
logger = logging.getLogger(__name__) | ||
for req in sorted(ctx.time_store.keys()): | ||
total_time = sum(ctx.time_store[req].values()) | ||
log = f"{prefix} {req} took {timedelta(seconds=total_time)} total" | ||
for fn_name, time_taken in ctx.time_store[req].items(): | ||
log += f", {timedelta(seconds=time_taken)} to {ctx.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