-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathttfb.py
More file actions
executable file
·268 lines (218 loc) · 8.74 KB
/
Copy pathttfb.py
File metadata and controls
executable file
·268 lines (218 loc) · 8.74 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
#!/usr/bin/env python3
import argparse
import asyncio
import hashlib
import json
import os
import subprocess
import sys
import time
from typing import List
import httpx
STATE_FILE_PATH = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"var",
"ncps",
"state.json",
)
TTFB_TIMEOUT_SECONDS = 180.0 # Adjust this value as needed
def nix_hash_to_hex(hash_str: str) -> str:
"""
Convert a Nix hash (SRI format like sha256:nix32hash) to hex using nix-hash.
This handles both nix32 and hex formats:
- sha256:nix32hash -> converts nix32 to hex
- sha256:hexhash -> passes through as hex
- sha256:sha256:hex -> extracts inner hex
"""
if not hash_str:
return ""
# Parse the SRI format
if not hash_str.startswith("sha256:"):
return hash_str
encoded = hash_str[7:] # Remove "sha256:" prefix
# Handle double-encoded format: sha256:sha256:hex
if encoded.startswith("sha256:"):
return encoded[7:] # Extract inner hex
# Otherwise, it's nix32 format - convert to hex using nix-hash
try:
result = subprocess.run(
["nix-hash", "--sri", "--to-base16", hash_str],
capture_output=True,
text=True,
check=True,
)
return result.stdout.strip()
except (subprocess.CalledProcessError, FileNotFoundError):
# nix-hash is required for verification.
print("Error: 'nix-hash' command not found or failed. It is required for hash verification.", file=sys.stderr)
sys.exit(1)
def parse_narinfo(text: str) -> dict:
"""Parse narinfo text and extract relevant fields."""
info = {}
for line in text.splitlines():
if ": " in line:
key, value = line.split(": ", 1)
info[key.strip()] = value.strip()
return info
def get_expected_hash_info(info: dict) -> tuple[str, str]:
"""Get the expected hash and type from narinfo."""
compression = info.get("Compression", "").lower()
# Determine which hash to use
if compression == "none":
# When compression is none, FileHash is expected empty, use NarHash
expected_hash = info.get("NarHash", "")
hash_type = "NarHash"
else:
# In all other cases, FileHash is used/required
expected_hash = info.get("FileHash", "")
hash_type = "FileHash"
return expected_hash, hash_type
def verify_nar_hash(content: bytes, info: dict) -> tuple[bool, str, str]:
"""
Verify NAR content hash against the narinfo.
Returns:
tuple of (passed: bool, expected_hex: str, actual_hash: str)
"""
expected_hash, _ = get_expected_hash_info(info)
if not expected_hash:
return True, "", ""
# Parse the expected hash to get hex digest
expected_hex = nix_hash_to_hex(expected_hash)
# Compute SHA256 hash of the content
actual_hash = hashlib.sha256(content).hexdigest()
# Compare
passed = actual_hash == expected_hex
return passed, expected_hex, actual_hash
def get_urls_from_state_file() -> List[str]:
"""Read running instance URLs from run.py's state file."""
try:
with open(STATE_FILE_PATH) as f:
data = json.load(f)
return [
f"http://127.0.0.1:{inst['port']}" for inst in data.get("instances", [])
]
except (FileNotFoundError, KeyError, json.JSONDecodeError):
return []
async def fetch_with_verification(client, base_url, path, narinfo, do_verify: bool):
"""Fetch NAR and optionally verify its hash."""
url = f"{base_url}/{path}"
start_time = time.perf_counter()
ttfb = None
try:
# Use streaming to capture the moment the first byte arrives
async with client.stream("GET", url) as response:
# aiter_bytes() triggers the read, which is subject to the 'read' timeout
content = bytearray()
async for chunk in response.aiter_bytes():
content.extend(chunk)
if ttfb is None:
ttfb = time.perf_counter() - start_time
total_time = time.perf_counter() - start_time
result = {
"url": base_url,
"ttfb": f"{ttfb:.4f}s" if ttfb else "N/A",
"total": f"{total_time:.4f}s",
"status": response.status_code,
"hash_passed": None,
"expected_hash": "",
"actual_hash": "",
}
# Verify hash if requested
if do_verify:
passed, expected, actual = verify_nar_hash(bytes(content), narinfo)
result["hash_passed"] = "PASSED" if passed else "FAILED"
result["expected_hash"] = expected
result["actual_hash"] = actual
return result
except httpx.TimeoutException:
return {
"url": base_url,
"error": f"Timeout: No response within {TTFB_TIMEOUT_SECONDS}s",
"hash_passed": None,
}
except Exception as e:
return {"url": base_url, "error": str(e), "hash_passed": None}
async def main():
parser = argparse.ArgumentParser(
description="Measure NAR latency across ncps instances."
)
parser.add_argument("hash", help="The Nix store hash (e.g., 9cmq42r...)")
parser.add_argument(
"--ncps-url",
action="append",
help="Base URL of an ncps instance (can be specified multiple times)",
)
parser.add_argument(
"--no-verify",
action="store_true",
help="Skip NAR hash verification",
)
args = parser.parse_args()
default_urls = get_urls_from_state_file()
target_urls = [u.rstrip("/") for u in (args.ncps_url or default_urls)]
if not target_urls:
print(
"error: No ncps instances found (state file not present and no --ncps-url given)."
)
sys.exit(1)
narinfo_url = f"{target_urls[0]}/{args.hash}.narinfo"
# Define the timeout structure. 'read' specifically limits the TTFB.
timeout = httpx.Timeout(connect=5.0, read=TTFB_TIMEOUT_SECONDS, write=5.0, pool=5.0)
async with httpx.AsyncClient(timeout=timeout) as client:
# 1. Fetch narinfo from first instance
try:
resp = await client.get(narinfo_url)
resp.raise_for_status()
except Exception as e:
print(f"Error fetching narinfo: {e}")
sys.exit(1)
# 2. Parse narinfo fields
narinfo = parse_narinfo(resp.text)
# 3. Parse URL entry
nar_path = narinfo.get("URL", "")
if not nar_path:
print("Could not find 'URL' entry in narinfo.")
sys.exit(1)
# Print what we're testing
do_verify = not args.no_verify
if do_verify:
expected_hash, hash_type = get_expected_hash_info(narinfo)
expected_hex = nix_hash_to_hex(expected_hash)
print(f"Testing NAR: {nar_path}")
print(f" Expected hash ({hash_type}): {expected_hex}\n")
else:
print(f"Testing NAR: {nar_path}")
print(" Skipping hash verification (--no-verify)\n")
# 4. Call in parallel across all instances with optional verification
tasks = [fetch_with_verification(client, url, nar_path, narinfo, do_verify) for url in target_urls]
results = await asyncio.gather(*tasks)
# 5. Report results
url_width = max(len(r.get("url", "")) for r in results)
if do_verify:
print(f"{'URL':<{url_width}} | {'TTFB':<10} | {'Total Time':<12} | {'Status':<6} | {'Hash'}")
print("-" * (url_width + 50))
else:
print(f"{'URL':<{url_width}} | {'TTFB':<10} | {'Total Time':<12} | {'Status'}")
print("-" * (url_width + 40))
failed_hashes = []
for r in results:
if "error" in r:
print(f"{r['url']:<{url_width}} | ERROR: {r['error']}")
else:
if do_verify:
status = r.get("status", "")
hash_ver = r.get("hash_passed", "N/A")
print(f"{r['url']:<{url_width}} | {r['ttfb']:<10} | {r['total']:<12} | {status:<6} | {hash_ver}")
if hash_ver == "FAILED":
failed_hashes.append(r)
else:
print(f"{r['url']:<{url_width}} | {r['ttfb']:<10} | {r['total']:<12} | {r['status']}")
# 6. If there are hash failures, print details
if failed_hashes:
print("\nHash verification failures:")
print(f"{'URL':<{url_width}} | {'Expected':<64} | {'Actual'}")
print("-" * (url_width + 80))
for r in failed_hashes:
print(f"{r['url']:<{url_width}} | {r['expected_hash']:<64} | {r['actual_hash']}")
if __name__ == "__main__":
asyncio.run(main())