2023-01-27 16:28:12 +08:00
|
|
|
import time
|
2023-07-22 22:08:45 +08:00
|
|
|
import argparse
|
2023-01-27 16:28:12 +08:00
|
|
|
|
|
|
|
|
2023-05-21 05:41:41 +08:00
|
|
|
class TimerSubcategory:
|
|
|
|
def __init__(self, timer, category):
|
|
|
|
self.timer = timer
|
|
|
|
self.category = category
|
|
|
|
self.start = None
|
|
|
|
self.original_base_category = timer.base_category
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.start = time.time()
|
|
|
|
self.timer.base_category = self.original_base_category + self.category + "/"
|
2023-07-22 22:08:45 +08:00
|
|
|
self.timer.subcategory_level += 1
|
|
|
|
|
|
|
|
if self.timer.print_log:
|
|
|
|
print(f"{' ' * self.timer.subcategory_level}{self.category}:")
|
2023-05-21 05:41:41 +08:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
elapsed_for_subcategroy = time.time() - self.start
|
|
|
|
self.timer.base_category = self.original_base_category
|
|
|
|
self.timer.add_time_to_record(self.original_base_category + self.category, elapsed_for_subcategroy)
|
2023-07-22 22:08:45 +08:00
|
|
|
self.timer.subcategory_level -= 1
|
|
|
|
self.timer.record(self.category, disable_log=True)
|
2023-05-21 05:41:41 +08:00
|
|
|
|
|
|
|
|
2023-01-27 16:28:12 +08:00
|
|
|
class Timer:
|
2023-07-22 22:08:45 +08:00
|
|
|
def __init__(self, print_log=False):
|
2023-01-27 16:28:12 +08:00
|
|
|
self.start = time.time()
|
|
|
|
self.records = {}
|
|
|
|
self.total = 0
|
2023-05-21 05:41:41 +08:00
|
|
|
self.base_category = ''
|
2023-07-22 22:08:45 +08:00
|
|
|
self.print_log = print_log
|
|
|
|
self.subcategory_level = 0
|
2023-01-27 16:28:12 +08:00
|
|
|
|
|
|
|
def elapsed(self):
|
|
|
|
end = time.time()
|
|
|
|
res = end - self.start
|
|
|
|
self.start = end
|
|
|
|
return res
|
|
|
|
|
2023-05-21 05:41:41 +08:00
|
|
|
def add_time_to_record(self, category, amount):
|
2023-01-27 16:28:12 +08:00
|
|
|
if category not in self.records:
|
|
|
|
self.records[category] = 0
|
|
|
|
|
2023-05-21 05:41:41 +08:00
|
|
|
self.records[category] += amount
|
|
|
|
|
2023-07-22 22:08:45 +08:00
|
|
|
def record(self, category, extra_time=0, disable_log=False):
|
2023-05-21 05:41:41 +08:00
|
|
|
e = self.elapsed()
|
|
|
|
|
|
|
|
self.add_time_to_record(self.base_category + category, e + extra_time)
|
|
|
|
|
2023-01-27 16:28:12 +08:00
|
|
|
self.total += e + extra_time
|
|
|
|
|
2023-07-22 22:08:45 +08:00
|
|
|
if self.print_log and not disable_log:
|
|
|
|
print(f"{' ' * self.subcategory_level}{category}: done in {e + extra_time:.3f}s")
|
|
|
|
|
2023-05-21 05:41:41 +08:00
|
|
|
def subcategory(self, name):
|
|
|
|
self.elapsed()
|
|
|
|
|
|
|
|
subcat = TimerSubcategory(self, name)
|
|
|
|
return subcat
|
|
|
|
|
2023-01-27 16:28:12 +08:00
|
|
|
def summary(self):
|
|
|
|
res = f"{self.total:.1f}s"
|
|
|
|
|
2023-05-21 05:41:41 +08:00
|
|
|
additions = [(category, time_taken) for category, time_taken in self.records.items() if time_taken >= 0.1 and '/' not in category]
|
2023-01-27 16:28:12 +08:00
|
|
|
if not additions:
|
|
|
|
return res
|
|
|
|
|
|
|
|
res += " ("
|
|
|
|
res += ", ".join([f"{category}: {time_taken:.1f}s" for category, time_taken in additions])
|
|
|
|
res += ")"
|
|
|
|
|
|
|
|
return res
|
2023-03-13 02:25:22 +08:00
|
|
|
|
2023-05-21 05:41:41 +08:00
|
|
|
def dump(self):
|
|
|
|
return {'total': self.total, 'records': self.records}
|
|
|
|
|
2023-03-13 02:25:22 +08:00
|
|
|
def reset(self):
|
|
|
|
self.__init__()
|
2023-05-21 05:41:41 +08:00
|
|
|
|
|
|
|
|
2023-07-22 22:08:45 +08:00
|
|
|
parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser.add_argument("--log-startup", action='store_true', help="print a detailed log of what's happening at startup")
|
|
|
|
args = parser.parse_known_args()[0]
|
|
|
|
|
|
|
|
startup_timer = Timer(print_log=args.log_startup)
|
2023-05-21 05:41:41 +08:00
|
|
|
|
|
|
|
startup_record = None
|