run_vadere_console_with_all_scenario_files.py 5.29 KB
Newer Older
1
2
3
4
# 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
5
# a little bit while script is running in continuous integration pipeline.
6

7
# Watch out: call this script from root directory of project. E.g.
8
9
10
11
12
#
#   python Tools/my_script.py

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

18
def find_scenario_files(path="VadereModelTests", scenario_search_pattern = "*.scenario", exclude_patterns = ["TESTOVM", "output","legacy"]):
19
20
21
22
23
    scenario_files = []

    for root, dirnames, filenames in os.walk(path):
        for filename in fnmatch.filter(filenames, scenario_search_pattern):
            scenario_path = os.path.join(root, filename)
24
            scenario_path_excluded = False
25
26
27
28

            for exclude_pattern in exclude_patterns:
                regex_pattern = re.compile(exclude_pattern)
                match = regex_pattern.search(scenario_path)
Marion Goedel's avatar
Marion Goedel committed
29
                if match:
30
                    scenario_path_excluded = True
31

32
            if scenario_path_excluded == False:
Marion Goedel's avatar
Marion Goedel committed
33
                scenario_files.append(scenario_path)
34

35
    return sorted(scenario_files)
36

37
def run_scenario_files_with_vadere_console(scenario_files, vadere_console="VadereSimulator/target/vadere-console.jar", scenario_timeout_in_sec=60):
38
39
40
41
42
    output_dir = "output"

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

43
44
    total_scenario_files = len(scenario_files)

45
46
    passed_scenarios = []
    failed_scenarios_with_exception = []
47

48
    for i, scenario_file in enumerate(scenario_files):
49

50
        try:
51
            print("Running scenario file ({}/{}): {}".format(i + 1, total_scenario_files, scenario_file))
52

53
            # Measure wall time and not CPU time simply because it is the simplest method.
54
55
56
            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
57
            completed_process = subprocess.run(args=["java", "-enableassertions", "-jar", vadere_console, "scenario-run", "-f", scenario_file, "-o", output_dir],
58
59
60
61
62
63
64
65
                                           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

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

            passed_scenarios.append(scenario_file)
        except subprocess.TimeoutExpired as exception:
70
            print("Scenario file failed: {}".format(scenario_file))
Marion Goedel's avatar
Marion Goedel committed
71
            print("->  Reason: timeout after {} s".format(exception.timeout))
72
73
            failed_scenarios_with_exception.append((scenario_file, exception))
        except subprocess.CalledProcessError as exception:
Marion Goedel's avatar
Marion Goedel committed
74
            prefix = ""
75
76
            if "TestOSM" in scenario_file:
                prefix = " * OSM * "
Marion Goedel's avatar
Marion Goedel committed
77
78
            print(prefix + "Scenario file failed: {}".format(scenario_file))
            print("->  Reason: non-zero return value {}".format(exception.returncode))
79
            failed_scenarios_with_exception.append((scenario_file, exception))
80
81
82
83

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

84
85
    return {"passed": passed_scenarios, "failed": failed_scenarios_with_exception}

86
87
88
89
def print_summary(passed_and_failed_scenarios):
    total_passed_scenarios = len(passed_and_failed_scenarios["passed"])
    total_failed_scenarios = + len(passed_and_failed_scenarios["failed"])
    total_scenarios = total_passed_scenarios + total_failed_scenarios
Marion Goedel's avatar
Marion Goedel committed
90

91
92
93
94
95
96
97
98
99
    print("###########")
    print("# Summary #")
    print("###########")
    print("")
    print("Total scenario runs: {}".format(total_scenarios))
    print("Passed: {}".format(total_passed_scenarios))
    print("Failed: {}".format(total_failed_scenarios))

if __name__ == "__main__":
100

101
102
103
104
105
106
107
    long_running_scenarios = [
            "basic_4_1_wall_gnm1",
            "queueing",
            "rimea_09",
            "rimea_11",
            "TestSFM",
            "thin_wall_and_closer_source_nelder_mead_ok",
108
109
            "thin_wall_and_closer_source_pso_could_fail",
            "rimea_04_flow_osm1_550_up",
110
    ]
111

112
113
    excluded_scenarios = ["TESTOVM", "output", "legacy"]
    excluded_scenarios.extend(long_running_scenarios)
Marion Goedel's avatar
Marion Goedel committed
114

115
    scenario_files_regular_length = find_scenario_files(exclude_patterns=excluded_scenarios)
116
    passed_and_failed_scenarios = run_scenario_files_with_vadere_console(scenario_files_regular_length)
Marion Goedel's avatar
Marion Goedel committed
117

118
    for scenario in long_running_scenarios:
Marion Goedel's avatar
Marion Goedel committed
119
120
        search_pattern = "*" + scenario + "*.scenario"
        scenario_files_long = find_scenario_files(scenario_search_pattern=search_pattern)
121
        tmp_passed_and_failed_scenarios = run_scenario_files_with_vadere_console(scenario_files_long, scenario_timeout_in_sec=480)
Marion Goedel's avatar
Marion Goedel committed
122
123
124
        passed_and_failed_scenarios["passed"].extend(tmp_passed_and_failed_scenarios["passed"])
        passed_and_failed_scenarios["failed"].extend(tmp_passed_and_failed_scenarios["failed"])

125
    print_summary(passed_and_failed_scenarios)
126
127
128
129
130

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