Skip to content

Commit ea750e0

Browse files
feat: adds timer decorator to facilitate debugging (#1917)
* 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>
1 parent b739596 commit ea750e0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

noxfile.py

+37
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
from __future__ import absolute_import
1616

17+
from functools import wraps
1718
import pathlib
1819
import os
1920
import re
2021
import shutil
2122
import nox
23+
import time
2224

2325

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

45+
46+
def _calculate_duration(func):
47+
"""This decorator prints the execution time for the decorated function."""
48+
49+
@wraps(func)
50+
def wrapper(*args, **kwargs):
51+
start = time.monotonic()
52+
result = func(*args, **kwargs)
53+
end = time.monotonic()
54+
total_seconds = round(end - start)
55+
hours = total_seconds // 3600 # Integer division to get hours
56+
remaining_seconds = total_seconds % 3600 # Modulo to find remaining seconds
57+
minutes = remaining_seconds // 60
58+
seconds = remaining_seconds % 60
59+
human_time = f"{hours:}:{minutes:0>2}:{seconds:0>2}"
60+
print(f"Session ran in {total_seconds} seconds ({human_time})")
61+
return result
62+
63+
return wrapper
64+
65+
4366
# 'docfx' is excluded since it only needs to run in 'docs-presubmit'
4467
nox.options.sessions = [
4568
"unit_noextras",
@@ -105,13 +128,15 @@ def default(session, install_extras=True):
105128

106129

107130
@nox.session(python=UNIT_TEST_PYTHON_VERSIONS)
131+
@_calculate_duration
108132
def unit(session):
109133
"""Run the unit test suite."""
110134

111135
default(session)
112136

113137

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

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

130155

131156
@nox.session(python=DEFAULT_PYTHON_VERSION)
157+
@_calculate_duration
132158
def mypy(session):
133159
"""Run type checks with mypy."""
134160

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

148174

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

162189

163190
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
191+
@_calculate_duration
164192
def system(session):
165193
"""Run the system test suite."""
166194

@@ -209,6 +237,7 @@ def system(session):
209237

210238

211239
@nox.session(python=DEFAULT_PYTHON_VERSION)
240+
@_calculate_duration
212241
def mypy_samples(session):
213242
"""Run type checks with mypy."""
214243

@@ -244,6 +273,7 @@ def mypy_samples(session):
244273

245274

246275
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
276+
@_calculate_duration
247277
def snippets(session):
248278
"""Run the snippets test suite."""
249279

@@ -279,6 +309,7 @@ def snippets(session):
279309

280310

281311
@nox.session(python=DEFAULT_PYTHON_VERSION)
312+
@_calculate_duration
282313
def cover(session):
283314
"""Run the final coverage report.
284315
@@ -292,6 +323,7 @@ def cover(session):
292323

293324

294325
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
326+
@_calculate_duration
295327
def prerelease_deps(session):
296328
"""Run all tests with prerelease versions of dependencies installed.
297329
@@ -382,6 +414,7 @@ def prerelease_deps(session):
382414

383415

384416
@nox.session(python=DEFAULT_PYTHON_VERSION)
417+
@_calculate_duration
385418
def lint(session):
386419
"""Run linters.
387420
@@ -400,6 +433,7 @@ def lint(session):
400433

401434

402435
@nox.session(python=DEFAULT_PYTHON_VERSION)
436+
@_calculate_duration
403437
def lint_setup_py(session):
404438
"""Verify that setup.py is valid (including RST check)."""
405439

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

409443

410444
@nox.session(python=DEFAULT_PYTHON_VERSION)
445+
@_calculate_duration
411446
def blacken(session):
412447
"""Run black.
413448
Format code to uniform standard.
@@ -418,6 +453,7 @@ def blacken(session):
418453

419454

420455
@nox.session(python="3.9")
456+
@_calculate_duration
421457
def docs(session):
422458
"""Build the docs."""
423459

@@ -454,6 +490,7 @@ def docs(session):
454490

455491

456492
@nox.session(python="3.10")
493+
@_calculate_duration
457494
def docfx(session):
458495
"""Build the docfx yaml files for this library."""
459496

0 commit comments

Comments
 (0)