Skip to content

Commit

Permalink
feat: adds timer decorator to facilitate debugging (#1917)
Browse files Browse the repository at this point in the history
* feat: adds timer decorator to sessions

* updates _calculate_duration function

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
chalmerlowe and gcf-owl-bot[bot] committed May 16, 2024
1 parent b739596 commit ea750e0
Showing 1 changed file with 37 additions and 0 deletions.
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)
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

0 comments on commit ea750e0

Please sign in to comment.