Recursive profiler results output

This commit is contained in:
Mark Qvist 2024-09-15 16:46:52 +02:00
parent 8d61ee8a81
commit 421b5ef32e

View File

@ -336,21 +336,24 @@ def exit():
profiler_ran = False profiler_ran = False
profiler_tags = {} profiler_tags = {}
def profiler(tag=None, capture=False): def profiler(tag=None, capture=False, super_tag=None):
global profiler_ran, profiler_tags global profiler_ran, profiler_tags
try: try:
thread_ident = threading.get_ident() thread_ident = threading.get_ident()
if capture: if capture:
end = time.perf_counter() end = time.perf_counter()
if tag in profiler_tags and thread_ident in profiler_tags[tag]["threads"]:
if profiler_tags[tag]["threads"][thread_ident]["current_start"] != None:
begin = profiler_tags[tag]["threads"][thread_ident]["current_start"] begin = profiler_tags[tag]["threads"][thread_ident]["current_start"]
profiler_tags[tag]["threads"][thread_ident]["current_start"] = None
profiler_tags[tag]["threads"][thread_ident]["captures"].append(end-begin) profiler_tags[tag]["threads"][thread_ident]["captures"].append(end-begin)
if not profiler_ran: if not profiler_ran:
profiler_ran = True profiler_ran = True
else: else:
if not tag in profiler_tags: if not tag in profiler_tags:
profiler_tags[tag] = {"threads": {}} profiler_tags[tag] = {"threads": {}, "super": super_tag}
if not thread_ident in profiler_tags[tag]["threads"]: if not thread_ident in profiler_tags[tag]["threads"]:
profiler_tags[tag]["threads"][thread_ident] = {"current_start": None, "captures": []} profiler_tags[tag]["threads"][thread_ident] = {"current_start": None, "captures": []}
@ -386,6 +389,8 @@ def profiler_results():
sample_count = len(tag_captures) sample_count = len(tag_captures)
if sample_count > 2: if sample_count > 2:
tag_results = { tag_results = {
"name": tag,
"super": tag_entry["super"],
"count": len(tag_captures), "count": len(tag_captures),
"mean": mean(tag_captures), "mean": mean(tag_captures),
"median": median(tag_captures), "median": median(tag_captures),
@ -394,12 +399,26 @@ def profiler_results():
results[tag] = tag_results results[tag] = tag_results
def print_results_recursive(tag, results, level=0):
print_tag_results(tag, level+1)
for tag_name in results:
sub_tag = results[tag_name]
if sub_tag["super"] == tag["name"]:
print_results_recursive(sub_tag, results, level=level+1)
def print_tag_results(tag, level):
ind = " "*level
print(f"{ind}{tag["name"]}")
print(f"{ind} Samples : {tag["count"]}")
print(f"{ind} Mean : {prettyshorttime(tag["mean"])}")
print(f"{ind} Median : {prettyshorttime(tag["median"])}")
print(f"{ind} St.dev. : {prettyshorttime(tag["stdev"])}")
print("")
print("\nProfiler results:\n") print("\nProfiler results:\n")
for tag_name in results: for tag_name in results:
tag = results[tag_name] tag = results[tag_name]
print(f" {tag_name}") if tag["super"] == None:
print(f" Samples : {tag["count"]}") print_results_recursive(tag, results)
print(f" Mean : {prettyshorttime(tag["mean"])}")
print(f" Median : {prettyshorttime(tag["median"])}")
print(f" St.dev. : {prettyshorttime(tag["stdev"])}")
print("")