From 0eb7bb3d628ace7976a52caaac5127da83350cb9 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Tue, 12 Oct 2021 16:02:58 -0400 Subject: [PATCH 1/2] feat: added basic server-tick-rate multi-worker simulator class --- global_variables.py | 3 +++ simulator.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 global_variables.py create mode 100644 simulator.py diff --git a/global_variables.py b/global_variables.py new file mode 100644 index 0000000..01f11ef --- /dev/null +++ b/global_variables.py @@ -0,0 +1,3 @@ +# Global Variables + +SERVER_TICK_INTERVAL = 0.033 diff --git a/simulator.py b/simulator.py new file mode 100644 index 0000000..e563a83 --- /dev/null +++ b/simulator.py @@ -0,0 +1,44 @@ +# Simulator + +import global_variables as GV +import time +from concurrent.futures import ProcessPoolExecutor +import secrets + +class Simulator: + def __init__(self, runAmount: int = 5, runTime: float = 100.0): + self.runIndex = 0 + self.runAmount = runAmount + self.runTime = runTime + + def runSingle(self, runID: int): + self.runIndex = runID + print("Starting run {} of {}".format(self.runIndex + 1, self.runAmount)) + sr = SimulationRun(self.runTime * secrets.randbelow(100)) + ret = sr.run() + print("Completed run {} of {}".format(self.runIndex + 1, self.runAmount)) + return ret + + def runParallel(self, numWorkers: int = 5): + with ProcessPoolExecutor(numWorkers) as executor: + results = executor.map(self.runSingle, range(self.runAmount)) + return sum([x for x in results]) + + def runAll(self): + self.runParallel(5) + + +class SimulationRun: + def __init__(self, runTime: float): + self.runTime = runTime + print(f'RUN TIME: {self.runTime}') + + def run(self): + time = 0.0 + while time < self.runTime: + time += GV.SERVER_TICK_INTERVAL + return 1 + +if __name__ == "__main__": + x = Simulator() + x.runAll() From b7c43687c8a186d752e3308d7cc9e3be54430f75 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Tue, 1 Feb 2022 12:25:12 -0500 Subject: [PATCH 2/2] fix: pythonification a little bit --- simulator.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/simulator.py b/simulator.py index e563a83..8482cae 100644 --- a/simulator.py +++ b/simulator.py @@ -5,40 +5,46 @@ from concurrent.futures import ProcessPoolExecutor import secrets + +def log(msg: str) -> None: + print(msg) + + class Simulator: - def __init__(self, runAmount: int = 5, runTime: float = 100.0): + def __init__(self, runAmount: int = 5, runTime: float = 100.0) -> None: self.runIndex = 0 self.runAmount = runAmount self.runTime = runTime - def runSingle(self, runID: int): + def runSingle(self, runID: int) -> int: self.runIndex = runID - print("Starting run {} of {}".format(self.runIndex + 1, self.runAmount)) sr = SimulationRun(self.runTime * secrets.randbelow(100)) + log(f"Starting run {self.runIndex + 1} of {self.runAmount}") ret = sr.run() - print("Completed run {} of {}".format(self.runIndex + 1, self.runAmount)) + log(f"Completed run {self.runIndex + 1} of {self.runAmount}") return ret - def runParallel(self, numWorkers: int = 5): + def runParallel(self, numWorkers: int = 5) -> int: with ProcessPoolExecutor(numWorkers) as executor: results = executor.map(self.runSingle, range(self.runAmount)) return sum([x for x in results]) - def runAll(self): + def runAll(self) -> None: self.runParallel(5) class SimulationRun: - def __init__(self, runTime: float): + def __init__(self, runTime: float) -> None: self.runTime = runTime - print(f'RUN TIME: {self.runTime}') + log(f"RUN TIME: {self.runTime}") - def run(self): + def run(self) -> int: time = 0.0 while time < self.runTime: time += GV.SERVER_TICK_INTERVAL return 1 + if __name__ == "__main__": x = Simulator() x.runAll()