Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds timer decorator to facilitate debugging #1917

Merged
merged 3 commits into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

from __future__ import absolute_import

from functools import wraps
import pathlib
import os
import re
import shutil
import nox
import time


MYPY_VERSION = "mypy==1.6.1"
Expand All @@ -40,6 +42,27 @@
UNIT_TEST_PYTHON_VERSIONS = ["3.7", "3.8", "3.12"]
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()


def _calculate_duration(func):
"""This decorator prints the execution time for the decorated function."""

@wraps(func)
def wrapper(*args, **kwargs):
start = time.monotonic()
result = func(*args, **kwargs)
end = time.monotonic()
total_seconds = round(end - start)
chalmerlowe marked this conversation as resolved.
Show resolved Hide resolved
hours = total_seconds // 3600 # Integer division to get hours
remaining_seconds = total_seconds % 3600 # Modulo to find remaining seconds
minutes = remaining_seconds // 60
seconds = remaining_seconds % 60
human_time = f"{hours:}:{minutes:0>2}:{seconds:0>2}"
print(f"Session ran in {total_seconds} seconds ({human_time})")
return result

return wrapper


# 'docfx' is excluded since it only needs to run in 'docs-presubmit'
nox.options.sessions = [
"unit_noextras",
Expand Down Expand Up @@ -105,13 +128,15 @@ def default(session, install_extras=True):


@nox.session(python=UNIT_TEST_PYTHON_VERSIONS)
@_calculate_duration
def unit(session):
"""Run the unit test suite."""

default(session)


@nox.session(python=[UNIT_TEST_PYTHON_VERSIONS[0], UNIT_TEST_PYTHON_VERSIONS[-1]])
@_calculate_duration
def unit_noextras(session):
"""Run the unit test suite."""

Expand All @@ -129,6 +154,7 @@ def unit_noextras(session):


@nox.session(python=DEFAULT_PYTHON_VERSION)
@_calculate_duration
def mypy(session):
"""Run type checks with mypy."""

Expand All @@ -147,6 +173,7 @@ def mypy(session):


@nox.session(python=DEFAULT_PYTHON_VERSION)
@_calculate_duration
def pytype(session):
"""Run type checks with pytype."""
# An indirect dependecy attrs==21.1.0 breaks the check, and installing a less
Expand All @@ -161,6 +188,7 @@ def pytype(session):


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
@_calculate_duration
def system(session):
"""Run the system test suite."""

Expand Down Expand Up @@ -209,6 +237,7 @@ def system(session):


@nox.session(python=DEFAULT_PYTHON_VERSION)
@_calculate_duration
def mypy_samples(session):
"""Run type checks with mypy."""

Expand Down Expand Up @@ -244,6 +273,7 @@ def mypy_samples(session):


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
@_calculate_duration
def snippets(session):
"""Run the snippets test suite."""

Expand Down Expand Up @@ -279,6 +309,7 @@ def snippets(session):


@nox.session(python=DEFAULT_PYTHON_VERSION)
@_calculate_duration
def cover(session):
"""Run the final coverage report.

Expand All @@ -292,6 +323,7 @@ def cover(session):


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
@_calculate_duration
def prerelease_deps(session):
"""Run all tests with prerelease versions of dependencies installed.

Expand Down Expand Up @@ -382,6 +414,7 @@ def prerelease_deps(session):


@nox.session(python=DEFAULT_PYTHON_VERSION)
@_calculate_duration
def lint(session):
"""Run linters.

Expand All @@ -400,6 +433,7 @@ def lint(session):


@nox.session(python=DEFAULT_PYTHON_VERSION)
@_calculate_duration
def lint_setup_py(session):
"""Verify that setup.py is valid (including RST check)."""

Expand All @@ -408,6 +442,7 @@ def lint_setup_py(session):


@nox.session(python=DEFAULT_PYTHON_VERSION)
@_calculate_duration
def blacken(session):
"""Run black.
Format code to uniform standard.
Expand All @@ -418,6 +453,7 @@ def blacken(session):


@nox.session(python="3.9")
@_calculate_duration
def docs(session):
"""Build the docs."""

Expand Down Expand Up @@ -454,6 +490,7 @@ def docs(session):


@nox.session(python="3.10")
@_calculate_duration
def docfx(session):
"""Build the docfx yaml files for this library."""

Expand Down