Skip to content

Commit d5474a9

Browse files
chore: Add decorator @calculate_duration (#1216)
* adds debugging to noxfile and tweaks to correct coverage * adds a decorator to help calculate the duration of a nox session * Minor tweaks. * adds back in a pip freeze that was accidentally removed. * 🦉 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 632d6ef commit d5474a9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

noxfile.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
from __future__ import absolute_import
2020

21+
from functools import wraps
2122
import os
2223
import pathlib
2324
import re
2425
import shutil
26+
import time
2527
from typing import Dict, List
2628
import warnings
2729

@@ -100,6 +102,27 @@
100102

101103
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
102104

105+
106+
def _calculate_duration(func):
107+
"""This decorator prints the execution time for the decorated function."""
108+
109+
@wraps(func)
110+
def wrapper(*args, **kwargs):
111+
start = time.monotonic()
112+
result = func(*args, **kwargs)
113+
end = time.monotonic()
114+
total_seconds = round(end - start)
115+
hours = total_seconds // 3600 # Integer division to get hours
116+
remaining_seconds = total_seconds % 3600 # Modulo to find remaining seconds
117+
minutes = remaining_seconds // 60
118+
seconds = remaining_seconds % 60
119+
human_time = f"{hours:}:{minutes:0>2}:{seconds:0>2}"
120+
print(f"Session ran in {total_seconds} seconds ({human_time})")
121+
return result
122+
123+
return wrapper
124+
125+
103126
nox.options.sessions = [
104127
"unit",
105128
"system",
@@ -118,6 +141,7 @@
118141

119142

120143
@nox.session(python=DEFAULT_PYTHON_VERSION)
144+
@_calculate_duration
121145
def lint(session):
122146
"""Run linters.
123147
@@ -134,6 +158,7 @@ def lint(session):
134158

135159

136160
@nox.session(python=DEFAULT_PYTHON_VERSION)
161+
@_calculate_duration
137162
def blacken(session):
138163
"""Run black. Format code to uniform standard."""
139164
session.install(BLACK_VERSION)
@@ -144,6 +169,7 @@ def blacken(session):
144169

145170

146171
@nox.session(python=DEFAULT_PYTHON_VERSION)
172+
@_calculate_duration
147173
def format(session):
148174
"""
149175
Run isort to sort imports. Then run black
@@ -164,6 +190,7 @@ def format(session):
164190

165191

166192
@nox.session(python=DEFAULT_PYTHON_VERSION)
193+
@_calculate_duration
167194
def lint_setup_py(session):
168195
"""Verify that setup.py is valid (including RST check)."""
169196
session.install("docutils", "pygments")
@@ -203,6 +230,7 @@ def install_unittest_dependencies(session, *constraints):
203230
"protobuf_implementation",
204231
["python", "upb", "cpp"],
205232
)
233+
@_calculate_duration
206234
def unit(session, protobuf_implementation, install_extras=True):
207235
# Install all test dependencies, then install this package in-place.
208236

@@ -278,6 +306,7 @@ def install_systemtest_dependencies(session, *constraints):
278306

279307

280308
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
309+
@_calculate_duration
281310
def system(session):
282311
"""Run the system test suite."""
283312
constraints_path = str(
@@ -321,6 +350,7 @@ def system(session):
321350

322351

323352
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
353+
@_calculate_duration
324354
def system_noextras(session):
325355
"""Run the system test suite."""
326356
constraints_path = str(
@@ -366,6 +396,7 @@ def system_noextras(session):
366396

367397

368398
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS[-1])
399+
@_calculate_duration
369400
def compliance(session):
370401
"""Run the SQLAlchemy dialect-compliance system tests"""
371402
constraints_path = str(
@@ -418,6 +449,7 @@ def compliance(session):
418449

419450

420451
@nox.session(python=DEFAULT_PYTHON_VERSION)
452+
@_calculate_duration
421453
def cover(session):
422454
"""Run the final coverage report.
423455
@@ -431,6 +463,7 @@ def cover(session):
431463

432464

433465
@nox.session(python="3.10")
466+
@_calculate_duration
434467
def docs(session):
435468
"""Build the docs for this library."""
436469

@@ -468,6 +501,7 @@ def docs(session):
468501

469502

470503
@nox.session(python="3.10")
504+
@_calculate_duration
471505
def docfx(session):
472506
"""Build the docfx yaml files for this library."""
473507

@@ -520,6 +554,7 @@ def docfx(session):
520554
"protobuf_implementation",
521555
["python", "upb", "cpp"],
522556
)
557+
@_calculate_duration
523558
def prerelease_deps(session, protobuf_implementation):
524559
"""Run all tests with prerelease versions of dependencies installed."""
525560

0 commit comments

Comments
 (0)