123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import time
- from colorama import Fore
- class TimeLogger:
- '''
- Class for keeping track of and logging execution times
- for suite actions
- '''
- def __init__(self, config):
- self.log = config.log
- self.start = time.time()
- self.benchmarking_start = 0
- self.benchmarking_total = 0
- self.database_starting = 0
- self.database_started = 0
- self.build_start = 0
- self.build_total = 0
- self.test_started = 0
- self.accepting_requests = 0
- self.test_start = 0
- self.test_total = 0
- self.verify_start = 0
- self.verify_total = 0
- self.build_logs = []
- @staticmethod
- def output(sec):
- output = ""
- h = sec // 3600
- m = (sec // 60) % 60
- s = sec % 60
- if h > 0:
- output = "%sh" % h
- if m > 0:
- output = output + "%sm " % m
- output = output + "%ss" % s
- return output
- def mark_starting_database(self):
- self.database_starting = time.time()
- def mark_started_database(self):
- self.database_started = int(time.time() - self.database_starting)
- def log_database_start_time(self, log_prefix, file):
- self.log("Time starting database: %s" % TimeLogger.output(
- self.database_started),
- prefix=log_prefix,
- file=file,
- color=Fore.YELLOW)
- def mark_benchmarking_start(self):
- self.benchmarking_start = time.time()
- def log_benchmarking_end(self, log_prefix, file):
- total = int(time.time() - self.benchmarking_start)
- self.benchmarking_total = self.benchmarking_total + total
- self.log("Benchmarking time: %s" % TimeLogger.output(total),
- prefix=log_prefix,
- file=file,
- color=Fore.YELLOW)
- def mark_build_start(self):
- self.build_start = time.time()
- def time_since_start(self):
- return time.time() - self.build_start
- def log_build_end(self, log_prefix, file):
- total = int(time.time() - self.build_start)
- self.build_total = self.build_total + total
- log_str = "Build time: %s" % TimeLogger.output(total)
- self.build_logs.append({'log_prefix': log_prefix, 'str': log_str})
- self.log(log_str, prefix=log_prefix, file=file, color=Fore.YELLOW)
- def log_build_flush(self, file):
- for b_log in self.build_logs:
- self.log(b_log['str'],
- prefix=b_log['log_prefix'],
- file=file,
- color=Fore.YELLOW)
- self.build_logs = []
- def mark_test_starting(self):
- self.test_started = time.time()
- def mark_test_accepting_requests(self):
- self.accepting_requests = int(time.time() - self.test_started)
- def log_test_accepting_requests(self, log_prefix, file):
- self.log("Time until accepting requests: %s" % TimeLogger.output(
- self.accepting_requests),
- prefix=log_prefix,
- file=file,
- color=Fore.YELLOW)
- def mark_test_start(self):
- self.test_start = time.time()
- def log_test_end(self, log_prefix, file):
- total = int(time.time() - self.test_start)
- self.log("Total test time: %s" % TimeLogger.output(total),
- prefix=log_prefix,
- file=file,
- color=Fore.YELLOW)
- self.log("Total time building so far: %s" % TimeLogger.output(
- self.build_total),
- prefix="tfb: ",
- file=file,
- color=Fore.YELLOW)
- self.log("Total time verifying so far: %s" % TimeLogger.output(
- self.verify_total),
- prefix="tfb: ",
- file=file,
- color=Fore.YELLOW)
- if self.benchmarking_total > 0:
- self.log("Total time benchmarking so far: %s" % TimeLogger.output(
- self.benchmarking_total),
- prefix="tfb: ",
- file=file,
- color=Fore.YELLOW)
- running_time = int(time.time() - self.start)
- self.log("Total execution time so far: %s" %
- TimeLogger.output(running_time),
- prefix="tfb: ",
- file=file,
- color=Fore.YELLOW)
- def mark_verify_start(self):
- self.verify_start = time.time()
- def log_verify_end(self, log_prefix, file):
- total = int(time.time() - self.verify_start)
- self.verify_total = self.verify_total + total
- self.log("Verify time: %s" % TimeLogger.output(total),
- prefix=log_prefix,
- file=file,
- color=Fore.YELLOW)
|