Last active
May 9, 2024 03:32
-
-
Save realphongha/e1de2007c415d4614491f5c758a0405b to your computer and use it in GitHub Desktop.
Python performance monitor as a Mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
class PerfMonitorMixin: | |
PERF_MONITOR = {} | |
def update_perf(self, name, latency, ignore_classwise=False): | |
if not ignore_classwise: | |
name = self.__class__.__name__ + "_" + name | |
if name not in PerfMonitorMixin.PERF_MONITOR: | |
PerfMonitorMixin.PERF_MONITOR[name] = [] | |
PerfMonitorMixin.PERF_MONITOR[name].append(latency) | |
def get_perf_for_this_class(self, name, display=True): | |
name = self.__class__.__name__ + "_" + name | |
return PerfMonitorMixin.get_perf(name, display) | |
def get_all_perfs_for_this_class(self, display=True): | |
class_name = self.__class__.__name__ + "_" | |
all_perfs = {} | |
perf_names = list(PerfMonitorMixin.PERF_MONITOR.keys()) | |
# Higest latency goes first | |
perf_names.sort(key=lambda k: | |
-np.mean(PerfMonitorMixin.PERF_MONITOR[k])) | |
for name in perf_names: | |
if name.startswith(class_name): | |
all_perfs[name] = PerfMonitorMixin.get_perf(name, display) | |
return all_perfs | |
@staticmethod | |
def get_perf(name, display=True): | |
if name not in PerfMonitorMixin.PERF_MONITOR: | |
if display: | |
print(f"{name} is not in performance monitor!") | |
return -1, -1 | |
latency = np.mean(PerfMonitorMixin.PERF_MONITOR[name]) | |
total_time = sum(PerfMonitorMixin.PERF_MONITOR[name]) | |
fps = 1 / latency | |
if display: | |
print("SPEED PERFORMANCE for '%s':" % name) | |
print("FPS - %10.2f, Latency - %15.8f, Total time - %15.8f" % | |
(fps, latency, total_time)) | |
return fps, latency | |
@staticmethod | |
def get_all_perfs(display=True): | |
all_perfs = {} | |
perf_names = list(PerfMonitorMixin.PERF_MONITOR.keys()) | |
# Higest latency goes first | |
perf_names.sort(key=lambda k: | |
-np.mean(PerfMonitorMixin.PERF_MONITOR[k])) | |
for name in perf_names: | |
all_perfs[name] = PerfMonitorMixin.get_perf(name, display) | |
return all_perfs | |
if __name__ == "__main__": | |
# examples on how it works | |
import time | |
class A(PerfMonitorMixin): | |
def __init__(self): | |
pass | |
def a(self): | |
s = time.time() | |
_ = 2**100 | |
self.update_perf("a", time.time()-s) | |
def b(self): | |
s = time.time() | |
_ = 2**200 | |
self.update_perf("b", time.time()-s) | |
class B(PerfMonitorMixin): | |
def __init__(self): | |
pass | |
def a(self): | |
s = time.time() | |
_ = 2**150 | |
self.update_perf("a", time.time()-s) | |
def b(self): | |
s = time.time() | |
_ = 2**250 | |
self.update_perf("b", time.time()-s) | |
a = A() | |
b = B() | |
for _ in range(1000): | |
a.a() | |
a.b() | |
b.a() | |
b.b() | |
a.get_perf_for_this_class("a") | |
print("=========================================") | |
a.get_all_perfs_for_this_class() | |
print("=========================================") | |
PerfMonitorMixin.get_perf("B_b") | |
print("=========================================") | |
perf = PerfMonitorMixin.get_all_perfs() | |
print(perf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment