time_logger.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import time
  2. from colorama import Fore, Style
  3. from toolset.utils.output_helper import log
  4. class TimeLogger:
  5. '''
  6. Class for keeping track of and logging execution times
  7. for suite actions
  8. '''
  9. def __init__(self):
  10. self.start = time.time()
  11. self.benchmarking_start = 0
  12. self.benchmarking_total = 0
  13. self.build_start = 0
  14. self.build_total = 0
  15. self.test_start = 0
  16. self.test_total = 0
  17. self.verify_start = 0
  18. self.verify_total = 0
  19. @staticmethod
  20. def output(sec):
  21. output = ""
  22. h = sec // 3600
  23. m = (sec // 60) % 60
  24. s = sec % 60
  25. if h > 0:
  26. output = "%sh" % h
  27. if m > 0:
  28. output = output + "%sm " % m
  29. output = output + "%ss" % s
  30. return output
  31. def log_benchmarking_start(self):
  32. self.benchmarking_start = time.time()
  33. def log_benchmarking_end(self, log_prefix, file):
  34. total = int(time.time() - self.benchmarking_start)
  35. self.benchmarking_total = self.benchmarking_total + total
  36. log("Total benchmarking time: %s" % TimeLogger.output(total),
  37. prefix=log_prefix,
  38. file=file,
  39. color=Fore.YELLOW)
  40. def log_build_start(self):
  41. self.build_start = time.time()
  42. def log_build_end(self, log_prefix, file):
  43. total = int(time.time() - self.build_start)
  44. self.build_total = self.build_total + total
  45. log("Total build time: %s" % TimeLogger.output(total),
  46. prefix=log_prefix,
  47. file=file,
  48. color=Fore.YELLOW)
  49. def log_test_start(self):
  50. self.test_start = time.time()
  51. def log_test_end(self, log_prefix, file):
  52. total = int(time.time() - self.test_start)
  53. log("Total test time: %s" % TimeLogger.output(total),
  54. prefix=log_prefix,
  55. file=file,
  56. color=Fore.YELLOW)
  57. log("Total time building so far: %s"
  58. % TimeLogger.output(self.build_total),
  59. prefix="tfb: ",
  60. file=file,
  61. color=Fore.YELLOW)
  62. log("Total time verifying so far: %s"
  63. % TimeLogger.output(self.verify_total),
  64. prefix="tfb: ",
  65. file=file,
  66. color=Fore.YELLOW)
  67. if self.benchmarking_total > 0:
  68. log("Total time benchmarking so far: %s"
  69. % TimeLogger.output(self.benchmarking_total),
  70. prefix="tfb: ",
  71. file=file,
  72. color=Fore.YELLOW)
  73. running_time = int(time.time() - self.start)
  74. log("Total execution time so far: %s"
  75. % TimeLogger.output(running_time),
  76. prefix="tfb: ",
  77. file=file,
  78. color=Fore.YELLOW)
  79. def log_verify_start(self):
  80. self.verify_start = time.time()
  81. def log_verify_end(self, log_prefix, file):
  82. total = int(time.time() - self.verify_start)
  83. self.verify_total = self.verify_total + total
  84. log("Total verify time: %s" % TimeLogger.output(total),
  85. prefix=log_prefix,
  86. file=file,
  87. color=Fore.YELLOW)