measure loading xpath.pl library loading time#349
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a script to measure and log the loading time of the xpath.pl library. The approach is sound, but the script could be made more robust with better error handling for the external commands it runs. I've added a couple of suggestions to catch potential exceptions and provide clearer error messages to the user. This will make the tool more reliable and easier to debug.
| result = subprocess.run( | ||
| ["git", "rev-parse", "--short", "HEAD"], | ||
| cwd=repo_root, | ||
| check=True, | ||
| capture_output=True, | ||
| text=True, | ||
| ) |
There was a problem hiding this comment.
The script will crash with an unhandled exception if the git command fails (e.g., if git is not installed, or this is not a git repository). It's better to catch potential subprocess.CalledProcessError and FileNotFoundError exceptions and print a user-friendly error message before exiting. This requires importing the sys module at the top of the file (e.g., import sys).
| result = subprocess.run( | |
| ["git", "rev-parse", "--short", "HEAD"], | |
| cwd=repo_root, | |
| check=True, | |
| capture_output=True, | |
| text=True, | |
| ) | |
| try: | |
| result = subprocess.run( | |
| ["git", "rev-parse", "--short", "HEAD"], | |
| cwd=repo_root, | |
| check=True, | |
| capture_output=True, | |
| text=True, | |
| ) | |
| except (FileNotFoundError, subprocess.CalledProcessError) as e: | |
| print(f"Error: Failed to get git commit hash.", file=sys.stderr) | |
| print(f"Details: {e}", file=sys.stderr) | |
| if isinstance(e, subprocess.CalledProcessError): | |
| print(f"Stderr: {e.stderr}", file=sys.stderr) | |
| sys.exit(1) |
| """Run xpath library load command and return elapsed seconds.""" | ||
| command = ["uv", "run", "vibeprolog.py", "./library/xpath.pl", '-q', 'true.'] | ||
| start = time.perf_counter() | ||
| subprocess.run(command, cwd=repo_root, check=True) |
There was a problem hiding this comment.
This subprocess.run call can fail if uv is not found (FileNotFoundError) or if the command returns a non-zero exit code (CalledProcessError). It's good practice to handle these potential errors and provide a clear error message. For better diagnostics, consider capturing the output and printing stderr on failure. This requires importing the sys module.
| subprocess.run(command, cwd=repo_root, check=True) | |
| try: | |
| # Capture output to provide more context on error | |
| subprocess.run(command, cwd=repo_root, check=True, capture_output=True, text=True) | |
| except (FileNotFoundError, subprocess.CalledProcessError) as e: | |
| print("Error: Failed to run xpath load measurement.", file=sys.stderr) | |
| print(f"Details: {e}", file=sys.stderr) | |
| if isinstance(e, subprocess.CalledProcessError): | |
| print(f"Stderr: {e.stderr}", file=sys.stderr) | |
| sys.exit(1) |
No description provided.