forked from csp-community/csp-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
276 lines (217 loc) · 9.24 KB
/
Copy pathrunner.py
File metadata and controls
276 lines (217 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
Benchmark runner for Hetzner Cloud servers.
"""
from __future__ import annotations
import logging
import subprocess
import tempfile
from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from hcloud.servers import BoundServer
logger = logging.getLogger(__name__)
@dataclass
class BenchmarkConfig:
"""Configuration for benchmark runs."""
csp_repo: str = "https://github.com/Point72/csp.git"
benchmark_repo: str = "https://github.com/csp-community/csp-benchmarks.git"
branches: list[str] = field(default_factory=lambda: ["main"])
python_version: str = "3.11"
commit_range: str | None = None # e.g., "HEAD~5..HEAD"
class HetznerBenchmarkRunner:
"""
Runs ASV benchmarks on a Hetzner Cloud server.
This class handles:
1. SSH connection to the server
2. Setting up the benchmark environment
3. Running ASV benchmarks
4. Collecting and returning results
"""
def __init__(
self,
server: BoundServer,
config: BenchmarkConfig | None = None,
ssh_key_path: str | None = None,
):
"""
Initialize the benchmark runner.
Args:
server: The Hetzner server to run benchmarks on
config: Benchmark configuration
ssh_key_path: Path to SSH private key for authentication
"""
self.server = server
self.config = config or BenchmarkConfig()
self.ssh_key_path = ssh_key_path
self.server_ip = server.public_net.ipv4.ip
def run_benchmarks(self) -> dict:
"""
Run the full benchmark suite on the remote server.
Returns:
Dictionary containing benchmark results and metadata
"""
logger.info(f"Starting benchmarks on {self.server_ip}")
# Setup the environment
self._setup_environment()
# Run ASV benchmarks
results = self._run_asv()
# Collect results
return self._collect_results(results)
def _run_ssh_command(self, command: str, check: bool = True) -> subprocess.CompletedProcess:
"""Run a command on the remote server via SSH."""
ssh_args = [
"ssh",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
]
if self.ssh_key_path:
ssh_args.extend(["-i", self.ssh_key_path])
ssh_args.extend([f"root@{self.server_ip}", command])
logger.debug(f"Running SSH command: {command}")
result = subprocess.run(ssh_args, capture_output=True, text=True, check=check)
if result.stdout:
logger.debug(f"stdout: {result.stdout}")
if result.stderr:
logger.debug(f"stderr: {result.stderr}")
return result
def _scp_to_server(self, local_path: str, remote_path: str) -> None:
"""Copy a file to the remote server."""
scp_args = [
"scp",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
]
if self.ssh_key_path:
scp_args.extend(["-i", self.ssh_key_path])
scp_args.extend([local_path, f"root@{self.server_ip}:{remote_path}"])
subprocess.run(scp_args, check=True)
def _scp_from_server(self, remote_path: str, local_path: str) -> None:
"""Copy a file from the remote server."""
scp_args = [
"scp",
"-r",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
]
if self.ssh_key_path:
scp_args.extend(["-i", self.ssh_key_path])
scp_args.extend([f"root@{self.server_ip}:{remote_path}", local_path])
subprocess.run(scp_args, check=True)
def _wait_for_ssh(self, timeout: int = 120, interval: int = 10) -> None:
"""Wait for SSH to become available on the server."""
import time
logger.info("Waiting for SSH to become available...")
start_time = time.time()
while time.time() - start_time < timeout:
try:
result = self._run_ssh_command("echo 'SSH ready'", check=False)
if result.returncode == 0:
logger.info("SSH connection established")
return
except Exception:
pass
logger.debug(f"SSH not ready, retrying in {interval}s...")
time.sleep(interval)
raise TimeoutError(f"SSH not available after {timeout} seconds")
def _setup_environment(self) -> None:
"""Set up the benchmark environment on the remote server."""
logger.info("Setting up benchmark environment...")
# Wait for SSH to become available
self._wait_for_ssh()
# Determine machine name based on server type
server_type = self.server.server_type.name
machine_name = f"hetzner-{server_type}"
commands = [
# Wait for cloud-init to complete
"cloud-init status --wait || true",
# Install uv
"curl -LsSf https://astral.sh/uv/install.sh | sh",
# Add uv to PATH for this session
"export PATH=$HOME/.local/bin:$PATH",
# Install Python versions with uv
"$HOME/.local/bin/uv python install 3.11 3.12 3.13",
# Clone the benchmark repository
f"git clone {self.config.benchmark_repo} /root/csp-benchmarks",
# Set up Python environment using uv
"cd /root/csp-benchmarks && $HOME/.local/bin/uv venv .venv --python 3.11",
"cd /root/csp-benchmarks && $HOME/.local/bin/uv pip install --upgrade pip",
"cd /root/csp-benchmarks && $HOME/.local/bin/uv pip install -e '.[develop]'",
# Copy machine file to ~/.asv-machine.json
"cp /root/csp-benchmarks/csp_benchmarks/asv-machine.json ~/.asv-machine.json",
]
for cmd in commands:
self._run_ssh_command(cmd)
# Store machine name for use in _run_asv
self._machine_name = machine_name
logger.info(f"Environment setup complete (machine: {machine_name})")
def _run_asv(self) -> str:
"""Run ASV benchmarks and return the output."""
logger.info("Running ASV benchmarks...")
# Determine which commits to benchmark
if self.config.commit_range:
commit_spec = self.config.commit_range
else:
# Just benchmark the latest commit on each branch
commit_spec = " ".join(f"{branch}^!" for branch in self.config.branches)
# Use the machine name determined during setup
machine_arg = f"--machine {self._machine_name}" if hasattr(self, "_machine_name") else ""
cmd = f"cd /root/csp-benchmarks && .venv/bin/python -m asv run --config csp_benchmarks/asv.conf.json {machine_arg} --verbose {commit_spec}"
result = self._run_ssh_command(cmd, check=False)
return result.stdout + result.stderr
def _collect_results(self, asv_output: str) -> dict:
"""Collect benchmark results from the remote server."""
logger.info("Collecting benchmark results...")
# Create a temporary directory to store results
with tempfile.TemporaryDirectory() as tmpdir:
local_results = Path(tmpdir) / "results"
local_results.mkdir()
# Copy results from server
self._scp_from_server("/root/csp-benchmarks/results/", str(local_results))
# Read and parse results
results = {
"server": {
"name": self.server.name,
"id": self.server.id,
"type": self.server.server_type.name,
"ip": self.server_ip,
},
"asv_output": asv_output,
"results_files": [],
}
for result_file in local_results.rglob("*.json"):
results["results_files"].append(
{
"name": result_file.name,
"content": result_file.read_text(),
}
)
return results
def push_results_to_repo(self, github_token: str | None = None) -> None:
"""
Push benchmark results back to the repository.
Args:
github_token: Optional GitHub token for authentication
"""
logger.info("Pushing results to repository...")
commands = [
"cd /root/csp-benchmarks && git config user.email 'benchmark-bot@example.com'",
"cd /root/csp-benchmarks && git config user.name 'Benchmark Bot'",
"cd /root/csp-benchmarks && git add results/",
"cd /root/csp-benchmarks && git commit -m 'Add benchmark results' || true",
]
if github_token:
# Use token for authentication
push_url = self.config.benchmark_repo.replace("https://", f"https://x-access-token:{github_token}@")
commands.append(f"cd /root/csp-benchmarks && git push {push_url} HEAD:main")
else:
commands.append("cd /root/csp-benchmarks && git push origin main")
for cmd in commands:
self._run_ssh_command(cmd, check=False)
logger.info("Results pushed successfully")