|
| 1 | +#!/usr/bin/env -S uv run --script |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.9" |
| 4 | +# dependencies = [ |
| 5 | +# "pytest>=8.4.2", |
| 6 | +# "pytest-codspeed>=4.2.0", |
| 7 | +# ] |
| 8 | +# /// |
| 9 | + |
| 10 | +import argparse |
| 11 | +import os |
| 12 | +import shlex |
| 13 | +import subprocess |
| 14 | +from pathlib import Path |
| 15 | +import time |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | + |
| 20 | +class ValgrindRunner: |
| 21 | + """Run Valgrind with different configurations.""" |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + cmd: str, |
| 26 | + valgrind_path: str = "valgrind", |
| 27 | + output_dir: str = "/tmp", |
| 28 | + ): |
| 29 | + """Initialize valgrind runner. |
| 30 | +
|
| 31 | + Args: |
| 32 | + cmd: Command to profile (can be a path or arbitrary shell command) |
| 33 | + valgrind_path: Path to valgrind executable |
| 34 | + output_dir: Directory for callgrind output files |
| 35 | + """ |
| 36 | + self.cmd = cmd |
| 37 | + self.valgrind_path = valgrind_path |
| 38 | + self.output_dir = Path(output_dir) |
| 39 | + self.output_dir.mkdir(parents=True, exist_ok=True) |
| 40 | + |
| 41 | + # Verify valgrind is available |
| 42 | + result = subprocess.run( |
| 43 | + [self.valgrind_path, "--version"], |
| 44 | + capture_output=True, |
| 45 | + text=True, |
| 46 | + ) |
| 47 | + if result.returncode != 0: |
| 48 | + raise RuntimeError(f"Valgrind not found at: {self.valgrind_path}") |
| 49 | + self.valgrind_version = result.stdout.strip() |
| 50 | + |
| 51 | + def run_valgrind(self, *args: str) -> None: |
| 52 | + """Execute valgrind with given arguments. |
| 53 | +
|
| 54 | + Args: |
| 55 | + *args: Valgrind arguments |
| 56 | + """ |
| 57 | + callgrind_output = self.output_dir / f"callgrind.{os.getpid()}.{time.time_ns()}" |
| 58 | + |
| 59 | + cmd = [ |
| 60 | + self.valgrind_path, |
| 61 | + "--tool=callgrind", |
| 62 | + f"--callgrind-out-file={callgrind_output}", |
| 63 | + *args, |
| 64 | + *shlex.split(self.cmd), |
| 65 | + ] |
| 66 | + |
| 67 | + # Add timeout and capture output to prevent hanging and buffering issues |
| 68 | + result = subprocess.run( |
| 69 | + cmd, |
| 70 | + capture_output=True, |
| 71 | + text=True, |
| 72 | + ) |
| 73 | + if result.returncode != 0: |
| 74 | + raise RuntimeError( |
| 75 | + f"Valgrind execution failed with code {result.returncode}\n" |
| 76 | + f"stderr: {result.stderr}" |
| 77 | + ) |
| 78 | + |
| 79 | + # Clean up |
| 80 | + if callgrind_output.exists(): |
| 81 | + callgrind_output.unlink() |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture |
| 85 | +def runner(request): |
| 86 | + """Fixture to provide runner instance to tests.""" |
| 87 | + return request.config._valgrind_runner |
| 88 | + |
| 89 | + |
| 90 | +def pytest_generate_tests(metafunc): |
| 91 | + """Parametrize tests with valgrind configurations.""" |
| 92 | + if "valgrind_args" in metafunc.fixturenames: |
| 93 | + runner = getattr(metafunc.config, "_valgrind_runner", None) |
| 94 | + if not runner: |
| 95 | + return |
| 96 | + |
| 97 | + # Define valgrind configurations |
| 98 | + configs = [ |
| 99 | + (["--read-inline-info=no"], "no-inline"), |
| 100 | + (["--read-inline-info=yes"], "inline"), |
| 101 | + ( |
| 102 | + [ |
| 103 | + "--trace-children=yes", |
| 104 | + "--cache-sim=yes", |
| 105 | + "--I1=32768,8,64", |
| 106 | + "--D1=32768,8,64", |
| 107 | + "--LL=8388608,16,64", |
| 108 | + "--collect-systime=nsec", |
| 109 | + "--compress-strings=no", |
| 110 | + "--combine-dumps=yes", |
| 111 | + "--dump-line=no", |
| 112 | + "--read-inline-info=yes", |
| 113 | + ], |
| 114 | + "full-with-inline", |
| 115 | + ), |
| 116 | + ( |
| 117 | + [ |
| 118 | + "--trace-children=yes", |
| 119 | + "--cache-sim=yes", |
| 120 | + "--I1=32768,8,64", |
| 121 | + "--D1=32768,8,64", |
| 122 | + "--LL=8388608,16,64", |
| 123 | + "--collect-systime=nsec", |
| 124 | + "--compress-strings=no", |
| 125 | + "--combine-dumps=yes", |
| 126 | + "--dump-line=no", |
| 127 | + ], |
| 128 | + "full-no-inline", |
| 129 | + ), |
| 130 | + ] |
| 131 | + |
| 132 | + # Create test IDs with format: valgrind-version, command, config-name |
| 133 | + test_ids = [ |
| 134 | + f"{runner.valgrind_version}, {runner.cmd}, {config_name}" |
| 135 | + for _, config_name in configs |
| 136 | + ] |
| 137 | + |
| 138 | + # Parametrize with just the args |
| 139 | + metafunc.parametrize( |
| 140 | + "valgrind_args", |
| 141 | + [args for args, _ in configs], |
| 142 | + ids=test_ids, |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.benchmark |
| 147 | +def test_valgrind(runner, valgrind_args): |
| 148 | + if runner: |
| 149 | + runner.run_valgrind(*valgrind_args) |
| 150 | + |
| 151 | + |
| 152 | +def main(): |
| 153 | + parser = argparse.ArgumentParser( |
| 154 | + description="Benchmark Valgrind with pytest-codspeed", |
| 155 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 156 | + epilog=""" |
| 157 | +Examples: |
| 158 | + # Run with a binary path |
| 159 | + uv run bench.py --cmd /path/to/binary |
| 160 | +
|
| 161 | + # Run with an arbitrary command |
| 162 | + uv run bench.py --cmd 'echo "hello world"' |
| 163 | +
|
| 164 | + # Run with custom valgrind installation |
| 165 | + uv run bench.py --cmd /usr/bin/ls --valgrind-path /usr/local/bin/valgrind |
| 166 | + """, |
| 167 | + ) |
| 168 | + |
| 169 | + parser.add_argument( |
| 170 | + "--cmd", |
| 171 | + type=str, |
| 172 | + required=True, |
| 173 | + help="Command to profile (can be a path to a binary or any arbitrary command)", |
| 174 | + ) |
| 175 | + parser.add_argument( |
| 176 | + "--valgrind-path", |
| 177 | + type=str, |
| 178 | + default="valgrind", |
| 179 | + help="Path to valgrind executable (default: valgrind)", |
| 180 | + ) |
| 181 | + parser.add_argument( |
| 182 | + "--output-dir", |
| 183 | + type=str, |
| 184 | + default="/tmp", |
| 185 | + help="Directory for callgrind files (default: /tmp)", |
| 186 | + ) |
| 187 | + |
| 188 | + args = parser.parse_args() |
| 189 | + |
| 190 | + # Create runner instance |
| 191 | + runner = ValgrindRunner( |
| 192 | + cmd=args.cmd, |
| 193 | + valgrind_path=args.valgrind_path, |
| 194 | + output_dir=args.output_dir, |
| 195 | + ) |
| 196 | + print(f"Valgrind version: {runner.valgrind_version}") |
| 197 | + print(f"Command: {args.cmd}") |
| 198 | + |
| 199 | + # Plugin to pass runner to tests |
| 200 | + class RunnerPlugin: |
| 201 | + def pytest_configure(self, config): |
| 202 | + config._valgrind_runner = runner |
| 203 | + |
| 204 | + exit_code = pytest.main( |
| 205 | + [__file__, "-v", "--codspeed"], |
| 206 | + plugins=[RunnerPlugin()], |
| 207 | + ) |
| 208 | + if exit_code != 0 and exit_code != 5: |
| 209 | + print(f"Benchmark execution returned exit code: {exit_code}") |
| 210 | + |
| 211 | + |
| 212 | +if __name__ == "__main__": |
| 213 | + main() |
0 commit comments