Skip to content

Commit

Permalink
Improved profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Jan 12, 2025
1 parent abc6578 commit 53df2fa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions RNS/Reticulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ def exit_handler():
RNS.Transport.exit_handler()
RNS.Identity.exit_handler()

# if RNS.Profiler.ran():
# RNS.Profiler.results()
if RNS.Profiler.ran():
RNS.Profiler.results()

@staticmethod
def sigint_handler(signal, frame):
Expand Down
43 changes: 30 additions & 13 deletions RNS/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def exit():
sys.exit(0)

class Profiler:
ran = False
_ran = False
profilers = {}
tags = {}

Expand Down Expand Up @@ -404,8 +404,8 @@ def __exit__(self, exc_type, exc_value, traceback):
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)
if not Profiler.ran:
Profiler.ran = True
if not Profiler._ran:
Profiler._ran = True
self.resume_super()

def pause(self, pause_started=None):
Expand All @@ -422,7 +422,7 @@ def resume(self):

@staticmethod
def ran():
return Profiler.ran
return Profiler._ran

@staticmethod
def results():
Expand All @@ -438,18 +438,25 @@ def results():
thread_captures = thread_entry["captures"]
sample_count = len(thread_captures)

if sample_count > 2:
if sample_count > 1:
thread_results = {
"count": sample_count,
"mean": mean(thread_captures),
"median": median(thread_captures),
"stdev": stdev(thread_captures)
}
elif sample_count == 1:
thread_results = {
"count": sample_count,
"mean": mean(thread_captures),
"median": median(thread_captures),
"stdev": None
}

tag_captures.extend(thread_captures)

sample_count = len(tag_captures)
if sample_count > 2:
if sample_count > 1:
tag_results = {
"name": tag,
"super": tag_entry["super"],
Expand All @@ -458,8 +465,17 @@ def results():
"median": median(tag_captures),
"stdev": stdev(tag_captures)
}
elif sample_count == 1:
tag_results = {
"name": tag,
"super": tag_entry["super"],
"count": len(tag_captures),
"mean": mean(tag_captures),
"median": median(tag_captures),
"stdev": None
}

results[tag] = tag_results
results[tag] = tag_results

def print_results_recursive(tag, results, level=0):
print_tag_results(tag, level+1)
Expand All @@ -474,12 +490,13 @@ def print_tag_results(tag, level):
ind = " "*level
name = tag["name"]; count = tag["count"]
mean = tag["mean"]; median = tag["median"]; stdev = tag["stdev"]
print(f"{ind}{name}")
print(f"{ind} Samples : {count}")
print(f"{ind} Mean : {prettyshorttime(mean)}")
print(f"{ind} Median : {prettyshorttime(median)}")
print(f"{ind} St.dev. : {prettyshorttime(stdev)}")
print(f"{ind} Utilised : {prettyshorttime(mean*count)}")
print( f"{ind}{name}")
print( f"{ind} Samples : {count}")
if stdev != None:
print(f"{ind} Mean : {prettyshorttime(mean)}")
print(f"{ind} Median : {prettyshorttime(median)}")
print(f"{ind} St.dev. : {prettyshorttime(stdev)}")
print( f"{ind} Total : {prettyshorttime(mean*count)}")
print("")

print("\nProfiler results:\n")
Expand Down

0 comments on commit 53df2fa

Please sign in to comment.