|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import re |
3 | 4 | import sqlite3 |
4 | 5 | import textwrap |
5 | 6 | from pathlib import Path |
|
14 | 15 | if TYPE_CHECKING: |
15 | 16 | from collections.abc import Generator |
16 | 17 |
|
| 18 | +benchmark_context_cleaner = re.compile(r"[^a-zA-Z0-9_]+") |
| 19 | + |
17 | 20 |
|
18 | 21 | def get_next_arg_and_return( |
19 | 22 | trace_file: str, |
@@ -46,6 +49,16 @@ def get_function_alias(module: str, function_name: str) -> str: |
46 | 49 | return "_".join(module.split(".")) + "_" + function_name |
47 | 50 |
|
48 | 51 |
|
| 52 | +def get_unique_test_name(module: str, function_name: str, benchmark_name: str, class_name: str | None = None) -> str: |
| 53 | + clean_benchmark = benchmark_context_cleaner.sub("_", benchmark_name).strip("_") |
| 54 | + |
| 55 | + base_alias = get_function_alias(module, function_name) |
| 56 | + if class_name: |
| 57 | + class_alias = get_function_alias(module, class_name) |
| 58 | + return f"{class_alias}_{function_name}_{clean_benchmark}" |
| 59 | + return f"{base_alias}_{clean_benchmark}" |
| 60 | + |
| 61 | + |
49 | 62 | def create_trace_replay_test_code( |
50 | 63 | trace_file: str, |
51 | 64 | functions_data: list[dict[str, Any]], |
@@ -209,7 +222,8 @@ def create_trace_replay_test_code( |
209 | 222 | formatted_test_body = textwrap.indent(test_body, " " if test_framework == "unittest" else " ") |
210 | 223 |
|
211 | 224 | test_template += " " if test_framework == "unittest" else "" |
212 | | - test_template += f"def test_{alias}({self}):\n{formatted_test_body}\n" |
| 225 | + unique_test_name = get_unique_test_name(module_name, function_name, benchmark_function_name, class_name) |
| 226 | + test_template += f"def test_{unique_test_name}({self}):\n{formatted_test_body}\n" |
213 | 227 |
|
214 | 228 | return imports + "\n" + metadata + "\n" + test_template |
215 | 229 |
|
@@ -294,3 +308,4 @@ def generate_replay_test( |
294 | 308 | logger.info(f"Error generating replay tests: {e}") |
295 | 309 |
|
296 | 310 | return count |
| 311 | + |
0 commit comments