Resource profiling with yappi instead of cprofile

This commit is contained in:
Mark Qvist 2022-06-14 13:44:12 +02:00
parent 3013c10180
commit 6bfd0bf4eb

View File

@ -32,11 +32,14 @@ c_rns = None
def init_rns(caller=None): def init_rns(caller=None):
global c_rns global c_rns
if c_rns == None: if c_rns == None:
targets_job(caller) if caller != None:
time.sleep(2) targets_job(caller)
time.sleep(2)
print("Starting local RNS instance...") print("Starting local RNS instance...")
c_rns = RNS.Reticulum("./tests/rnsconfig") c_rns = RNS.Reticulum("./tests/rnsconfig")
c_rns.m_proc = caller.process if caller != None:
c_rns.m_proc = caller.process
print("Done starting local RNS instance...") print("Done starting local RNS instance...")
def close_rns(): def close_rns():
@ -88,7 +91,7 @@ class TestLink(unittest.TestCase):
b = 0 b = 0
pr_t = 0 pr_t = 0
receipts = [] receipts = []
if RNS.Cryptography.backend() == "internal": if RNS.Cryptography.backend() == "internal" or RNS.Reticulum.MTU > 500:
num_packets = 50 num_packets = 50
else: else:
num_packets = 500 num_packets = 500
@ -304,12 +307,30 @@ if __name__ == '__main__':
unittest.main(verbosity=1) unittest.main(verbosity=1)
def targets(): def targets(yp=False):
if yp:
import yappi
def resource_started(resource): def resource_started(resource):
print("Resource started") print("Resource started")
if yp:
yappi.start()
def resource_concluded(resource): def resource_concluded(resource):
print("Resource concluded") print("Resource concluded")
if yp:
try:
yappi.stop()
yappi.get_func_stats().save("receiver_main_calls.data", type="pstat")
threads = yappi.get_thread_stats()
for thread in threads:
print(
"Function stats for (%s) (%d)" % (thread.name, thread.id)
) # it is the Thread.__class__.__name__
yappi.get_func_stats(ctx_id=thread.id).save("receiver_thread_"+str(thread.id)+".data", type="pstat")
except Exception as e:
print("Error: "+str(e))
if hasattr(resource.link.attached_interface, "rxptime"): if hasattr(resource.link.attached_interface, "rxptime"):
rx_pr = (resource.link.attached_interface.rxb*8)/resource.link.attached_interface.rxptime rx_pr = (resource.link.attached_interface.rxb*8)/resource.link.attached_interface.rxptime
@ -330,24 +351,24 @@ def targets():
while True: while True:
time.sleep(1) time.sleep(1)
def targets_profiling(): def targets_profiling(yp=False):
targets() targets(yp)
def profile_resource(): def profile_resource():
import cProfile # import cProfile
import pstats # import pstats
from pstats import SortKey # from pstats import SortKey
cProfile.runctx("entry()", {"entry": resource_profiling, "size_str": size_str}, {}, "profile-resource.data") # cProfile.runctx("entry()", {"entry": resource_profiling, "size_str": size_str}, {}, "profile-resource.data")
p = pstats.Stats("profile-resource.data") # p = pstats.Stats("profile-resource.data")
# p.strip_dirs().sort_stats(SortKey.TIME, SortKey.CUMULATIVE).print_stats()
resource_profiling()
def profile_targets(): def profile_targets():
import cProfile
import pstats targets_profiling(yp=True)
from pstats import SortKey # cProfile.runctx("entry()", {"entry": targets_profiling, "size_str": size_str}, {}, "profile-targets.data")
cProfile.runctx("entry()", {"entry": targets_profiling, "size_str": size_str}, {}, "profile-targets.data") # p = pstats.Stats("profile-targets.data")
p = pstats.Stats("profile-targets.data") # p.strip_dirs().sort_stats(SortKey.TIME, SortKey.CUMULATIVE).print_stats()
p.strip_dirs().sort_stats(SortKey.TIME, SortKey.CUMULATIVE).print_stats()
def resource_profiling(): def resource_profiling():
@ -366,6 +387,10 @@ def resource_profiling():
resource_size = 5*1000*1000 resource_size = 5*1000*1000
data = os.urandom(resource_size) data = os.urandom(resource_size)
print("Sending "+size_str(resource_size)+" resource...") print("Sending "+size_str(resource_size)+" resource...")
import yappi
yappi.start()
resource = RNS.Resource(data, l1, timeout=resource_timeout) resource = RNS.Resource(data, l1, timeout=resource_timeout)
start = time.time() start = time.time()
@ -377,6 +402,17 @@ def resource_profiling():
t = time.time() - start t = time.time() - start
print("Resource completed at "+size_str(resource_size/t, "b")+"ps") print("Resource completed at "+size_str(resource_size/t, "b")+"ps")
yappi.get_func_stats().save("sender_main_calls.data", type="pstat")
threads = yappi.get_thread_stats()
for thread in threads:
print(
"Function stats for (%s) (%d)" % (thread.name, thread.id)
) # it is the Thread.__class__.__name__
yappi.get_func_stats(ctx_id=thread.id).save("sender_thread_"+str(thread.id)+".data", type="pstat")
# t_pstats = yappi.convert2pstats(tstats)
# t_pstats.save("resource_tstat.data", type="pstat")
if hasattr(resource.link.attached_interface, "rxptime"): if hasattr(resource.link.attached_interface, "rxptime"):
rx_pr = (resource.link.attached_interface.rxb*8)/resource.link.attached_interface.rxptime rx_pr = (resource.link.attached_interface.rxb*8)/resource.link.attached_interface.rxptime
print("Average RX proccessing rate: "+size_str(rx_pr, "b")+"ps") print("Average RX proccessing rate: "+size_str(rx_pr, "b")+"ps")