|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def pytest_addoption(parser): |
| 8 | + # Declaring additional command-line options within test suite-specific `conftest.py` |
| 9 | + # files is documented as unsupported by pytest. See: |
| 10 | + # https://docs.pytest.org/en/stable/reference/reference.html#pytest.hookspec.pytest_addoption. |
| 11 | + # |
| 12 | + # However, it seems to work in practice with the following limitations: |
| 13 | + # - The options only appear in the help text for suites that are configured in |
| 14 | + # the default `testpaths` configuration. This is undesirable because we don't |
| 15 | + # want a missing argument to trigger heavy tests like performance profiling. |
| 16 | + # - The options are parsed differently such that an equals sign is required for |
| 17 | + # options with a value rather than the more natural space-separated format. |
| 18 | + # |
| 19 | + # As a workaround, we parse the command line for options that match the patterns of |
| 20 | + # the configurable test suites and add their options to the parser. |
| 21 | + configurable_test_suites = { |
| 22 | + "tests/prof/perf": add_performance_profiling_options, |
| 23 | + } |
| 24 | + |
| 25 | + cwd = Path.cwd() |
| 26 | + project_root = Path(__file__).parent.parent |
| 27 | + for test_suite_relpath, add_options_func in list(configurable_test_suites.items()): |
| 28 | + test_suite_path = project_root.joinpath(test_suite_relpath) |
| 29 | + if test_suite_path == cwd or test_suite_path in cwd.parents: |
| 30 | + add_options_func(parser) |
| 31 | + configurable_test_suites.pop(test_suite_relpath) |
| 32 | + break |
| 33 | + |
| 34 | + # Always accept forward slashes and the system separator, escaping the latter in case of backslashes (Windows). |
| 35 | + allowed_separators = {"/", re.escape(os.sep)} |
| 36 | + separator_pattern = f"[{''.join(sorted(allowed_separators))}]" |
| 37 | + configurable_test_suites_patterns = { |
| 38 | + re.compile( |
| 39 | + rf"\b{test_suite_relpath.replace('/', separator_pattern)}\b" |
| 40 | + ): add_options_func |
| 41 | + for test_suite_relpath, add_options_func in configurable_test_suites.items() |
| 42 | + } |
| 43 | + |
| 44 | + for arg in sys.argv: |
| 45 | + if not configurable_test_suites_patterns: |
| 46 | + break |
| 47 | + |
| 48 | + for pattern, add_options_func in list( |
| 49 | + configurable_test_suites_patterns.items() |
| 50 | + ): |
| 51 | + if arg in {"-h", "--help"} or pattern.search(arg): |
| 52 | + add_options_func(parser) |
| 53 | + configurable_test_suites_patterns.pop(pattern) |
| 54 | + break |
| 55 | + |
| 56 | + |
| 57 | +def add_performance_profiling_options(parser): |
| 58 | + group = parser.getgroup("performance profiling") |
| 59 | + group.addoption( |
| 60 | + "--calibrate", |
| 61 | + action="store_true", |
| 62 | + help=( |
| 63 | + "Override automatic calibration of benchmarks and enable the following options: ", |
| 64 | + "--rounds, --warmup-rounds, --iterations", |
| 65 | + ), |
| 66 | + ) |
| 67 | + group.addoption( |
| 68 | + "--rounds", |
| 69 | + type=int, |
| 70 | + default=1000, |
| 71 | + metavar="ROUNDS", |
| 72 | + help="Number of rounds for benchmarks (default: 1000)", |
| 73 | + ) |
| 74 | + group.addoption( |
| 75 | + "--warmup-rounds", |
| 76 | + type=int, |
| 77 | + default=50, |
| 78 | + metavar="WARMUP_ROUNDS", |
| 79 | + help="Number of warmup rounds for benchmarks (default: 50)", |
| 80 | + ) |
| 81 | + group.addoption( |
| 82 | + "--iterations", |
| 83 | + type=int, |
| 84 | + default=10, |
| 85 | + metavar="ITERATIONS", |
| 86 | + help="Number of iterations in each round of benchmarks (default: 10)", |
| 87 | + ) |
| 88 | + group.addoption( |
| 89 | + "--size", |
| 90 | + type=int, |
| 91 | + default=1000, |
| 92 | + metavar="SIZE", |
| 93 | + help="Size of the benchmark data (default: 1000)", |
| 94 | + ) |
0 commit comments