Skip to content

Commit 9ddd763

Browse files
committed
module_name traverse up and cleanup prints
1 parent ea0cab7 commit 9ddd763

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

codeflash/benchmarking/plugin/plugin.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def setup(self, trace_path: str, project_root: str) -> None:
5353
raise
5454

5555
def write_benchmark_timings(self) -> None:
56-
print("XXX ATTEMPTING TO WRITE THE BENCHMARK TIMINGS")
5756
if not self.benchmark_timings:
5857
return # No data to write
5958

@@ -96,7 +95,6 @@ def get_function_benchmark_timings(trace_path: Path) -> dict[str, dict[Benchmark
9695
9796
"""
9897
# Initialize the result dictionary
99-
print("XXX ATTEMPTING get_function_benchmark_timings")
10098
result = {}
10199

102100
# Connect to the SQLite database
@@ -156,7 +154,6 @@ def get_benchmark_timings(trace_path: Path) -> dict[BenchmarkKey, int]:
156154
157155
"""
158156
# Initialize the result dictionary
159-
print("XXX ATTEMPTING get_benchmark_timings")
160157
result = {}
161158
overhead_by_benchmark = {}
162159

@@ -190,7 +187,6 @@ def get_benchmark_timings(trace_path: Path) -> dict[BenchmarkKey, int]:
190187

191188
# Create the benchmark key (file::function::line)
192189
benchmark_key = BenchmarkKey(module_path=benchmark_file, function_name=benchmark_func)
193-
print(f"XXX Processing benchmark: {benchmark_key}")
194190
# Subtract overhead from total time
195191
overhead = overhead_by_benchmark.get(benchmark_key, 0)
196192
result[benchmark_key] = time_ns - overhead
@@ -246,11 +242,9 @@ def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item
246242
class Benchmark: # noqa: D106
247243
def __init__(self, request: pytest.FixtureRequest) -> None:
248244
self.request = request
249-
print("XXX INITIALIZING THE BENCHMARK")
250245

251246
def __call__(self, func, *args, **kwargs): # type: ignore # noqa: ANN001, ANN002, ANN003, ANN204, PGH003
252247
"""Handle both direct function calls and decorator usage."""
253-
print("XXX CALLED THE BENCHMARK")
254248
if args or kwargs:
255249
# Used as benchmark(func, *args, **kwargs)
256250
return self._run_benchmark(func, *args, **kwargs)
@@ -264,10 +258,10 @@ def wrapped_func(*args, **kwargs): # noqa: ANN002, ANN003, ANN202
264258

265259
def _run_benchmark(self, func, *args, **kwargs): # noqa: ANN001, ANN002, ANN003, ANN202
266260
"""Actual benchmark implementation."""
267-
print("XXX RUNNING THE BENCHMARK!!")
268261
benchmark_module_path = module_name_from_file_path(
269-
Path(str(self.request.node.fspath)), Path(codeflash_benchmark_plugin.project_root)
262+
Path(str(self.request.node.fspath)), Path(codeflash_benchmark_plugin.project_root), traverse_up=True
270263
)
264+
271265
benchmark_function_name = self.request.node.name
272266
line_number = int(str(sys._getframe(2).f_lineno)) # 2 frames up in the call stack # noqa: SLF001
273267
# Set env vars
@@ -299,7 +293,6 @@ def benchmark(request: pytest.FixtureRequest) -> object:
299293
"""Fixture to provide the benchmark functionality."""
300294
if not request.config.getoption("--codeflash-trace"):
301295
return None
302-
print("XXX BENCHMARK PLUGIN INITIATED")
303296
return CodeFlashBenchmarkPlugin.Benchmark(request)
304297

305298

codeflash/code_utils/code_utils.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,21 @@ def get_qualified_name(module_name: str, full_qualified_name: str) -> str:
109109
return full_qualified_name[len(module_name) + 1 :]
110110

111111

112-
def module_name_from_file_path(file_path: Path, project_root_path: Path) -> str:
113-
relative_path = file_path.relative_to(project_root_path)
114-
return relative_path.with_suffix("").as_posix().replace("/", ".")
112+
def module_name_from_file_path(file_path: Path, project_root_path: Path, *, traverse_up: bool = True) -> str:
113+
try:
114+
relative_path = file_path.relative_to(project_root_path)
115+
return relative_path.with_suffix("").as_posix().replace("/", ".")
116+
except ValueError:
117+
if traverse_up:
118+
parent = file_path.parent
119+
while parent not in (project_root_path, parent.parent):
120+
try:
121+
relative_path = file_path.relative_to(parent)
122+
return relative_path.with_suffix("").as_posix().replace("/", ".")
123+
except ValueError:
124+
parent = parent.parent
125+
msg = f"File {file_path} is not within the project root {project_root_path}."
126+
raise ValueError(msg) # noqa: B904
115127

116128

117129
def file_path_from_module_name(module_name: str, project_root_path: Path) -> Path:

0 commit comments

Comments
 (0)