Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added basic server-tick-rate multi-worker simulator class #3

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions global_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Global Variables

SERVER_TICK_INTERVAL = 0.033
50 changes: 50 additions & 0 deletions simulator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Simulator

import global_variables as GV
import time
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) -> None:
self.runIndex = 0
self.runAmount = runAmount
self.runTime = runTime

def runSingle(self, runID: int) -> int:
self.runIndex = runID
sr = SimulationRun(self.runTime * secrets.randbelow(100))
log(f"Starting run {self.runIndex + 1} of {self.runAmount}")
ret = sr.run()
log(f"Completed run {self.runIndex + 1} of {self.runAmount}")
return ret

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) -> None:
self.runParallel(5)


class SimulationRun:
def __init__(self, runTime: float) -> None:
self.runTime = runTime
log(f"RUN TIME: {self.runTime}")

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()