-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsimulator.py
More file actions
50 lines (37 loc) · 1.28 KB
/
simulator.py
File metadata and controls
50 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()