Add a decorator to TimerMetrics and wrap most of the functions.

master
Simon Quigley 5 years ago
parent 28c7f2b397
commit 1b502ee4e4

@ -70,6 +70,7 @@ class Generator:
return metadata_conf return metadata_conf
@timer.run("Parse the metadata")
def parse_metadata(self): def parse_metadata(self):
"""Parse the data pulled from clone_metadata """Parse the data pulled from clone_metadata
@ -98,6 +99,7 @@ class Generator:
return metadata_conf["repositories"] return metadata_conf["repositories"]
@timer.run("Auth to Jenkins")
def auth_jenkins_server(self): def auth_jenkins_server(self):
"""Authenticate to the Jenkins server """Authenticate to the Jenkins server
@ -105,8 +107,6 @@ class Generator:
set in Jenkins. These need to be private, so they are defined in the set in Jenkins. These need to be private, so they are defined in the
system-wide Jenkins credential storage. system-wide Jenkins credential storage.
""" """
timer.start("Authenticate to the server")
# Load the API values from the environment variables # Load the API values from the environment variables
api_site = getenv("API_SITE") api_site = getenv("API_SITE")
api_user = getenv("API_USER") api_user = getenv("API_USER")
@ -118,8 +118,6 @@ class Generator:
# Authenticate to the server # Authenticate to the server
server = Jenkins(api_site, username=api_user, password=api_key) server = Jenkins(api_site, username=api_user, password=api_key)
timer.stop("Authenticate to the server")
return server return server
def load_config(self, job_type, data=None): def load_config(self, job_type, data=None):
@ -179,6 +177,7 @@ class Generator:
return package_config return package_config
@timer.run("Get existing jobs")
def get_existing_jenkins_jobs(self, server): def get_existing_jenkins_jobs(self, server):
"""This returns a tuple of all existing Jenkins jobs """This returns a tuple of all existing Jenkins jobs
@ -233,6 +232,7 @@ class Generator:
total_rel = set() total_rel = set()
for package in metadata: for package in metadata:
timer.start("Merger job creation")
# Create the merger jobs first # Create the merger jobs first
job_name = "merger_" + package["name"] job_name = "merger_" + package["name"]
package_config = self.load_config("merger", package) package_config = self.load_config("merger", package)
@ -247,7 +247,9 @@ class Generator:
else: else:
view = server.views.create("merger") view = server.views.create("merger")
view.add_job(job_name) view.add_job(job_name)
timer.stop("Merger job creation")
timer.start("Release job creation")
for release in package["releases"]: for release in package["releases"]:
# Add the release to the total release set, which is used to # Add the release to the total release set, which is used to
# generate the management jobs # generate the management jobs
@ -272,7 +274,9 @@ class Generator:
view = server.views.create(viewname) view = server.views.create(viewname)
view.add_job(job_name) view.add_job(job_name)
timer.stop("Release job creation")
timer.start("Management job creation")
# From here on out, the same template is used # From here on out, the same template is used
package_config = self.load_config("release-mgmt") package_config = self.load_config("release-mgmt")
@ -302,7 +306,10 @@ class Generator:
view = server.views["mgmt"] view = server.views["mgmt"]
view.add_job("merger") view.add_job("merger")
timer.stop("Management job creation")
if __name__ == "__main__": if __name__ == "__main__":
generator = Generator() generator = Generator()
print(generator.create_jenkins_jobs()) print(generator.create_jenkins_jobs())
timer.display()

@ -17,6 +17,7 @@
import time import time
import tabulate import tabulate
import sys
from tabulate import tabulate from tabulate import tabulate
tabulate.PRESERVE_WHITESPACE = True tabulate.PRESERVE_WHITESPACE = True
@ -100,6 +101,24 @@ class TimerMetrics:
cur_time = t_val - self.data[name]["start_time"] cur_time = t_val - self.data[name]["start_time"]
self.data[name]["total_time"] += cur_time self.data[name]["total_time"] += cur_time
def run(self, label):
"""Wrap a function inside a timer
This allows for the usage of a decorator on a function which
automatically and easily starts and ends a timer
"""
self.start(label)
def wrap(func):
def run_function(*args, **kwargs):
try:
return func(*args, **kwargs)
finally:
self.stop(label)
return run_function
return wrap
def display(self): def display(self):
"""Print a pretty(-ish) table with all of the data in it""" """Print a pretty(-ish) table with all of the data in it"""

Loading…
Cancel
Save