import multiprocessing as mp
from concurrent.futures import ThreadPoolExecutor
import time
import numpy as np

class Benchmark:
    shutdown:bool = False

    def __init__(self,fps:float, time_scaler:float=1.0):
        assert fps > 0.0
        assert time_scaler > 0.0
        self.fps = fps * time_scaler
        self.prev_fps = self.fps


    matrix_size = 10
    def some_process(self):
        mat = np.random.randn(self.matrix_size,self.matrix_size)
        np.dot(mat,mat)

    
    def activation(self,clock):
        while not self.shutdown:
            start_time = time.time()
            self.some_process()

            wait_time = self.prev_fps/(self.fps*self.fps) - (time.time() - start_time)
            if wait_time > 0:
                pass
                time.sleep(wait_time)
            self.prev_fps = 1 / (time.time()-start_time)
            clock.value = self.prev_fps

    def run(self,num_workers:int, view_interval:float = 10):
        
        clocks = [mp.Value('d',0.0) for _ in range(num_workers)]
        processes = []
        executor =  ThreadPoolExecutor(num_workers)
        for c in clocks:
            proc = executor.submit(self.activation, c)
            processes.append(proc)
            

        try:
            while True:
                v = [f'{c.value:2.1f}' for c in clocks]
                print('fps:',', '.join(v))
                time.sleep(view_interval)
        except KeyboardInterrupt:
            self.shutdown = True
            for p in processes:
                p.result()
            executor.shutdown(True)
            print('shutdowned')

if __name__ == "__main__":
    b = Benchmark(fps=100000, time_scaler=2.0)
    b.run(num_workers=3*7,view_interval=2)
import multiprocessing as mp
from concurrent.futures import ThreadPoolExecutor
import time
import numpy as np

class Benchmark:
    shutdown:bool = False

    def __init__(self,fps:float, time_scaler:float=1.0):
        assert fps > 0.0
        assert time_scaler > 0.0
        self.fps = fps * time_scaler
        self.prev_fps = self.fps


    matrix_size = 10
    def some_process(self):
        mat = np.random.randn(self.matrix_size,self.matrix_size)
        np.dot(mat,mat)

    
    def activation(self,clock):
        while not self.shutdown:
            start_time = time.time()
            self.some_process()

            wait_time = self.prev_fps/(self.fps*self.fps) - (time.time() - start_time)
            if wait_time > 0:
                pass
                time.sleep(wait_time)
            self.prev_fps = 1 / (time.time()-start_time)
            clock.value = self.prev_fps

    def run(self,num_workers:int, view_interval:float = 10):
        
        clocks = [mp.Value('d',0.0) for _ in range(num_workers)]
        processes = []
        executor =  ThreadPoolExecutor(num_workers)
        for c in clocks:
            proc = executor.submit(self.activation, c)
            processes.append(proc)
            

        try:
            while True:
                v = [f'{c.value:2.1f}' for c in clocks]
                print('fps:',', '.join(v))
                time.sleep(view_interval)
        except KeyboardInterrupt:
            self.shutdown = True
            for p in processes:
                p.result()
            executor.shutdown(True)
            print('shutdowned')

if __name__ == "__main__":
    b = Benchmark(fps=100000, time_scaler=2.0)
    b.run(num_workers=3*7,view_interval=2)