|
2 | 2 | Benchmark script for Weekly scan command. |
3 | 3 | Measures performance when scanning many repositories. |
4 | 4 | """ |
5 | | -import time |
| 5 | +import argparse |
6 | 6 | import shutil |
7 | 7 | import subprocess |
8 | | -from pathlib import Path |
| 8 | +import time |
9 | 9 | from datetime import datetime, timedelta |
10 | | -import argparse |
| 10 | +from pathlib import Path |
| 11 | + |
11 | 12 |
|
12 | 13 | def create_mock_repo(repo_path: Path, index: int): |
13 | 14 | """Create a mock Git repository with some files and commits.""" |
14 | 15 | repo_path.mkdir(parents=True, exist_ok=True) |
15 | 16 | subprocess.run(["git", "init", "-q"], cwd=repo_path, check=True) |
16 | | - subprocess.run(["git", "config", "user.email", "bench@example.com"], cwd=repo_path, check=True) |
17 | | - subprocess.run(["git", "config", "user.name", "Bench User"], cwd=repo_path, check=True) |
18 | | - |
| 17 | + subprocess.run( |
| 18 | + ["git", "config", "user.email", "bench@example.com"], cwd=repo_path, check=True |
| 19 | + ) |
| 20 | + subprocess.run( |
| 21 | + ["git", "config", "user.name", "Bench User"], cwd=repo_path, check=True |
| 22 | + ) |
| 23 | + |
19 | 24 | # Create some files |
20 | | - (repo_path / "README.md").write_text(f"# Mock Repo {index}\nThis is mock repository number {index}.") |
21 | | - (repo_path / "requirements.txt").write_text("requests==2.28.1\npytest\nblack==23.1.0") |
22 | | - (repo_path / "app.py").write_text("def main():\n print('hello')\n\nif __name__ == '__main__':\n main()") |
23 | | - |
| 25 | + (repo_path / "README.md").write_text( |
| 26 | + f"# Mock Repo {index}\nThis is mock repository number {index}." |
| 27 | + ) |
| 28 | + (repo_path / "requirements.txt").write_text( |
| 29 | + "requests==2.28.1\npytest\nblack==23.1.0" |
| 30 | + ) |
| 31 | + (repo_path / "app.py").write_text( |
| 32 | + "def main():\n print('hello')\n\nif __name__ == '__main__':\n main()" |
| 33 | + ) |
| 34 | + |
24 | 35 | # Initial commit |
25 | 36 | subprocess.run(["git", "add", "."], cwd=repo_path, check=True) |
26 | | - subprocess.run(["git", "commit", "-m", "feat: initial commit", "-q"], cwd=repo_path, check=True) |
| 37 | + subprocess.run( |
| 38 | + ["git", "commit", "-m", "feat: initial commit", "-q"], cwd=repo_path, check=True |
| 39 | + ) |
| 40 | + |
27 | 41 |
|
28 | 42 | def run_benchmark(num_repos: int, jobs: int): |
29 | 43 | """Run the benchmark with a specified number of repositories and parallel jobs.""" |
30 | 44 | bench_dir = Path("bench_workdir") |
31 | 45 | if bench_dir.exists(): |
32 | 46 | shutil.rmtree(bench_dir) |
33 | 47 | bench_dir.mkdir() |
34 | | - |
| 48 | + |
35 | 49 | repos_root = bench_dir / "repos" |
36 | 50 | output_dir = bench_dir / "reports" |
37 | | - |
| 51 | + |
38 | 52 | print(f"Creating {num_repos} mock repositories...") |
39 | 53 | start_create = time.time() |
40 | 54 | for i in range(num_repos): |
41 | 55 | create_mock_repo(repos_root / f"repo_{i}", i) |
42 | 56 | end_create = time.time() |
43 | | - print(f"Created {num_repos} repositories in {end_create - start_create:.2f} seconds.") |
44 | | - |
| 57 | + print( |
| 58 | + f"Created {num_repos} repositories in {end_create - start_create:.2f} seconds." |
| 59 | + ) |
| 60 | + |
45 | 61 | print(f"\nRunning 'weekly scan' with jobs={jobs}...") |
46 | 62 | # Use 'python -m weekly.cli' or 'weekly' if installed |
47 | 63 | cmd = [ |
48 | | - "python3", "-m", "weekly.cli", "scan", |
| 64 | + "python3", |
| 65 | + "-m", |
| 66 | + "weekly.cli", |
| 67 | + "scan", |
49 | 68 | str(repos_root), |
50 | | - "--output", str(output_dir), |
51 | | - "--jobs", str(jobs), |
52 | | - "--no-recursive" |
| 69 | + "--output", |
| 70 | + str(output_dir), |
| 71 | + "--jobs", |
| 72 | + str(jobs), |
| 73 | + "--no-recursive", |
53 | 74 | ] |
54 | | - |
| 75 | + |
55 | 76 | start_scan = time.time() |
56 | 77 | result = subprocess.run(cmd, capture_output=True, text=True) |
57 | 78 | end_scan = time.time() |
58 | | - |
| 79 | + |
59 | 80 | if result.returncode != 0: |
60 | 81 | print(f"Scan failed with return code {result.returncode}") |
61 | 82 | print("STDOUT:", result.stdout) |
62 | 83 | print("STDERR:", result.stderr) |
63 | 84 | return |
64 | | - |
| 85 | + |
65 | 86 | scan_time = end_scan - start_scan |
66 | 87 | print(f"Scan completed in {scan_time:.2f} seconds.") |
67 | 88 | print(f"Average time per repository: {scan_time / num_repos:.3f} seconds.") |
68 | | - |
| 89 | + |
69 | 90 | # Cleanup |
70 | 91 | shutil.rmtree(bench_dir) |
71 | 92 |
|
| 93 | + |
72 | 94 | if __name__ == "__main__": |
73 | 95 | parser = argparse.ArgumentParser(description="Benchmark Weekly scan performance.") |
74 | | - parser.add_argument("--repos", type=int, default=20, help="Number of repositories to create (default: 20)") |
75 | | - parser.add_argument("--jobs", type=int, default=4, help="Number of parallel jobs (default: 4)") |
76 | | - |
| 96 | + parser.add_argument( |
| 97 | + "--repos", |
| 98 | + type=int, |
| 99 | + default=20, |
| 100 | + help="Number of repositories to create (default: 20)", |
| 101 | + ) |
| 102 | + parser.add_argument( |
| 103 | + "--jobs", type=int, default=4, help="Number of parallel jobs (default: 4)" |
| 104 | + ) |
| 105 | + |
77 | 106 | args = parser.parse_args() |
78 | 107 | run_benchmark(args.repos, args.jobs) |
0 commit comments