run_vadere_console_with_all_scenario_files.py 3.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# Use "vadere-console.jar", which is created by "mvn package", to run all
# scenario files under "VadereModelTests" subdirectory.
#
# Note: script contains some print statements so that progress can be tracked
# a little bit

# Wach out: call this script from root directory of project. E.g.
#
#   python Tools/my_script.py

import fnmatch
import os
13
import re
14
15
import shutil
import subprocess
16
import time
17
18
19
20

def find_scenario_files(path="VadereModelTests"):
    scenario_search_pattern = "*.scenario"
    scenario_files = []
21
    exclude_patterns = ["TESTOVM"]
22
23
24
25

    for root, dirnames, filenames in os.walk(path):
        for filename in fnmatch.filter(filenames, scenario_search_pattern):
            scenario_path = os.path.join(root, filename)
26
27
28
29
30
31
32

            for exclude_pattern in exclude_patterns:
                regex_pattern = re.compile(exclude_pattern)
                match = regex_pattern.search(scenario_path)

                if match is None:
                    scenario_files.append(scenario_path)
33
34

    print("Total scenario files: {}".format(len(scenario_files)))
35
    print("Exclude patterns: {}".format(exclude_patterns))
36

37
    return sorted(scenario_files)
38
39
40
41
42
43
44

def run_scenario_files_with_vadere_console(scenario_files, vadere_console="VadereGui/target/vadere-console.jar", scenario_timeout_in_sec=60):
    output_dir = "output"

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

45
46
    total_scenario_files = len(scenario_files)

47
48
    passed_scenarios = []
    failed_scenarios_with_exception = []
49

50
51
52
53
54
55
56
57
    for i, scenario_file in enumerate(scenario_files):
        try:
            print("Running scenario file ({}/{}): {}".format(i + 1, total_scenario_files, scenario_file))

            # Measure wall time and not cpu because it is the easiest.
            wall_time_start = time.time()

            # Use timout feature, check return value and capture stdout/stderr to a PIPE (use completed_process.stdout to get it).
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
58
            completed_process = subprocess.run(args=["java", "-enableassertions", "-jar", vadere_console, "scenario-run", "-f", scenario_file, "-o", output_dir],
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                                           timeout=scenario_timeout_in_sec,
                                           check=True,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE)

            wall_time_end = time.time()
            wall_time_delta = wall_time_end - wall_time_start

            print("Finished scenario file ({:.1f} s): {}".format(wall_time_delta, scenario_file))

            passed_scenarios.append(scenario_file)
        except subprocess.TimeoutExpired as exception:
            print("Scenario file failed: {}".format(scenario_file))
            print("->  Reason: timeout after {} s ({})".format(exception.timeout, exception.cmd))
            failed_scenarios_with_exception.append((scenario_file, exception))
        except subprocess.CalledProcessError as exception:
            print("Scenario file failed: {}".format(scenario_file))
            print("->  Reason: non-zero return value {} ({})".format(exception.returncode, exception.cmd))
            failed_scenarios_with_exception.append((scenario_file, exception))
78
79
80
81

    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)

82
83
    return {"passed": passed_scenarios, "failed": failed_scenarios_with_exception}

84
85
if __name__ == "__main__":
    scenario_files = find_scenario_files()
86
87
88
89
90
91
    passed_and_failed_scenarios = run_scenario_files_with_vadere_console(scenario_files)

    if len(passed_and_failed_scenarios["failed"]) > 0:
        exit(1)
    else:
        exit(0)