testPyPDEVS.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python3
  2. import subprocess
  3. import csv
  4. import re
  5. from argparse import ArgumentParser, Action
  6. from itertools import product
  7. from datetime import datetime
  8. MODEL_TYPES = ["LI", "HI", "HO", "HOmod"]
  9. DS_MODEL_TYPES = ["LI2HI"]
  10. INT_CYCLES = 0
  11. EXT_CYCLES = 0
  12. PyPDEVS = "./pythonpdevs/main.py"
  13. MinimalPyPDEVS = "./pythonpdevs-minimal/main.py"
  14. OUTPUT_FILE = "./Results-PyPDEVS/DEVSTONE"
  15. # Regex to capture times from script output
  16. TIME_PATTERN = re.compile(
  17. r"Model creation time: ([\d.eE+-]+).*"
  18. r"Engine setup time: ([\d.eE+-]+).*"
  19. r"Simulation time: ([\d.eE+-]+)",
  20. re.S
  21. )
  22. def run_devstone(model_type, depth, width, minimal=False, classic=False, dynamic=False):
  23. """Runs the devstone_driver.py script and returns timing results as floats."""
  24. SCRIPT_PATH = PyPDEVS
  25. if minimal:
  26. SCRIPT_PATH = MinimalPyPDEVS
  27. cmd = [
  28. "python3", SCRIPT_PATH,
  29. "-m", model_type,
  30. "-d", str(depth),
  31. "-w", str(width),
  32. "-i", str(INT_CYCLES),
  33. "-e", str(EXT_CYCLES),
  34. ]
  35. if dynamic and not minimal:
  36. cmd.extend(["-D"])
  37. if classic and not minimal:
  38. cmd.extend(["-C"])
  39. result = subprocess.run(cmd, capture_output=True, text=True)
  40. output = result.stdout.strip()
  41. errors = result.stderr.strip()
  42. if errors:
  43. print(errors)
  44. match = TIME_PATTERN.search(output)
  45. if match:
  46. creation, setup, sim = map(float, match.groups())
  47. return creation, setup, sim, output
  48. else:
  49. print("Failed to parse output for", cmd)
  50. print(output)
  51. return None, None, None, output
  52. class EnsureDS(Action):
  53. def __call__(self, parser, namespace, values, option_string=None):
  54. if not getattr(namespace, "dynamic", False):
  55. parser.error("--dynamic-models (-dm) can only be used with --dynamic (-D)")
  56. setattr(namespace, self.dest, values)
  57. def parse_args():
  58. parser = ArgumentParser()
  59. parser.add_argument("-M", "--minimal", action="store_true", default=False, help="Uses the minimal DEVS kernel. (Less features => less overhead)")
  60. parser.add_argument("-C", "--classic", action="store_true", default=False, help="Selects the Classic DEVS Simulator")
  61. parser.add_argument("-D", "--dynamic", action="store_true", default=False, help="Enables Dynamic Structure DEVS")
  62. parser.add_argument("-ms", "--model-structure", nargs="+", default=MODEL_TYPES+DS_MODEL_TYPES, choices=MODEL_TYPES+DS_MODEL_TYPES, help="Select one or more model structures")
  63. parser.add_argument("-md", "--max-depth", default=8, type=int, help="Max depth for the generated models")
  64. parser.add_argument("-ds", "--depth-stepsize", default=1, type=int, help="Determines for which depths a model is generated.")
  65. parser.add_argument("-mw", "--max-width", default=8, type=int, help="Max width for the generated models")
  66. parser.add_argument("-ws", "--width-stepsize", default=1, type=int, help="Determines for which depths a model is generated.")
  67. parser.add_argument("-r", "--repetitions", default=1, type=int, help="The number of times a simulation is repeated")
  68. return parser.parse_args()
  69. if __name__ == "__main__":
  70. args = parse_args()
  71. print(args)
  72. d = (args.max_depth, args.depth_stepsize)
  73. w = (args.max_width, args.width_stepsize)
  74. if d[0] % d[1] != 0:
  75. raise RuntimeError("Invalid depth stepsize.")
  76. if w[0] % w[1] != 0:
  77. raise RuntimeError("Invalid width stepsize.")
  78. appendix = ""
  79. if args.minimal:
  80. appendix += "-minimal"
  81. if args.classic:
  82. appendix += "-classic"
  83. if args.dynamic:
  84. appendix += "-dynamic"
  85. with open(OUTPUT_FILE + appendix + ".csv", "w", newline="") as csvfile:
  86. fieldnames = ["model_type", "depth", "width", "run",
  87. "creation_time", "setup_time", "simulation_time"]
  88. writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
  89. writer.writeheader()
  90. for model_type, d, w in product(args.model_structure, range(d[1], d[0]+1, d[1]), range(w[1], w[0]+1, w[1])):
  91. for run in range(0, args.repetitions):
  92. creation, setup, sim, output = run_devstone(model_type, d, w, args.minimal, args.classic, args.dynamic)
  93. if creation is not None:
  94. writer.writerow({
  95. "model_type": model_type,
  96. "depth": d,
  97. "width": w,
  98. "run": run,
  99. "creation_time": creation,
  100. "setup_time": setup,
  101. "simulation_time": sim
  102. })
  103. csvfile.flush()
  104. else:
  105. print("Skipping due to parse failure")
  106. print(f"\n Benchmarking complete. Results saved to {OUTPUT_FILE}")